Reputation: 4585
I am trying to draw a bounding box around the white blob in the image below:
I did like this:
bw = imread('box.jpg');
bw=im2bw(bw);
imshow(bw)
L = bwlabel(bw);
s = regionprops(L, 'Area', 'BoundingBox');
s(1);
area_values = [s.Area];
idx = find((100 <= area_values) & (area_values <= 1000)); % list of all the objects
%whose area is between 100 and 1000
bw2 = ismember(L, idx); %construct a binary image containing all the objects whose
%area is between 100 and 1000 by passing L and idx to ismember.
imshow(bw2)
The output bw2, so far is:
Can someone one tell me how to draw a bounding box around this blob(white)?
Update Wajih's answer actually accurately solved the issue.
Upvotes: 3
Views: 27989
Reputation: 5913
As the 20,000th viewer of this question, I'll answer what I think the asker is actually asking.
To render a rectangle on the page, you just need to properly interpret the bounding box for the shape. You've already computed this in s(idx).BoundingBox
Thus, add these two lines to your script:
bb = s(idx).BoundingBox;
rectangle('Position',[bb(1) bb(2) bb(3) bb(4)],'EdgeColor','green');
and you'll get:
Upvotes: 3
Reputation: 36
I think you can try to use bwboundries
boundaries = bwboundaries(blob);
numberOfBoundaries = size(boundaries);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
Upvotes: 0
Reputation:
Pseduo -
assuming top left of image as (0,0)
(smallestX,smallestY)-----------------(largestX,smallestY)
| |
| |
| |
| |
(smallestX,largestY)------------------(largestX,largestY)
And for finding minimum/maximum values and indices.
[r,c]=find(img==min(min(img)))
[r,c]=find(img==max(max(img)))
r,c represent row and column in the img matrix.
Upvotes: 3