From: Ryan on
I am trying to create a gaussian fit of data that is arranged in a histogram. The histogram was created from an MRI image (size 256x256x256), and has 124568 data points centered between 50 and 60. I do not have any statistical background, so everything i find online about Gaussian fits does not make much sense.

Is there a way to make a gaussian fit from a histogram? If so, can somebody give me an example of a script this so that I can try to adapt it to my data?

Thanks!
From: ImageAnalyst on
Can't you just take the mean and standard deviation of your data?
Something along these lines:

[pixelCount grayLevels] = imhist(grayImage);
meanBin = sum(pixelCount .* grayLevels) / sum(pixelCount)
StDevOfBins1 = sum(pixelCount .* (grayLevels - meanBin).^2) /
sum(pixelCount) % One method
StDevOfBins2 = sum(pixelCount .* grayLevels .^2) / sum(pixelCount) -
meanBin^2 % Same as above

Once you have the mean and stddev, doesn't that pretty much define the
Gaussian?
From: Image Analyst on
Ryan :
Sorry -- I gave you the wrong formula, plus you'll have errors with it because it didn't convert to floating point and there will be clipping if your data is uint8 that will give errors. I believe the correct formulas are below for getting mean, std dev, and variance:

% Read in standard MATLAB gray scale demo image.
grayImage = imread('cell.tif');
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.

% Just for fun, let's get its histogram.
[pixelCount grayLevels] = imhist(grayImage, 256);
subplot(2, 1, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
set(gca,'XTick',[0:10:grayLevels(end)]);

% Convert to double so you don't have clipping to the range 0-255.
doubleGrayImage = double(grayImage);
% Compute mean by two different methods:
meanBin = sum(pixelCount .* grayLevels) / sum(pixelCount) % Assumes each bin is one gray level wide.
meanGrayLevel = mean2(doubleGrayImage)

% Compute variance by five different methods:
varianceOfImage1 = var(doubleGrayImage(:))
varianceOfImage2 = mean((doubleGrayImage(:) - meanGrayLevel).^2)
squareImage = doubleGrayImage .^2;
varianceOfImage3 = mean(squareImage(:)) - meanGrayLevel.^2
% Do it using histogram.
varianceOfImage4 = sum(pixelCount .* (grayLevels - meanBin).^2) / sum(pixelCount)
varianceOfImage5 = sum(pixelCount .* grayLevels .^2) / sum(pixelCount) - meanBin^2

% Compute standard deviation by four different methods:
StDevOfImage1 = std(doubleGrayImage(:))
StDevOfImage2 = sqrt(varianceOfImage1)
% Do it using histogram.
StDevOfImage3 = sqrt(sum(pixelCount .* (grayLevels - meanBin).^2) / sum(pixelCount))
StDevOfImage4 = sqrt(sum(pixelCount .* grayLevels .^2) / sum(pixelCount) - meanBin^2)

uiwait(msgbox('Done. Look in the command window.'));