MATLAB Answers

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

조회 수: 4(최근 30일)
SvenvdB
SvenvdB 2021년 6월 29일
댓글: Walter Roberson 2021년 6월 29일
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

채택된 답변

Joseph Cheng
Joseph Cheng 2021년 6월 29일
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
Joseph Cheng
Joseph Cheng 2021년 6월 29일
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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by