How can I make an auto cropping command

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.

 Accepted 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);

8 Comments

No leave it. It is JUST PERFECT. THANK YOU FOR ONCE MORE.
Well I don't know. It appears that you just arbitrarily cropped the image half as (roughly) high as the original for some reason. We don't know that reason but presumably you do.
And I don't know what the user has to do with anything - the image is cropped without user intervention/participation at all.
The user needs to define the cropping window somehow, since it won't be the same for all ultrasound units. For example, it may ask for standoff thickness and then have an equation to estimate 50% of standoff thickness down and further 50% of the same dimension for soft tissue. Under this rule, no matter what image you put the cropping window size will be adjusted accordingly
So this 100 value is actually counting from 1 to 100 pixels down the image from top of the imaging window? Can you please confirm?
Well okay. If your rule is to ask the user how many pixels down from the top they want to go, then that's fine. Or you could just have them draw out a box with imrect() or rbbox().
Actually, I was thinking the rule of 50-50%. It will depend on the standoff's thickness every time. The cropping window will start at the half of standoff thickness further down the imaging window and also cover the same thickness at the soft tissue area. In this case, the cropping window is adjustable and its dimensions are known.
If it is to leave it as it is. Then, assumingly that using bb(end) = 250 pixels. Where this counting of pixels start from? I doubt if it is at the top of the imaging window since the standoff is not completely cropped. I need to know this cropping window size so I can estimate the exact pixels that are missing from counting.
So, it is not that convenient. Can you please, find me a way to acquire the pixel coordinates of this cropping window you have as it is, related to the original image???
Now that I am thinking about is that this method starts counting from top of the imaging window. If you notice on the image, at the top boundary you'll see some artifacts that I need to avoid, otherwise, they will cause me trouble on the segmentation process with the active contour.
In this case, I need the cropping window to start a bit lower NOT to include the artifacts, but later on, I need to know how many pixel rows and columns I missed, as a magnitude and add them later on, since, I care about the total imaging window dimensions and I wouldn't like to miss any rows and columns.
Am I clear?
How do I tell Matlab that I want the 50% or 70% of the cropping window as it is, starting from the bottom of the image NOT from the top. So, the remaining 50% or 30% will be deducted from the top of the cropping window.
And, then, I need to know how many pixel rows were deducted so to add them later on, as pixels or mm.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!