regionprops Bounding box property returns values outside image dimensions

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

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.

Sign in to comment.

 Accepted Answer

OK @sarvan_keep you haven't answered either of us so I guess you must be having great difficulty. So attached is a full demo that identifies the letters, put boxes around them, and also crops out the bounding boxes into new sub images. You can then process each sub image (like do OCR on it) or save it to disk, whatever you want.
Another thing left to do is to merge bounding boxes of small blobs, like dots above the i, into the nearest other box. But I'm sure you can do that. There is a variety of ways to do that.
Please respond.

11 Comments

Thank you so much for your help. I was unwell and so wasn't able to respond to the answer. I am extremly sorry about that. And thank you for the detailed code. I do have a query on the method used to find the best threshold value. Is there any difference to the method in the comments to a method like graythresh or adaptthresh?
@sarvan_keep graythresh is a threshold determined by looking at the whole image while adapthresh finds a threshold in each little local window. For your situation you could probably use a fixed threshold, which is usually best in most situations. However graythresh will most likely give a really similar binary image for this particular image because it's almost binary already.
Got it, thank you so much. Can you please give me a clue on how I could find the nearest region as well when the region is too small?
You can get the corners of the bounding box like this
props = regionprops(binaryImage, 'BoundingBox', 'Centroid');
% Get centroids
xy = vertcat(props.Centroid);
% Get [x1, y1, x2, y2] of bounding box
bb = vertcat(props.BoundingBox);
x1 = bb(:, 1);
y1 = bb(:, 2);
xy1 = [x1, y1];
x2 = x1 + bb(:, 3);
y2 = y1 + bb(:, 4);
xy2 = [x2, y2];
% Compute distances from centroids from upper left of bounding boxes
distances = pdist2(xy, xy1);
minDistance = min(distances(distances > 0));
[rowOfMin, colOfMin] = find(distances == minDistance);
if minDistance < minAllowableDistance
% This bounding box is really close to another one so it's like the dot
% above an i. Need to combine bounding boxes (or whatever you want to do).
end
% etc.
I suspect you can take it from there.
@Image Analyst may I know what is the unit for the values returned from using regionprops boundingbox,
I get the values [498.50 867.50 660 662 ]
@Dhuwaragish Ravichandrakumar they're in the form [xLeft, yTop, width, height]. The exact values depend on the blobs found in your specific image.
@Image Analyst are these values in pixel unit or pixels square unit
This is my image with the bounding box
They're linear distances so they're in pixels. Pixels is kind of a funny unit because it's both a linear unit and a square or areal unit. A distance will have units of pixels, but an area will also have units of pixels. It's kind of like "elements". A 1-D vector can have a length of 9 elements, but a 2-D, 3-by-3 matrix will also be 9 elements. Same word even though they have different dimensions.
@Image Analyst Can I get your help with understanding on how to plot a 1D graph which shows the variations of the black and white stripes within the boundingbox
I can try. Let's start a new discussion thread so we don't hijack @sarvan_keep's thread and bombard him with unrelated emails. I'll let you post the completely new question in your own thread. If the title is something image related, I'll see it.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

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)
Thank you so much. I realised my mistake. Got my dimensions wrong. And thank you for the shortcut on cropping the images out. That really helped.
You're welcome, but please Accept-click the answer to indicate that it addressed your question.

Sign in to comment.

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!