Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bounding box using MATLAB for the image

I am trying to draw a bounding box around the white blob in the image below:

enter image description here

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: enter image description here

Can someone one tell me how to draw a bounding box around this blob(white)?

Update Wajih's answer actually accurately solved the issue.

like image 996
gpuguy Avatar asked Nov 19 '25 03:11

gpuguy


1 Answers

Pseduo -

  • Pick largest y, largest x, smallest x, smallest y with in the blob. That is, points on the blob. These are your coordinates that you can use to build the bounding box.

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.

  • I have marked the points on your image that you can use to create the bounding box.
  • Zoomed Image to get a better view. Makred Zoomed

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!