# how can i automatically crop this image? if it is possible.

6 views (last 30 days)
SvenvdB on 29 Jun 2021
Commented: Walter Roberson on 29 Jun 2021
I have a script that automatically calculates de surface area of a shape. The problem is i have to manually crop a the images.
Does anyone know if this i possible to do. and tips on how to do it.
This is an example of the picture i want to crop.
i want to crop it like this:
this i my code for calculating surface area:
imshow(i);
%%Convert RGB to Gray
imshow(gry,[]);
%%Image segementation by thresholding
level=0.7;
thres=im2bw(gry,level);
imshow(thres);
%sort image
format long g
props = regionprops(~thres, 'Area');
sA = sort([props.Area])
mA = min(sA(1:end-1))
relA = sA(end)/mA
small_area = 10*10;
absA = relA * small_area
Walter Roberson on 29 Jun 2021
I knew that looked familiar ;-)

Joseph Cheng on 29 Jun 2021
well, you can find the corner squares by doing the following:
figure(1),subplot(221),imshow(img);
%%Convert RGB to Gray
%find corner squares;
[rgion] = find(rsum~=0);
[cgion] = find(csum~=0);
subplot(224),imshow(img(rgion(1):rgion(end),cgion(1):cgion(end),:))
the area in the bwareafilt was selected from your calculation of the regionprops. similarly with the region props you can find the centroid of these corner markers and perform a rotation if you need to morph the image.
##### 2 CommentsShow 1 older commentHide 1 older comment
Joseph Cheng on 29 Jun 2021
it can be whatever you want it to be. if you follow the step by step code you can see that i find the extremes/bounds of the cropped image by the defined rgion and cgion (row and column regions):
Croppedimg = img(rgion(1):rgion(end),cgion(1):cgion(end),:);
so you can save the original image in a variable by specifying the indexes of the original image

### Categories

Find more on Image Segmentation and Analysis in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!