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

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