MATLAB: How to draw a BoundingBox

boundingboxedgesImage Processing Toolbox

Hello. Can someone help me please ? I'm just starting in programation. I found a code that can detect edges, and i want to draw a bounding boxes arround the detected edges.
if true
close all;clear all;clc
vid = VideoReader ('F:\TRAIT IMG\DETECTION\vol_drone.mp4');
k = vid.NumberOfFrames;
for i=1:k
J=read(vid,i);
%imshow(rgb2gray(J));
%J = imread('drone.jpg');
I = rgb2gray(J);
% imshow(I), title('image original grisé');
[~, threshold] = edge(I, 'sobel');
fudgeFactor = .5;
BWs = edge(I,'sobel', threshold * fudgeFactor);
figure, imshow(BWs), title('binary gradient mask');
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
BWdfill = imfill(BWsdil, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
BWnobord = imclearborder(BWdfill, 4);
figure, imshow(BWnobord), title('cleared border image');
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');
BWoutline = bwperim(BWfinal);
Segout = J;
Segout(BWoutline) = 255;
figure,
imshow(Segout), title('outlined original image');
end
end

Best Answer

Hello,
I'm not sure in what state are your images after image preprocessing but this is what I used in similar problem with drawing bounding boxes.
imshow(YOUR_IMAGE);
hold on;
labeledImage = logical(YOUR_BINARY_IMAGE); %not needed, image should be already in logical
measurements = regionprops(labeledImage, 'BoundingBox');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position',[thisBB(1),thisBB(2),thisBB(3),thisBB(4)],'EdgeColor','b','LineWidth',1 );
end