How to find number of pixels (or matrix count) in a group in a given imagesc or data

10 views (last 30 days)
Hello everyone,
I need a help to find all the number of pixels present in the data. I have attached the data and the imagesc of it. The data contains the height information of pixel. I have already updated the data according to my requirement by limiting the height value and change to 'nan'.
The blue represents the 'Nan' and the yellow represents the heights that are accepted. As marked in the imagesc figure, i need a new array that will store value like 2,1,1,1,11,5,3,3,... each represents the group of pixels includig single pixel.
Please look at the attached data.xlsx and help me to find, how to get the output as i am expecting.

Accepted Answer

Dave B
Dave B on 20 Dec 2021
There are a few functions for finding little regions like this, but if you have the Image Processing Toolbox you might consider regionprops, which works well for htis kind of problem.
% Area gives the number of pixels in a region
ans = 1
ans = 1
ans = 2
% PixelIdxList is good if you want to go back to the original image and see
% what was in there (other than that it wasn't NaN)
ans = -378
ans = -250
ans = 2×1
-277 -419

More Answers (1)

Image Analyst
Image Analyst on 20 Dec 2021
Don't use nan. To find the number of non-zero pixels set all your nans to 0 and use nnz
grayImage(isnan(grayImage)) = 0; % Make Nan's equal to zero.
numNonZeroPixels = nnz(grayImage)
To get the area and intensity of each 4-connected blob, do this:
labeledImage = bwlabel(grayImage > 0, 4);
props = regionprops(labeledImage, grayImage, 'Area', 'MeanIntensity');
allAreas = [props.Area];
allIntensities = [props.MeanIntensity];

Community Treasure Hunt

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

Start Hunting!

Translated by