how can i automatically crop this image? if it is possible.
2 次查看(过去 30 天)
显示 更早的评论
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
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read, Write, and Modify Image 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!