Имкроп принимает следующий аргумент imcrop(bw,[xmin ymin width height])
.
Итак, вам нужно определить xmin
, ymin
, width
и height
%We load the bw picture
bw = im2bw(imread('im.jpg'));
%Determine the centroid
s = regionprops(bw,'centroid');
%struct to matrice
c = reshape([s.Centroid],2,length(s)).'
%crop the image: min(c) will determine xmin and ymin, and max(c)-min(c) will determine the width and height
I = imcrop(bw,[min(c) max(c)-min(c)]);
imshow(I);
![enter image description here](https://i.stack.imgur.com/MD1NZ.png)
Если ваши 5 точек не образуют прямоугольник, вы также можете создать маску, чтобы показать только интересующую область:
%returns the 2-D convex hull of the points (X,Y)
k = convhull(c(:,1),c(:,2));
%create the mask
mask = poly2mask(c(k,1),c(k,2),size(bw,1),size(bw,2));