MATLAB: How to make an auto cropping command

automated croppingimage processingImage Processing Toolbox

Please, see the two images I attach.
They are actually the same images, but the one is cropped at the middle of the imaging window. I defined a cropping window and just cropped it.
How can I have the same procedure in an automated way?
So Matlab needs to identify the imaging window and assume we put some values to automatically create a cropping window of e.g. 50% of the standoff's thickness. and another 50% of soft tissue. This can also be adjusted by the user and be able to chose his own values.
Looking forward to your valuable help.
God bless you.

Best Answer

I thought I already gave you code for this. Basically you threshold it, then call bwareafilt() to extract the largest blob. Then call regionprops() to get the bounding box. Then reassign the last element of the bounding box to be whatever height you want, then call imcrop(). Try it. Here's a start
binaryImage = bwareafilt(grayImage > 0, 1);
props = regionprops(binaryImage, 'BoundingBox');
bb = props.BoundingBox;
bb(end) = 100; % Whatever height you want.
croppedImage = imcrop(grayImage, bb);
imshow(croppedImage);