MATLAB: Reshape an object in binary image

digital image processingImage Processing Toolboxshape

Hi dear;
I have a binary image that contains an object (some times a lot of objects), I want to reshape the object as showing in the image 1 to the one as showing in the image 2. My goal is to have an object tha has a straight boundaries.
Any help and suggestion will be appreciated.

Best Answer

Use
[labeledImage, numBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'BoundingBox');
then for each blob in a for loop over all blobs, get the bounding box and get the bounding rows and columns,
for k = 1 : numBlobs
xLeft = ceil(measurements(k).BoundingBox(1));
% etc.
binaryImage(yTop:yBottom, xLeft:xRight) = true;
end
Let me know if you can't figure out the rest.