find what is the bounding box that encircles the whole objects as one unit?

8 views (last 30 days)
Assume we have the next png imgae:
Is there a way to find what is the bounding box that encircles the whole objects as one unit? (the bounding box that includes all the letters)
(I think that the logo touches the limits of the image, assume that there is padding of zeros all around the logo)

Answers (3)

Varun
Varun on 7 Sep 2023
Edited: Varun on 7 Sep 2023
Hi David,
As I can see you have given an image which consists of a text “Disney”, and you want to enclose this text with a boundary and thus find bounding box of the image.
Here are the 5 steps which you can follow:
Step 1: Read the image and convert it to binary image.
% Read the image
image = imread('image.png').
% Convert the image to binary
binaryImage = imbinarize(image,0.7);
Step 2: Find the connected components in the binary image:
cc = bwconncomp(binaryImage);
This line uses the bwconncomp function to find the connected components in the binary image. The output cc is a structure containing information about the connected components.
Step 3: Get the bounding box of each connected component:
props = regionprops(cc, 'BoundingBox');
This line uses the regionprops function to calculate the bounding box for each connected component.
Step 4: Find the bounding box that encloses all objects:
boundingBox = [Inf, Inf, -Inf, -Inf]; % Initialize with large values
for i = 1:numel(props)
bbox = props(i).BoundingBox;
boundingBox(1) = min(boundingBox(1), bbox(1));
boundingBox(2) = min(boundingBox(2), bbox(2));
boundingBox(3) = max(boundingBox(3), bbox(1) + bbox(3));
boundingBox(4) = max(boundingBox(4), bbox(2) + bbox(4));
end
This code snippet iterates over each connected component's bounding box (props(i).BoundingBox). It updates the boundingBox variable to store the minimum x-coordinate, minimum y-coordinate, maximum x-coordinate, and maximum y-coordinate required to enclose all the objects.
Step 5: Find the bounding box that encloses all objects:
imshow(image);
hold on;
rectangle('Position', boundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
hold off;
This code displays the original image using imshow. Then, it overlays a red rectangle on the image using the rectangle function, specifying the position of the rectangle as the boundingBox. The rectangle is drawn with a red edge color and a line width of 2 pixels.
The above code generates following bounding box of the image.
Hope this helps.

Image Analyst
Image Analyst on 7 Sep 2023
Edited: Image Analyst on 19 Jul 2025
No looping and using min() and max() needed. Just use bwconvhull and regionprops. Something like (untested):
% Read the image
rgbImage = imread('Disney.png');
% Convert the image to gray scale and then binary so we can get the mask.
binaryImage = im2gray(rgbImage) < 255;
% Take the convex hull - like you were wrapping a
% rubber band around all the objects.
binaryImage = bwconvhull(binaryImage, 'union');
subplot(2, 1, 1); imshow(binaryImage); axis('on', 'image') % Display image.
% Measure the bounding box of the combined letters blob.
props = regionprops(binaryImage, 'BoundingBox');
% Get the overall bounding box from the structure.
bb = props.BoundingBox;
subplot(2, 1, 2); imshow(rgbImage); axis('on', 'image') % Display image.
% Draw the OVERALL bounding box in red over the original image.
hold on;
rectangle('Position', bb, 'EdgeColor', 'r', 'LineWidth', 2);
Write back if you need more help.

suong Tran
suong Tran on 18 Jul 2025
Edited: Walter Roberson on 18 Jul 2025
% Read the image
I = imread('image.png');
I2 = rgb2gray(I);
T = adaptthresh(I2,0.4,'ForegroundPolarity','dark');
%imshow(T)
BW = imbinarize(I2,T);
cc = bwconncomp(~BW);
props = regionprops(cc, 'BoundingBox');
bbox_1 = props(1).BoundingBox;
boundingBox = [bbox_1(1), bbox_1(2), bbox_1(3)+bbox_1(1),bbox_1(4)+bbox_1(2)]; % Initialize with large values
if(numel(props)>=2)
for i = 2:numel(props)
bbox = props(i).BoundingBox;
boundingBox(1) = min(boundingBox(1), bbox(1));
boundingBox(2) = min(boundingBox(2), bbox(2));
boundingBox(3) = max(boundingBox(3), bbox(1) + bbox(3));
boundingBox(4) = max(boundingBox(4), bbox(2) + bbox(4));
end
end
imshow(BW)
hold on;
rectangle('Position', [boundingBox(1),boundingBox(2), boundingBox(3)-boundingBox(1),boundingBox(4)-boundingBox(2)], 'EdgeColor', 'r', 'LineWidth', 2);
hold off;
  1 Comment
Walter Roberson
Walter Roberson on 18 Jul 2025
The
if(numel(props)>=2)
appears to be unnecessary to me.
Suppose numel(props) is 1. Then 2:numel(props) would be empty, so for i = 2:numel(props) would simply not loop at all, which is the same result as protecting the for loop with the if . So the if can be removed, just using the for loop.

Sign in to comment.

Categories

Find more on Convert Image Type 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!