简体   繁体   English

如何在MATLAB中生成多重2D高斯图像分布

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

I want to generate a multiplicate Gaussian image in MATLAB. 我想在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 总的来说,图像的直方图将是一个多重的高斯分布,作为预期的直方图

在此输入图像描述

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 谢谢大家

在此输入图像描述

First, you have a couple of things wrong in your code. 首先,您的代码中存在一些错误。

The first one in when you convert from unit8 to double. 当你从unit8转换为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?!?!). 你不需要除以最大值,但需要除以255,这是理论上的最大值,无论你是否在你的图像中都有它(否则,为什么你以后再乘以255?!?!)。

Also, I show both images as uint8. 另外,我将这两个图像显示为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: 给图像:

在此输入图像描述

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. 一个是真正的直方图以0-50之间的大量值开始,第二个是这个高斯大小不适合你的“客观”直方图中的大小。 Lets address them one by one. 让我们一个接一个地解决它们。

The first problem is kind of ovbious: Your image has not only 3 colors, but a black background! 第一个问题是有点像:你的图像不仅有3种颜色,还有黑色背景! Thus, your histogram will have 3 Gaussian and a lot of black. 因此,您的直方图将具有3高斯和大量黑色。 If you want to remove this, you need to make sure that your 3 levels cover the WHOLE image, leaving no "black areas". 如果你想删除它,你需要确保你的3个级别覆盖整个图像,不留下“黑色区域”。

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. 做一些简单的圆形区域计算,你应该能够计算你想要与其他圆形成比例的圆形区域,以便让高斯大小达到你想要的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM