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

24 Ansichten (letzte 30 Tage)
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)

Antworten (2)

Varun
Varun am 7 Sep. 2023
Bearbeitet: Varun am 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 am 7 Sep. 2023
Use bwconvhull and regionprops. Something like (untested):
% Read the image
image = imread('image.png').
% Convert the image to binary
binaryImage = imbinarize(image,0.7);
binaryImage = bwconvhull(binaryImage, 'union');
props = regionprops(binaryImage, 'BoundingBox');
bb = props.BoundingBox [leftColX, topRowY, width, height];
hold on;
rectangle('Position', bb, 'Color', 'r', 'LineWidth', 2);
Write back if you need more help.

Community Treasure Hunt

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

Start Hunting!

Translated by