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

16 views (last 30 days)
%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?

Answers (2)

Michael scheinfeild
Michael scheinfeild on 9 Jul 2014
inpolygon

Image Analyst
Image Analyst on 22 Aug 2022
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.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!