Displaying area of each labeled region

8 views (last 30 days)
I would like to display area for each labeled region in the generated result image, after binarize and labeling gray-scale image. This is part of the codes:
hold on;
Measurements = regionprops(labImg, fgImg, 'all');
numberOfBlobs = size(Measurements, 1);
for k = 1:numberOfBlobs
areas=Measurements(k).Area;
plot(areas(:,2), areas(:,1), 2);
end
hold off;
Besides that, is there any idea to display labeled no. and area of labeled image in orders in command window?

Accepted Answer

Walter Roberson
Walter Roberson on 8 Feb 2012
You should be able to use this outside of a loop:
blobareas = [Measurements.Area];
printf('%3d: %5d\n', [1 : length(blobareas); blobareas]);
Note: the Area field returned by regionprops() will always be a scalar, so accessing the second column of it like you do in areas(:,2) will cause an error.
  7 Comments
Walter Roberson
Walter Roberson on 9 Feb 2012
If you only need the area shown on the screen, go back to the code in my Answer (except switching to fprintf and adding the Measurements assignment.)
Please explain further about how you would like to see the values represented as variables? With the code I posted earlier, the K'th blob's area is in blobarea(K)
Image Analyst
Image Analyst on 9 Feb 2012
He wanted to display the area "in the generated result image" so in that case he needs to get the centroid (via regionprops) and create a string with sprintf() and call text() to display it in the image at the location of the centroid, like I do in BlobsDemo. (I think I told him this already in one of his other related questions.)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Feb 2012
That looks strange. Why is areas a 2 element array, or a N by 2 array??? Anyway....
See my BlobsDemo http://www.mathworks.com/matlabcentral/fileexchange/25157 where I plot the label number at the centroid of the object. Basically you get the centroid and any other measurements you want then use sprintf() to create a string, then display that string with text() at the location of the centroid.
  7 Comments
MMSAAH
MMSAAH on 6 May 2020
Thank you for your reply and for sharing.
I ve changed the proposed code by :
blobMeasurements = regionprops(binaryImage, 'Centroid', 'Area', 'EquivDiameter');
% Get all centroids
allCentroids = vertcat(blobMeasurements.Centroid);
% Sort according to centroid's vertical location.
[sorted_y, sortOrder] = sort(allCentroids(:, 2), 'ascend');
% Reorder blobMeasurements
blobMeasurements = blobMeasurements(sortOrder);
Now it works.
Image Analyst
Image Analyst on 6 May 2020
OK. Sorry about that. Lately I've just been using props instead of blobMeasurements because it's shorter to type.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!