[Math] Gaussian distribution and its parameters

image processingMATLABnormal distribution

I need to learn more about Gaussian distribution and given a set of data, plot a Gaussian distribution of it.

Using the following code sample, could you please tell me how I can plot a Gaussian distribution for it and find the corresponding parameters, mean and sigma for the distribution?

im = imread(im_address);
pixel_freq = zeros(3, 256);

for i=1:256
    pixel_freq(1, i) = length(find(im(:, :, 1) == i));
    pixel_freq(2, i) = length(find(im(:, :, 2) == i));
    pixel_freq(3, i) = length(find(im(:, :, 3) == i));
end

In the pixel_freq variable I store the number of pixels having one of the 256 colors for each layer of an RGB image. Then I want to plot a Gaussian distribution corresponding to each layer of the image. Could you please tell me how I can do that?

The next thing I want to do to find a probability of a point falling in the color range of, say, 50-55 for one of the RGB color layers. How can I calculate this probability?

And finally, given the new pixel color, I want to update the mean and sigma of the distribution. How can I do that? Imagine in one of the color layers of an image, I change the colors of some pixels from 50 to 52. How can I update the new parameters for the new distribution?

Thank you

Best Answer

To plot the histograms, try something like this:

Nbins = 256;
figure;

subplot(3,1,1);    
hist( vec(im(:,:,1)), Nbins );
title( 'Channel R' );

subplot(3,1,2);
hist( vec(im(:,:,2)), Nbins );
title( 'Channel G' );

subplot(3,1,3);
hist( vec(im(:,:,3)), Nbins );
title( 'Channel B' );

To estimate the probability that a pixel is within some range of values, find the number pixels that are within that range and divide them by the total number of pixels:

length( find( vec(img(:,:,1)) < X & vec(img(:,:,1)) > Y ) ) / size(img,1) / size(img,2)

If you want to change the mean of the pixels, I'd first just bias them and clip any values under 0 or over 255. If that's good enough, then you're done. Otherwise, you'll have to come up with some method for taking the clipping into account. To change the variance, just scale the pixels rather than bias them. There may be a better way to do this that I am not aware of.

In the above, the vec() function would be defined by

function y = vec(x)
y = x(:);