MATLAB: Merge binary regions to get a bigger object

Image Processing Toolboxjoining all blobs

when i do segmentation i get bboxes such as
bboxes =
85 76 75 79
90 79 55 78
57 168 48 19
91 92 38 67
how can i merge all the regions of the bounding box to one single object…. i want all the regions in one bounding box….

Best Answer

Try this
bboxes = [...
85 76 75 79
90 79 55 78
57 168 48 19
91 92 38 67]
allx1 = bboxes(:, 1)
ally1 = bboxes(:, 2)
allx2 = allx1 + bboxes(:, 3)
ally2 = ally1 + bboxes(:, 4)
maxWidth = max(allx2) - min(allx1)
maxHeight = max(ally2) - min(ally1)
overallBBox = [min(allx1), min(ally1), maxWidth, maxHeight]
% Display every individual box.
for k = 1 : size(bboxes, 1)
rectangle('Position', bboxes(k, :), 'EdgeColor', 'k', 'LineWidth', 5);
hold on;
end
% Display the overall box.
rectangle('Position', overallBBox, 'EdgeColor', 'r', 'LineWidth', 3);