How to draw single bounding box by combining all bounding boxes

10 vues (au cours des 30 derniers jours)
Stephen john
Stephen john le 25 Août 2022
Commenté : Walter Roberson le 25 Août 2022
Hello everyone, I hope you are doing well. I have the image in which i want to draw bounding box on the object.
I have write the following code, which convert the image in binary form and draw the bounding box around white object.
But i want to draw a single bounding box. How can i draw or combine all bounding boxes in MATLAB and plot it on original Image.
Image=imread('GFSK.png')
im1=im2bw(Image);
invertedI = ~im1
[labeledImage, numRegions] = bwlabel(invertedI);
props = regionprops(labeledImage,'all' );
allLengths = [props.Area];
% NumberofPulses=allLengths;
centroids = vertcat(props.Centroid);
figure;
hold on;
for k = 1 : numRegions
y1 = round(centroids(k, 2));
y2 = y1;
xt = props(k).Centroid(1);
yt = props(k).Centroid(2);
BB = props(k).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)],'EdgeColor','b','LineWidth',1) ;
caption = sprintf('Class 1') ;
hold off
title(caption, 'FontSize',10);
end

Réponse acceptée

KSSV
KSSV le 25 Août 2022
Image=imread('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1107335/GFSK.png') ;
im1=im2bw(Image);
invertedI = ~im1 ;
[labeledImage, numRegions] = bwlabel(invertedI);
props = regionprops(labeledImage,'all' );
allLengths = [props.Area];
% NumberofPulses=allLengths;
centroids = vertcat(props.Centroid);
X = zeros(numRegions,4) ;
Y = zeros(numRegions,4) ;
figure;
hold on;
for k = 1 : numRegions
y1 = round(centroids(k, 2));
y2 = y1;
xt = props(k).Centroid(1);
yt = props(k).Centroid(2);
BB = props(k).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)],'EdgeColor','b','LineWidth',1) ;
X(k,:) = [BB(1) BB(1)+BB(3) BB(1)+BB(3) BB(1)] ;
Y(k,:) = [BB(2) BB(2) BB(2)+BB(4) BB(2)+BB(4)] ;
caption = sprintf('Class 1') ;
hold off
title(caption, 'FontSize',10);
end
x0 = min(X(:)) ; x1 = max(X(:)) ;
y0 = min(Y(:)) ; y1 = max(Y(:)) ;
L = abs(x1-x0) ;
B = abs(y1-y0) ;
rectangle('Position', [x0 y0 L B],'EdgeColor','r','LineWidth',1) ;
  6 commentaires
Stephen john
Stephen john le 25 Août 2022
@KSSV i am unable to do that , thats why i asked
Walter Roberson
Walter Roberson le 25 Août 2022
text(x0, y0, 'class1')

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by