4

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

Jame
  • 3,746
  • 6
  • 52
  • 101

1 Answers1

3

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.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Thank you for your help. Actually, the first issue is that I did not multiple the image with circle1 (Because I only consider the intensity inside the circle1, thus background will become zeros if I multiple it). Is it possible to make a gaussian image without multiplication with Gaussian Noise as my method? It means the intensity still follows Gaussian, but the image will not be noisy image – Jame Mar 01 '16 at 14:49
  • @user8430 Yeah I guess you can, but it is very ambiguous. There are infintie ways of doing that, you need to constrain the problem further – Ander Biguri Mar 01 '16 at 14:54
  • I found the way/matlab code to do it. But I did not found. Could you suggest to me some way or answer directly in your ans. Thanks – Jame Mar 01 '16 at 14:58
  • @user8430 you could just make a gradient from left to rigth, by generating random normal numebrs, sorting them, and giving each column a value. – Ander Biguri Mar 01 '16 at 15:02
  • Sorry, I did not understand this way. Because Gradient and Gaussian are different, and random normal number also is different with Gaussian .Could you please provide the code and show its histogram? Thanks – Jame Mar 01 '16 at 15:06
  • @user8430 no sorry. I already explained how it can be done, but It seems that you are confused with your mathematical termns. please, take your tyime, read what I wrote and try it yourselsf. Also, you are asking a different question now, that should be addressed in a different SO Q&A, not in this one. Consider if what I answered is the answer of what you asked in the first place. If thatch the case, accept it and ask a new question. – Ander Biguri Mar 01 '16 at 15:37
  • Thanks. I added a new quesiton and accepted your ans http://stackoverflow.com/questions/35727328/generate-a-gaussian-image-without-adding-noise – Jame Mar 01 '16 at 15:49