Displaying area of each labeled region

8 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2012-2-8
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 个评论
Walter Roberson
Walter Roberson 2012-2-9
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 2012-2-9
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.)

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2012-2-8
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 个评论
MMSAAH
MMSAAH 2020-5-6
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 2020-5-6
OK. Sorry about that. Lately I've just been using props instead of blobMeasurements because it's shorter to type.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by