本文介绍了如何在MATLAB中生成多重2D高斯图像分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想在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这是我的代码。但是,它没有达到我预期的直方图。你能帮我生成一个直方图如上图所示的图像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 figurerows=256; columns=256;grayImage=zeros(rows,columns);t = linspace(0,2*pi,50); % approximated by 100 linesr = (rows-10)/2; % circles will be separated by a 10 pixels bordercircle1 = 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.从unit8转换为double时的第一个。您不需要除以最大值,但需要除以255,因为这是理论上的最大值,无论您是否在图像中都有它(否则,为什么稍后再乘以255?!?)。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?!?!).另外,我将这两个图像显示为uint8。更改后的代码类似于: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 linesr = (rows-10)/2; % circles will be separated by a 10 pixels bordercircle1 = 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');提供图像:嗯,现在直方图看起来更相似了。但为什么不一样呢?Well, now the histogram looks more similar. But why is not the same??有两种不同的事情正在发生。一个是真正的直方图从0到50之间的大量值开始,第二个是这个高斯的大小不适合你的客观直方图中的大小。让我们一个接一个地解决它们。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.第一个问题是有点像:你的图像不仅有3种颜色,还有黑色背景!因此,您的直方图将具有3高斯和大量黑色。如果你想删除它,你需要确保你的3个级别覆盖整个图像,不留下黑色区域。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".第二个问题是siz of这三个疙瘩,高斯人。要理解这一点,你需要知道这些高斯的振幅取决于这些单色斑点占据的面积。深灰色的像素量大于白色像素量,因此对应于该颜色的高斯分量更高。做一些简单的圆形区域计算,你应该能够计算你想要与其他圆形成比例的圆形区域,以便让高斯大小达到你想要的大小。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. 这篇关于如何在MATLAB中生成多重2D高斯图像分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 05:34