Rectangle around the object, Bounding box,

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

댓글 수: 4

sukku
sukku 2017년 3월 30일
I want to find out an region properties of below image
Image Analyst
Image Analyst 2017년 3월 30일
Use regionprops(). See my Image Segmentation Tutorial where I cover all that code.
Kimo Kalip
Kimo Kalip 2018년 7월 2일
The image segmentation strategy included in that tutorial works great for well defined, solid images on a relatively consistent background. However, what do you do when you're trying to work with objects that aren't perfectly defined or shaped? Like what if you had something with various holes like a slinky or spring?
Image Analyst
Image Analyst 2018년 7월 2일
Well that's where the art of designing an image analysis algorithm comes in. You're right - it's not so straightforward and easy. That's what keeps people like me employed.

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2013년 9월 18일

8 개 추천

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

댓글 수: 9

Felix
Felix 2013년 9월 19일
It is good! thank you!
Rupali
Rupali 2013년 12월 12일
thanks a lot
Explorer
Explorer 2014년 1월 19일
편집: Explorer 2014년 1월 19일
INPUT IMAGE:
___________________________________________________________________________ MATLAB CODE: ___________________________________________________________________________ clear all; close all; clc image=imread('a_ASL3.jpg'); BW=binary_image(image); BW = ~BW; st = regionprops(BW, 'BoundingBox' ); for k = 1 : length(st) thisBB = st(k).BoundingBox; rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],... 'EdgeColor','r','LineWidth',2 ) end ___________________________________________________________________________
OUTPUT IMAGE:
___________________________________________________________________________ Problem: I want bounding box just around hand so that I can crop it and do further operations but I am getting image as output in which there is bounding box around every object.
Image Analyst
Image Analyst 2014년 1월 19일
Can you start your own thread so you'll get emails about updates instead of Felix?
Explorer
Explorer 2014년 1월 19일
Sure
syhem samti
syhem samti 2015년 11월 19일
does it work the same way as "vision.BlobAnalysis"? same results?
Torkan
Torkan 2019년 10월 16일
Hi,
How can we define the length and width of these rectangles?
DANIEL SYAFIQ
DANIEL SYAFIQ 2023년 1월 4일
THANK YOU SO MUCH IMAGE ANALYST.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Tiago Almeida
Tiago Almeida 2017년 10월 11일
편집: Image Analyst 2017년 10월 11일

2 개 추천

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

댓글 수: 4

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 )
how can we store/save the four values of bounding box for more than one images in a mat/text file calculated through following code
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
Try this:
allBB = zeros(length(measurements), 4);
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor', 'r', 'LineWidth', 2)
allBB(k, :) = thisBB;
end
Maham Khan
Maham Khan 2018년 5월 7일
Thanks @Image Analyst

댓글을 달려면 로그인하십시오.

Hafsa Asad
Hafsa Asad 2017년 1월 28일
편집: Hafsa Asad 2017년 1월 28일

0 개 추천

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 )

댓글 수: 2

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 )
Hafsa Asad
Hafsa Asad 2018년 9월 21일
Image Analyst, you are right. thanks.

댓글을 달려면 로그인하십시오.

sidraa Aleem
sidraa Aleem 2018년 4월 4일

0 개 추천

@Image Analyst I am trying to use the following code to do exactly what Felix wanted,but I am having error.
for k = 1 : length(st)
thisBB = st(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
Error using rectangle Value not a numeric scalar Please help me to resolve it.
Masar Uthaib
Masar Uthaib 2019년 12월 1일
편집: Image Analyst 2022년 4월 3일

0 개 추천

Hello
rectangle('Position',[st.BoundingBox(1),st.BoundingBox(2),st.BoundingBox(3),st.BoundingBox(4)],...
'EdgeColor','r','LineWidth',2 )
does not operate with me. Can anyone clarify to me what is wrong?

댓글 수: 1

No one, except you, did that. This should work if you have a single blob:
rectangle('Position', st.BoundingBox, 'EdgeColor','r','LineWidth',2 )
If you have multiple blobs, try this:
measurements = regionprops(mask, 'BoundingBox');
allBB = vertcat(props.BoundingBox);
hold on;
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox; % Or allBB(k, :)
rectangle('Position', thisBB, 'EdgeColor', 'r', 'LineWidth', 2);
end
hold off;

댓글을 달려면 로그인하십시오.

Muhammad Zulkifli
Muhammad Zulkifli 2023년 5월 14일

0 개 추천

How can i get bounding box ?

댓글 수: 1

It has been explained about how to use regionprops to get the bounding box of blobs in your binary image. In short:
props = regionprops(binaryImage, 'BoundingBox');
allBoundingBoxes = vertcat(props.BoundingBox)

댓글을 달려면 로그인하십시오.

태그

질문:

2013년 9월 18일

댓글:

2023년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by