Merging overlapping Bounding boxes in a loop
Show older comments
Hello! I'm a beginner MATLAB user.
I am looping through a binarized image where I am drawing bounding boxes around every object. If 2 bounding boxes overlap I'd like to merge them into one big bounding box like in the 2 images below:

Currently I am looping through all the objects in this large image and drawing bounding boxes around each of them. But I can't get the merge working at all. I need help. I understand I could dilate the image so they touch, but I do not want to do this. Thanks.
hold on;
for k = 1 : length(st) % for every object
thisBB = st(k).BoundingBox;
cropBB = imcrop(bw, [thisBB(1),thisBB(2),thisBB(3),thisBB(4)]);
% merge bounding boxes if they overlap (HOW???)
%newBox = zeros(1,4)
%newBox(1)
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],'EdgeColor','r','LineWidth', 2); % draw bounding box
end
hold off;
Answers (1)
There are probably other ways to do this, but here's an example.
Let's start with a binary image and plot its bounding boxes and labels so that we can get an idea of which ones overlap.
% get a binary image of some blobs
inpict = imread('indexedblobs.png');
mask = inpict>0;
% get bounding boxes
PS = regionprops(mask,'boundingbox');
% plot things
imshow(mask,'border','tight'); hold on
for k = 1:numel(PS)
% plot bbox
thisbox = PS(k).BoundingBox;
rectangle('position',thisbox,'edgecolor','g')
% indicate each blob label
% this will help clarify the next example
x = thisbox(1) + thisbox(3)/2;
y = thisbox(2) + thisbox(4)/2;
text(x,y,sprintf('%d',k),'color','r','horizontalalignment','center', ...
'fontsize',12,'fontweight','bold')
end
Now let's try to group them and plot the group bounding boxes. I assume that for most uses, it will be desired to know which blobs are members of each group. Accordingly, I'll get a list of members for each blob group and plot those as well.
% get a binary image of some blobs
inpict = imread('indexedblobs.png');
mask = inpict>0;
% get bounding boxes and subscripts for bounding box areas
PS = regionprops(mask,'boundingbox','subarrayidx');
% create union of bounding boxes
boxmask = false(size(mask));
for k = 1:numel(PS)
boxmask(PS(k).SubarrayIdx{:}) = true;
end
% get properties of bbox mask
% this will be needed if it's necessary to know which
% blobs belong to each group
L = bwlabel(mask);
PSbox = regionprops(boxmask,L,'boundingbox','pixelvalues');
% find members of each blob group
groups = cell(numel(PSbox,1));
for k = 1:numel(PSbox)
thisgroup = unique(PSbox(k).PixelValues);
groups{k} = thisgroup(thisgroup~=0);
end
% plot things
imshow(mask,'border','tight'); hold on
for k = 1:numel(PSbox)
% plot group bbox
thisbox = PSbox(k).BoundingBox;
rectangle('position',thisbox,'edgecolor','g')
% indicate the blobs which are members of each group
x = thisbox(1) + thisbox(3)/2;
y = thisbox(2) + thisbox(4)/2;
thislabel = sprintf('%d\n',groups{k});
text(x,y,thislabel(1:end-1),'color','r','horizontalalignment','center', ...
'fontsize',12,'fontweight','bold')
end
Categories
Find more on Image Category Classification 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!
