Calculate mean, median, and mode of an ROI
9 views (last 30 days)
Show older comments
I am having difficulty determining the mean, median, and mode of an ROI.
My first issue is extracting one of the ROI. So far I have tried to use the ROI as an index into the original image. However, I continue to get errors.
Also, I have tried to used imhist, however, I was unable to isolate just one ROI (I could only do it for the entire image).
And lastly, I have tried using the function ksdensity, however, I continue to get the following error "Data values must be between lower and upper 'support' values."
My original image has a value of 192x192 double. And I have created a mask using active contour. The mask also has a value of 192x192 double.
Using bwboundaries I have outlined the desired ROI but I am unable to extract just this region on the original image and then calculate the mean, median, and mode.
Any help would be welcomed. Thank you
0 Comments
Answers (1)
DGM
on 2 May 2023
Isolating one part of a mask might be part of the problem, but otherwise, it's hard to say what was going wrong without seeing the code or the mask. Consider the following.
Say we have a simple grayscale image and a logical mask with multiple regions. We can use bwselect() to select one of the blobs if we know a point in the desired blob.
% a grayscale image of class 'double'
inpict = imread('peppers.png');
inpict = im2gray(inpict);
inpict = im2double(inpict);
% a logical mask with multiple blobs
mask = imread('pepcircmask.png');
% display the composition of image and mask for sake of visualization
imshow(imfuse(inpict,mask,'blend'),'border','tight')
% select the blob associated with the yellow pepper (the SE blob)
thismask = bwselect(mask,150,260);
% the pixels to test
roipixels = inpict(thismask);
% region stats
roimean = mean(roipixels)
roimed = median(roipixels)
roimode = mode(roipixels)
If the image might be RGB, the addressing is only a bit more complicated
% a RGB image of class 'double'
inpict = imread('peppers.png');
inpict = im2double(inpict);
% a logical mask with multiple blobs
mask = imread('pepcircmask.png');
% display the composition of image and mask for sake of visualization
imshow(imfuse(inpict,mask,'blend'),'border','tight')
% select the blob associated with the yellow pepper (the SE blob)
thismask = bwselect(mask,150,260);
% the pixels to test
nc = size(inpict,3);
roipixels = inpict(repmat(thismask,[1 1 nc]));
roipixels = reshape(roipixels,[],nc);
% region stats
roimean = mean(roipixels)
roimed = median(roipixels)
roimode = mode(roipixels)
% display the colors as a 3-px test stripe
imshow(permute([roimean; roimed; roimode],[1 3 2]))
Notice that the mode color is not even remotely representative of the colors in the region. This should be expected when dealing with color images. See also:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

