Drawing a boundary box around an image?
Show older comments
I want to draw a boundary box around an image. I tried the code below, but it is throwing me an error. Any suggestions would be appreciated.
m=imread('bb.png')
props = regionprops(m, 'BoundingBox');
bbx = rectangle('Position',props.BoundingBox,'EdgeColor','b','LineWidth',3);
I am getting the following error message.
Error using rectangle
Input arguments must be parameter-value pairs. Each parameter name must be followed by a corresponding value.
1 Comment
KSSV
on 29 Sep 2022
props is a structire....you cannot use that like you have used. What exactly you want to do?
Accepted Answer
More Answers (2)
I = imread('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1139790/bb.png');
props = regionprops(rgb2gray(I), 'BoundingBox');
imshow(I)
hold on
for i = 1:length(props)
rectangle('Position',props(i).BoundingBox,'EdgeColor','b','LineWidth',3);
end
4 Comments
Warid Islam
on 29 Sep 2022
I = imread('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1139790/bb.png');
I = imcrop(I) ; % crop the image and select only black part
I1 = rgb2gray(I) ;
[y,x] = find(I) ;
% Bounding box
P = [min(x) min(y) ;
max(x) min(y) ;
max(x) max(y) ;
min(x) max(y) ;
min(x) min(y)] ;
imshow(I)
hold on
plot(P(:,1),P(:,2),'r')
Warid Islam
on 29 Sep 2022
Warid Islam
on 29 Sep 2022
rgbImage = imread('box.png');
subplot(2, 1, 1);
imshow(rgbImage);
impixelinfo;
title('Original Image')
% Get blue box. Use imclear border to get rid of the white outside
% padding/frame.
mask = imclearborder(rgbImage(:, :, 3) > 0);
mask = bwareafilt(mask, 1);
subplot(2, 1, 2);
imshow(mask);
title('Mask Image')
% Get the bounding box amd dos[;au ot pver the original image in red.
props = regionprops(mask, 'BoundingBox');
subplot(2, 1, 1);
hold on;
rectangle('Position', props.BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2)
1 Comment
Warid Islam
on 30 Sep 2022
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



