How to get multiple bounding boxes
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel Larsson
am 17 Nov. 2021
Kommentiert: Daniel Larsson
am 18 Nov. 2021
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;
0 Kommentare
Akzeptierte Antwort
Matt J
am 17 Nov. 2021
Bearbeitet: Matt J
am 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().
Weitere Antworten (2)
Image Analyst
am 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 Kommentare
Image Analyst
am 17 Nov. 2021
If you like an answer youi can click the Vote (thumbs up) icon to award the answerer "reputation points".
Image Analyst
am 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');
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!