How to find number of pixels (or matrix count) in a group in a given imagesc or data
10 views (last 30 days)
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.
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
% 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)
More Answers (1)
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];