How to draw bounding boxes around objects?

56 次查看(过去 30 天)
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
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');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by