Asked by Felix
on 18 Sep 2013

Hello, I used this code to make rectangle around the object at binary image:

st = regionprops(BW, 'BoundingBox' );

figure, imshow('MY_IMAGE.jpg')

rectangle('Position',[st.BoundingBox(1),st.BoundingBox(2),st.BoundingBox(3),st.BoundingBox(4)],... 'EdgeColor','r','LineWidth',2 )

but if I have more than one object, this code doesn't working How can I draw N rectangles for N objects on image? Thank you

Answer by Image Analyst
on 18 Sep 2013

Accepted Answer

Put it in a loop (untested)

for k = 1 : length(st) thisBB = st(k).BoundingBox; rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],... 'EdgeColor','r','LineWidth',2 ) end

Explorer
on 19 Jan 2014

Sure

Explorer
on 19 Jan 2014

syhem samti
on 19 Nov 2015

does it work the same way as "vision.BlobAnalysis"? same results?

Log in to comment.

Answer by Hafsa Asad
on 28 Jan 2017

Edited by Hafsa Asad
on 28 Jan 2017

Here is an even simpler way to do it if you have the binary image with white blobs

st = regionprops(my_binary_image, 'BoundingBox', 'Area' );

for ii= 1 : length(st) Areai(ii)= st(ii).Area; end largest_blob_id= find(Areai==max(Areai)); imshow(my_binary_image) rectangle('Position'[st(largest_blob_id).BoundingBox(1),st(largest_blob_id).BoundingBox(2),st(largest_blob_id).BoundingBox(3),st(largest_blob_id).BoundingBox(4)], 'EdgeColor','r','LineWidth',2 )

Image Analyst
on 28 Jan 2017

That would be good if he wanted to draw the box around *only* the *largest* blob instead of drawing "N rectangles for N objects". An even shorter way would be to get rid of the for loop like this:

st = regionprops(my_binary_image, 'BoundingBox', 'Area' ); [maxArea, indexOfMax] = max([st.Area]); rectangle('Position'[st(indexOfMax).BoundingBox(1),st(indexOfMax).BoundingBox(2),st(indexOfMax).BoundingBox(3),st(indexOfMax).BoundingBox(4)], 'EdgeColor','r','LineWidth',2 )

Log in to comment.

Answer by Tiago Almeida
on 11 Oct 2017

Edited by Image Analyst
on 11 Oct 2017

How can i draw the rectangles with the same size using this code?

labeledImage = bwlabel(A); measurements = regionprops(labeledImage, 'BoundingBox', 'Area'); for k = 1 : length(measurements) thisBB = measurements(k).BoundingBox; rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],... 'EdgeColor','r','LineWidth',2 ) end

Image Analyst
on 11 Oct 2017

Instead of having variable widths and heights, like this thisBB(3),thisBB(4), replace that with some fixed widths and heights.

rectangle('Position', [thisBB(1),thisBB(2), fixedWidth, fixedHeight],... 'EdgeColor','r','LineWidth',2 )

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## sukku (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/87597-rectangle-around-the-object-bounding-box#comment_441415

I want to find out an region properties of below image

## Image Analyst (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/87597-rectangle-around-the-object-bounding-box#comment_441587

Use regionprops(). See my Image Segmentation Tutorial where I cover all that code.

Log in to comment.