How to draw single bounding box by combining all bounding boxes

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

采纳的回答

KSSV
KSSV 2022-8-25
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by