regionprops Bounding box property returns values outside image dimensions
Show older comments
Hi,
I am using Matlab R2022a. I am trying to draw bounding boxes to an image considering English alphabets. I am extremely new to Matlab so I apologize if my question or technique is wrong.

I considered using thresholding after binarizing the image and then using connected components to find the bounding boxes. Here is my code:
image = imread(image_path);
image = im2double(imcomplement(image));
image1 = imbinarize(image_comp);
connected = bwconncomp(image1);
imshow(image1);
props = regionprops(connected, 'BoundingBox');
for i = 1:connected.NumObjects
if(props(i).BoundingBox(1) > connected.ImageSize(1))
rectangle('Position', props(i).BoundingBox,'EdgeColor', [0, 1, 0], 'LineWidth', 2)
continue;
end
rectangle('Position', props(i).BoundingBox,'EdgeColor', [1, 0, 0], 'LineWidth', 2)
end
I noticed that some of the x values in the bounding boxes returned were outside the dimensions of the actual image (drawn in green colour). So I am unable to crop these regions of the image.
Here is the output I receive:

Please advice on what I am doing wrong.
1 Comment
Image Analyst
on 21 Sep 2022
Not sure what you mean. All boxes, red or green, completely contain one blob. None of the bounding box x values of the left edge should be outside the image (below a value of 1).
props = regionprops(connected, 'BoundingBox');
boundingBoxes = vertcat(props.BoundingBox); % List of [xLeft, yTop, width, height] values. Each row is a different blob.
Accepted Answer
More Answers (1)
You should be comparing BoundingBox(1) with ImageSize(2)
image = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132160/image.jpeg');
image_comp = im2gray(imcomplement(image));
image1 = imbinarize(image_comp);
connected = bwconncomp(image1);
imshow(image1);
props = regionprops(connected, 'BoundingBox');
for i = 1:connected.NumObjects
if(props(i).BoundingBox(1) > connected.ImageSize(2))
rectangle('Position', props(i).BoundingBox,'EdgeColor', [0, 1, 0], 'LineWidth', 2)
continue;
end
rectangle('Position', props(i).BoundingBox,'EdgeColor', [1, 0, 0], 'LineWidth', 2)
end
3 Comments
So I am unable to crop these regions of the image.
If your goal is just to extract subimages enclosed by the BoundingBoxes, it is far simpler as below,
image = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132160/image.jpeg');
image_comp = im2gray(imcomplement(image));
BW = logical(imbinarize(image_comp));
props=regionprops(BW, 'Image');
imshow(props(33).Image)
sarvan_keep
on 23 Sep 2022
Matt J
on 23 Sep 2022
You're welcome, but please Accept-click the answer to indicate that it addressed your question.
Categories
Find more on Image Segmentation 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!



