mardi 29 juin 2021

Generate synthetic random image with predefined randomness

I am trying to generate circular image with two distinct colors. Currently, I can control the ratio of two colors but I want the ability to control the adjacency of the colors too. For example, if the randomness is high then the probability of getting two pixel of same color would be low. Below is my code to generate image so far:

import numpy as np 
import matplotlib.pyplot as plt

class GenerateImage:
    def __init__(self, radius=24):
        self.radius = radius
        self.image = np.zeros(shape=(48, 48, 3))
        
    def generate_random(self, prob=[0.5, 0.5]):
        mask = np.random.choice([0, 1], size=np.prod(self.image.shape[:2]), p=prob)
        mask = mask.reshape(self.image.shape[:2])
        self.image[:, :, 1] = mask * 255
        self.image[:, :, 2] = np.where(mask == 1, 0, 1) * 255
        
        x, y = np.ogrid[0:self.image.shape[0], 0:self.image.shape[1]]
        center_x = self.image.shape[0] / 2 - 0.5
        center_y = self.image.shape[1] / 2 - 0.5
        circle_mask = (x - center_x)**2 + (y - center_y)**2 > self.radius**2
        self.image[circle_mask] = [0,0,0]
        
    
    def display(self):
        plt.imshow(self.image)
        self.generate_random()
        plt.show()
        
        
        
if __name__ == '__main__':
    g = GenerateImage()
    g.generate_random(prob=[0.5, 0.5])
    g.display()

It produces images like:

enter image description here

There are linear patches of blue or green colors that I want to control. What would be a reasonable way to achieve that? Thanks.

Another question (if anyone knows): how to compute the entropy of a colored image (I can use skimage to find entropy of a grayscale image but couldn't find any resource to do that for colored images like the one above)




Aucun commentaire:

Enregistrer un commentaire