Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a multiplicate 2D Gaussian Image distribution in MATLAB

I want to generate a multiplicate Gaussian image in MATLAB. The image includes three circles. The intensity in each circle follows a gaussian distribution. Totally, the histogram of the image will be a muliplicate Gaussian distribution as expected histogram

enter image description here

This is my code. However, it does not achieve my expected histogram. Could you help me to generate a image that has histogram as above figure

rows=256; columns=256;
grayImage=zeros(rows,columns);
t = linspace(0,2*pi,50);   % approximated by 100 lines
r = (rows-10)/2;              % circles will be separated by a 10 pixels border
circle1 = poly2mask(r*cos(t)+rows/2+0.5, r*sin(t)+columns/2+0.5, rows, columns);

r = (rows-10)/3;
circle2 = poly2mask(r*cos(t)+rows/2+0.5, r*sin(t)+columns/2+0.5, rows, columns);

r = (rows-10)/5;
circle3 = poly2mask(r*cos(t)+rows/2+0.5, r*sin(t)+columns/2+0.5, rows, columns);

grayImage(circle1) =30; 
grayImage(circle2) =100; 
grayImage(circle3) =130; 
im_normal=double(grayImage)./max(grayImage(:));
v = var(im_normal(:));
im_noise= imnoise(im_normal,'gaussian',0,v/20);
subplot(131);imshow(grayImage,[]); title('Free-noise image');
subplot(132);imshow(im_noise);title('Noisy image');
subplot(133);imhist(uint8(255.*im_noise)); title('Hist. of noisy mage');

This is my image for above code. Thank all

enter image description here

like image 994
Jame Avatar asked Sep 17 '25 18:09

Jame


1 Answers

First, you have a couple of things wrong in your code.

The first one in when you convert from unit8 to double. You dont need to divide by the maximum, but by 255, as that is the theoretical maximum, whether you have it in your image or not (else, why do you multiply bu 255 later?!?!).

Also, I show both images as uint8. The changed code loks like:

rows=256; columns=256;
grayImage=zeros(rows,columns);
t = linspace(0,2*pi,50);   % approximated by 100 lines
r = (rows-10)/2;              % circles will be separated by a 10 pixels border
circle1 = poly2mask(r*cos(t)+rows/2+0.5, r*sin(t)+columns/2+0.5, rows, columns);

r = (rows-10)/3;
circle2 = poly2mask(r*cos(t)+rows/2+0.5, r*sin(t)+columns/2+0.5, rows, columns);

r = (rows-10)/5;
circle3 = poly2mask(r*cos(t)+rows/2+0.5, r*sin(t)+columns/2+0.5, rows, columns);

grayImage(circle1) =30; 
grayImage(circle2) =100; 
grayImage(circle3) =130; 
im_normal=double(grayImage)./255;
v = var(im_normal(:));
im_noise= imnoise(im_normal,'gaussian',0,v/20);
subplot(131);imshow(grayImage,[]); title('Free-noise image');
subplot(132);imshow(mat2gray(im_noise),[]);title('Noisy image');
subplot(133);imhist(uint8(255.*im_noise)); title('Hist. of noisy mage');

Giving an image as:

enter image description here

Well, now the histogram looks more similar. But why is not the same??

There are 2 different things that are happening. One is that the real histogram starts with alot of values between 0-50 and the second one is the size of this gaussians does not fit the ones in your "objective" histogram. Lets address them one by one.

The first problem is kind of ovbious: Your image has not only 3 colors, but a black background! Thus, your histogram will have 3 Gaussian and a lot of black. If you want to remove this, you need to make sure that your 3 levels cover the WHOLE image, leaving no "black areas".

The second problem is the siz of these three lumps, the gaussians. To understand this, you need to be aware that the amplitude of these gaussians depends on the area occupied by these "single color" blobs. The amount of pixels in dark gray is way bigger than the amount of pixels in white, thus the Gaussian corresponding to that color is higher. Doing some simple circle area computations, you should be able to compute the area of the circle you want to be proportional to other circles, in order to have the gaussians the size you want.

like image 168
Ander Biguri Avatar answered Sep 21 '25 07:09

Ander Biguri