% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end

%------------------------------------------------------------------------------------------------------
% Computes first, second, third, and fourth central moments of the gray levels.
% Get the mean gray level, standard deviation, skew, and kurtosis from the histogram bin values.
% Note: gray level moments are different than spatial moments which are more like rotational moments of intertia.
% Uses formulas from http://itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
% "Skewness is a measure of symmetry, or more precisely, the lack of symmetry.
% A distribution, or data set, is symmetric if it looks the same to the left and right of the center point.
% The skewness for a normal distribution is zero, and any symmetric data should have a skewness near zero.
% Negative values for the skewness indicate data that are skewed left and positive values for the skewness
% indicate data that are skewed right. By skewed left, we mean that the left tail is long relative to the right tail.
%
% Kurtosis is a measure of whether the data are peaked or flat relative to a normal distribution.
% That is, data sets with high kurtosis tend to have a distinct peak near the mean,
% decline rather rapidly, and have heavy tails. Data sets with low kurtosis tend
% to have a flat top near the mean rather than a sharp peak. A uniform distribution would be the extreme case."
function [meanGL stdDev skew kurtosis] = ComputeImageMoments(GLs, pixelCounts)
try
% Get the number of pixels in the histogram.
numberOfPixels = sum(pixelCounts);
% Get the mean gray lavel.
meanGL = sum(GLs .* pixelCounts) / numberOfPixels;
% Get the variance, which is the second central moment.
varianceGL = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels-1);
% Get the standard deviation.
stdDev = sqrt(varianceGL);
% Get the skew.
skew = sum((GLs - meanGL) .^ 3 .* pixelCounts) / ((numberOfPixels - 1) * stdDev^3);
% Get the kurtosis.
kurtosis = sum((GLs - meanGL) .^ 4 .* pixelCounts) / ((numberOfPixels - 1) * stdDev^4);
catch ME
errorMessage = sprintf('Error in ComputeImageMoments().\nThe error reported by MATLAB is:\n\n%s', ME.message);
uiwait(warndlg(errorMessage));
set(handles.txtInfo, 'String', errorMessage);
end
return; % from ComputeImageMoments