How to get multiple bounding boxes

22 views (last 30 days)
I was trying to use regionprops to get bounding boxes around separated digits in an image, but the resulting bounding box ended up covering all of the digits like this:
Can this be fixed? Code can be found below, where im is the image of the digits
%Threshold image gray levels
moreThan25 = im > 25;
LEQThan25 = im <= 25;
im(moreThan25) = 255;
im(LEQThan25) = 0;
%Filter out noise
im = medfilt2(im);
boundingBoxes = regionprops(im, 'BoundingBox');
imshow(im)
hold on
for k = 1:length(boundingBoxes)
curr = boundingBoxes(k).BoundingBox;
rectangle('Position', [curr(1), curr(2), curr(3), curr(4)], 'EdgeColor', 'r');
end
pause;

Accepted Answer

Matt J
Matt J on 17 Nov 2021
Edited: Matt J on 17 Nov 2021
If that happens, it is likely that the background black pixels are not really zero. You could use imbinarize() to get a purely binary image.
The 5 digit is broken into multiple regions, however, so you will have to seal the breaks in order to get a single bounding box around that. Perhaps using imclose().
  1 Comment
Daniel Larsson
Daniel Larsson on 17 Nov 2021
imbinarize() made it work! Though I still need to close up the 5 digit as you mentioned, but I'll try that on my own for a bit (using imclose() sounds like a good start). Thanks for the help!

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 17 Nov 2021
Daniel:
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'numbers.png';
grayImage = imread(fileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel.
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(3, 3, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get a mask using locally adaptive thresholding.
mask = imbinarize(grayImage, 'adaptive');
% Get rid of blobs less than 300 pixels in area.
mask = bwareaopen(mask, 300);
% Display the image.
subplot(3, 3, 2);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask/Binary/Logical Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get the horizontal profile
horizontalProfile = any(mask, 1);
subplot(3, 3, 3);
plot(horizontalProfile, 'b-', 'Linewidth', 2);
grid on;
xlabel('Column', 'FontSize', fontSize)
% Get the bounding boxes of the profile
x1 = strfind(horizontalProfile, [0, 1])
x2 = strfind(horizontalProfile, [1, 0])
% Go through all the boxes and if they are within the profile, expand them to the profile.
numLetters = length(x1);
for k = 1 : numLetters
thisLetter = mask(:, x1(k):x2(k));
% Find overall bounding box.
[r, c] = find(thisLetter);
% Crop.
r1 = min(r);
r2 = max(r);
c1 = min(c);
c2 = max(c);
thisLetter = thisLetter(r1:r2, c1:c2);
subplot(3, 3, k+3)
imshow(thisLetter);
caption = sprintf('Letter #%d of %d', k, numLetters);
title(caption, 'FontSize',fontSize)
end
  3 Comments
Image Analyst
Image Analyst on 17 Nov 2021
If you like an answer youi can click the Vote (thumbs up) icon to award the answerer "reputation points".
Daniel Larsson
Daniel Larsson on 18 Nov 2021
Ah I see, sorry I'm new to posting on this forum, just gave you a thumbs up!

Sign in to comment.


Image Analyst
Image Analyst on 17 Nov 2021
I think this
%Threshold image gray levels
moreThan25 = im > 25;
LEQThan25 = im <= 25;
im(moreThan25) = 255;
im(LEQThan25) = 0;
%Filter out noise
im = medfilt2(im);
boundingBoxes = regionprops(im, 'BoundingBox');
should be this:
% Threshold image gray levels
moreThan25 = im > 25;
% Filter out noise
moreThan25 = bwmorph(moreThan25, 'majority'); % Or, better, use bwareafilt() or bwareaopen()
boundingBoxes = regionprops(moreThan25, 'BoundingBox');
  1 Comment
Daniel Larsson
Daniel Larsson on 17 Nov 2021
This solution also corrected the bounding boxes! Although the resulting image was not binarized (only in black and white), so this time I went with the other solution. Still though, thanks for the help!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!