MATLAB: How i can put a rectangular around forground

image processingImage Processing Toolbox

in regionprob, bounding box put a rectangular a round each object but i need just one rectangular around my forground which is in the center of my image .i mean i need a perim around my object that have rectangular form.

Best Answer

Call bwconvhull() before you call bwlabel() or bwconncomp(). That will join the two blobs and create just one giant blob, which, of course, will have just one bounding box.
binaryImage = bwconvhull(binaryImage);
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'BoundingBox');
boundingBox = measurements.BoundingBox;