How to find number of pixels (or matrix count) in a group in a given imagesc or data
19 views (last 30 days)
Show older comments
Chandrashekar D S
on 20 Dec 2021
Answered: Image Analyst
on 20 Dec 2021
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.
0 Comments
Accepted Answer
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
b(2).Area
b(3).Area
% 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)
a(b(2).PixelIdxList)
a(b(3).PixelIdxList)
0 Comments
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];
0 Comments
See Also
Categories
Find more on Detection in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!