Jame
Jame

Reputation: 3854

Generate a Gaussian Image without adding noise

I want to generate a Gaussian Image by Matlab. It has 3 circles (as three classes). The intensity in each circle will be followed by Gaussian distribution. Thus, the histogram of the image will be multiplicate Gaussian distribution as a question. However, I used a free-noise image and added it with Gaussian noise to make the multiplicate Gaussian distribution, it is very noise. In this question, I am looking for a way to generate synthetic Gaussian image, which different with my previous method (adding noise). Thank for reading

Upvotes: 2

Views: 1152

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

The following code creates an image following a mixture of 3 Gaussians (very easily extrapolable to more Gaussians if needed) by generating a monotonically decreasing square pattern image.

The way it work is

  1. Generate random numbers following desired distributions
  2. Sort said numbers
  3. Spiral from center of image outwards, and insert sorted values pixel by pixel

Code:

rows=256; columns=256;
grayImage=zeros(rows,columns);

% We will work with doubles in the range of 0-255, and then we will
% discretize, for the shake of dealing properly with random numbers

%define gaussians
mean1=30;   std1=10;
mean2=100;  std2=10;
mean3=130;  std3=5;

% how many points on each of them?? 
% equal:
n=ceil(rows*columns/3);
% Random samples I tested to make it look as your image
n1=ceil(rows*columns/5);
n2=ceil(2/5*rows*columns);
n3=ceil(2/5*rows*columns);
%generate random numbers
rnd1=normrnd(mean1,std1,n1,1);
rnd2=normrnd(mean2,std2,n2,1);
rnd3=normrnd(mean3,std3,n3,1);

%now the hard part.


rnd=[rnd1;rnd2;rnd3];
% Does this looks like what you want? Tune above parameters if it doesnt.
% histogram(rnd)

% Sort the data
rnd=sort(rnd,'descend');


% Here comes the tricky part: filling the image. I chose square shaped, and
% I fill it in a spiral, starting from the center
% web('https://stackoverflow.com/questions/398299/looping-in-a-spiral')
x= 0;
y= 0;
dx = 0;
dy = -1;
next=1;
for ii= 1:rows*columns
    if (-rows/2 < x <= rows/2) && (-columns/2 < y <= columns/2)
        grayImage(x+columns/2,y+rows/2)=rnd(next);
        next=next+1;
    end
    if x == y || (x < 0 && x == -y) || (x > 0 && x == 1-y)
        auxdx=dx;
        dx=-dy; 
        dy =auxdx;
    end
    x=x+dx;
    y=y+dy;
end


% 
subplot(121);imshow(uint8(grayImage)); title('Syntetic image');
subplot(122);imhist(uint8(grayImage)); title('Histogram');

Output:

enter image description here

Please, do not hesitate to ask me any question about the code, but hopefully it is quite self explanatory.

Upvotes: 4

Related Questions