MATLAB Answers

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

2 views (last 30 days)
SvenvdB
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:
%%Load Image
i=imread('picture.jpg');
imshow(i);
%Image Adjust
adj=imadjust(i,stretchlim(i));
imshow(adj);
%%Convert RGB to Gray
gry=rgb2gray(adj);
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

Accepted Answer

Joseph Cheng
Joseph Cheng on 29 Jun 2021
well, you can find the corner squares by doing the following:
img = (imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/668673/image.jpeg'));
figure(1),subplot(221),imshow(img);
%Image Adjust
adj=imadjust(img,stretchlim(img));
%%Convert RGB to Gray
gry=rgb2gray(adj);
%find corner squares;
mask = gry>0;
subplot(222),imshow(mask);
mask = bwareaopen(~mask,1000);
mask = bwareafilt(mask,[5000 11000]);
subplot(223),imshow(mask);
rsum = sum(mask,2);
csum = sum(mask,1);
[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 Comments
Joseph Cheng
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

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!