How to calculate variance, kurtosis, and skewness of a ROI (Region of Interest) in grayscale image?

2 views (last 30 days)
I'm going to calculate means, standard deviation, kurtosis, skewness, and also variance from a ROI of grayscale image. I create select the ROI using mask. This is the code I use
Img = imread('image_name.jpg');
GrayImg = rgb2gray(Img);
h = impoly(gca);
wait(h);
mask = createMask(h);
Mean = mean2(GrayImg(mask));
StandardDeviation = std2(GrayImg(mask));
How to calculate variance, kurtosis and skewness of the grayscale image using the mask? Is the code to calculate mean and standard deviation is correct?

Answers (2)

Image Analyst
Image Analyst on 29 Dec 2024
See my attached demo for computing image moments. Adapt as needed.

ag
ag on 29 Dec 2024
Hi Yogi,
To calculate the statistical measures such as mean, standard deviation, variance, kurtosis, and skewness for a region of interest (ROI) in a grayscale image, you can utilize a mask to define the ROI. Below is an improved version of your code, which accurately performs these calculations:
% Read the image and convert it to grayscale
Img = imread('image_name.jpg');
GrayImg = rgb2gray(Img);
% Select the ROI using a polygon tool
h = impoly(gca);
wait(h);
% Generate a mask from the selected polygon
mask = createMask(h);
% Extract the pixel values within the mask
maskedPixels = GrayImg(mask);
% Compute the statistical measures
Mean = mean(maskedPixels);
StandardDeviation = std(maskedPixels);
Variance = var(maskedPixels);
Kurtosis = kurtosis(double(maskedPixels)); % Convert to double for kurtosis
Skewness = skewness(double(maskedPixels)); % Convert to double for skewness
% Display the results
For more details, please refer to the following MathWorks documentations:
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!