How do I get only the centroid inside the bounding box?

4 ビュー (過去 30 日間)
Suhas Yelluru
Suhas Yelluru 2014 年 7 月 9 日
回答済み: Image Analyst 2022 年 8 月 22 日
%compute area centroid and boundingbox
stats = regionprops(bw6, ['basic']);
s = regionprops(bw6, 'centroid');
centroids = cat(1, s.Centroid);
imshow(bw6);
%find size of stats
[N,M] = size(stats);
if (bw==0)
break;
else
tmp = stats(1);
for i = 2 : N
if stats(i).Area > tmp.Area
tmp = stats(i);
end
end
bb = tmp.BoundingBox;
bc = tmp.Centroid;
imshow(data)
hold on
rectangle('Position', bb, 'EdgeColor', 'r', 'LineWidth', 2)
_*plot(centroids(:,1), centroids(:,2), 'b*');*_
hold off
The above code shows all the centroids, however, i want only the centroid that is inside the bounding box. How do i extract the coordinates of only that particular centroid?

回答 (2 件)

Michael scheinfeild
Michael scheinfeild 2014 年 7 月 9 日
inpolygon

Image Analyst
Image Analyst 2022 年 8 月 22 日
The centroid of a blob will ALWAYS be inside the bounding box of the blob. How could it not be? Can you think of an example where it's not? No, you can't.
It might not be inside the blob's white pixels, like for the case of a blob shaped like the letter G, or a blob that is a donut shape, but it will always be guaranteed to be inside the bounding box of the blob.

Community Treasure Hunt

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

Start Hunting!

Translated by