Effacer les filtres
Effacer les filtres

How to draw bounding boxes around objects?

56 vues (au cours des 30 derniers jours)
Ali Alomar
Ali Alomar le 20 Mai 2022
Commenté : Image Analyst le 19 Fév 2024
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 commentaires
Matt J
Matt J le 20 Mai 2022
Well, attach Ibin in a .mat file (not any other image file type) and we can examine it.
Ali Alomar
Ali Alomar le 21 Mai 2022
well, .mat is not an image file type!! So I can't attach it as you request..

Connectez-vous pour commenter.

Réponses (1)

prabhat kumar sharma
prabhat kumar sharma le 19 Fév 2024
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 commentaire
Image Analyst
Image Analyst le 19 Fév 2024
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');

Connectez-vous pour commenter.

Catégories

En savoir plus sur Image Segmentation and Analysis dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by