Clear Filters
Clear Filters

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

8 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.
a=readmatrix('data.xlsx');
b=regionprops(~isnan(a),'Area','PixelIdxList');
% Area gives the number of pixels in a region
b(1).Area
ans = 1
b(2).Area
ans = 1
b(3).Area
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)
a(b(1).PixelIdxList)
ans = -378
a(b(2).PixelIdxList)
ans = -250
a(b(3).PixelIdxList)
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!