Drawing a boundary box around an image?

46 ビュー (過去 30 日間)
Warid Islam
Warid Islam 2022 年 9 月 29 日
コメント済み: Warid Islam 2022 年 9 月 30 日
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 件のコメント
KSSV
KSSV 2022 年 9 月 29 日
props is a structire....you cannot use that like you have used. What exactly you want to do?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 29 日
My goal is to create a bounding box around the non-zero pixel value region of my image.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140570/box.png';
I = imread(filename);
BW = imbinarize(I);
hmask = any(BW,1);
vmask = any(BW,2);
firstrow = find(hmask, 1); lastrow = find(hmask, 1, 'last');
firstcol = find(vmask, 1); lastcol = find(vmask, 1, 'last');
imshow(I);
rectangle('Position', [firstrow, firstcol, lastrow-firstrow, lastcol-firstcol], 'EdgeColor', 'r');
What happened? Well... it turns out that your image has a white border around it. White borders are non-zero so the entire image needs to be enclosed in the border according to your stated requirements.
BW = any(I < 240, 3) & any(I > 10,3);
hmask = any(BW,1);
vmask = any(BW,2);
firstrow = find(hmask, 1); lastrow = find(hmask, 1, 'last');
firstcol = find(vmask, 1); lastcol = find(vmask, 1, 'last');
imshow(I);
rectangle('Position', [firstrow, firstcol, lastrow-firstrow, lastcol-firstcol], 'EdgeColor', 'r');
  1 件のコメント
Warid Islam
Warid Islam 2022 年 9 月 30 日
Thank you very much.

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2022 年 9 月 29 日
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 件のコメント
Warid Islam
Warid Islam 2022 年 9 月 29 日
Hi @KSSV,
Thank you for your response. I don't get any error message. However, after using imcrop, the size of my image reduces which may not be the ideal case. I am using images of the same size which I would multiply later. If the above image size decreases, then the later operations would give me an error message. My goal is to create a bounding box around the non-zero pixel value region of my image.I would perform some algorithm later on which I would like to restrict within the bounding box. I was wondering if it is possible to create a bounding box without reducing the size of the image.
Warid Islam
Warid Islam 2022 年 9 月 29 日
Hi @KSSV,
I tried to implement your code above.
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
The region inside the box is filled with Blue color. I just want the boundary to be Blue and the inside region remain visible to me. I would implement some algorithms later which would be limited inside the box.

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 9 月 29 日
You can use imclearborder to get rid of the white frame, then use regionprops on the blue channel.
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 件のコメント
Warid Islam
Warid Islam 2022 年 9 月 30 日
Thank you very much. The bounding box works.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Segmentation and Analysis についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by