How to draw bounding boxes around objects?

조회 수: 34 (최근 30일)
Ali Alomar
Ali Alomar 2022년 5월 20일
댓글: Image Analyst 2024년 2월 19일
Hello..
I'm trying to get a bounding box around the objects in an image:
[bwlabel,num]=bwlabel(Ibin);
num;
s=regionprops(FeatureMap,'BoundingBox');
imshow(FeatureMap),title('Image with bounding box')
hold on
for i=1:length(s)
currbb=s(i).BoundingBox;
rectangle('position',[currbb(1),currbb(2),currbb(3),currbb(4)],'EdgeColor','g')
end
hold off
but the result is one rectangle like this:
can someone help...
  댓글 수: 2
Matt J
Matt J 2022년 5월 20일
Well, attach Ibin in a .mat file (not any other image file type) and we can examine it.
Ali Alomar
Ali Alomar 2022년 5월 21일
well, .mat is not an image file type!! So I can't attach it as you request..

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

답변 (1개)

prabhat kumar sharma
prabhat kumar sharma 2024년 2월 19일
Hi Ali,
I understand that you are trying to identify and draw bounding boxes around connected components in a binary image. You've mentioned using bwlabel and regionprops, but there seems to be a discrepancy in how they are being used in conjunction with the variable FeatureMap.
To clarify, bwlabel is a MATLAB function that labels connected components in a binary image, and regionprops computes properties of these labeled regions, including their bounding boxes. It's important to use the labeled image output from bwlabel as the input to regionprops.
Here's the steps with the corrected code to help you with your task:
  1. Read your RGB image into MATLAB.
  2. Convert the RGB image to grayscale.
  3. Threshold the grayscale image to create a binary image.
  4. Use bwlabel to label the connected components in the binary image.
  5. Use regionprops to get the bounding boxes of the labeled regions.
  6. Display the binary image and draw the bounding boxes.
% Read the RGB image
rgbImage = imread('C:\Users\prabhats\Downloads\MATLAB CODES\image.png'); % Replace with your image path
% Convert the RGB image to grayscale
grayImage = rgb2gray(rgbImage);
% Choose a threshold value
thresholdValue = 128; % This is an example value; adjust it as needed
% Convert the grayscale image to a binary image using the threshold
Ibin = imbinarize(grayImage, thresholdValue/255);
[labeledImage, num] = bwlabel(Ibin);
s = regionprops(labeledImage, 'BoundingBox');
imshow(Ibin), title('Image with bounding box'); % Display the binary image
hold on;
% Loop through each region and draw the bounding box
for i = 1:length(s)
currbb = s(i).BoundingBox;
rectangle('Position', [currbb(1), currbb(2), currbb(3), currbb(4)], 'EdgeColor', 'g');
end
hold off;
Output Image:
I hope it helps to resolve your issue!
  댓글 수: 1
Image Analyst
Image Analyst 2024년 2월 19일
It's not important to pass in the labeled image to regionprops. If you do you'll get a warning. It's fine to just pass in the binary image directly into regionprops.
s=regionprops(Ibin,'BoundingBox');

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by