如何在Matlab中对这五个点进行黑白图像裁剪?

时间:2019-04-09 14:51:04

标签: matlab

我想在论文五点内裁剪图像 我已经完成了一些代码,但是没有用。

我要在这五个点内裁剪图像:

binary image with 5 points

bw=baseimage;
cc=regionprops(bw,'Centroid');
fixedPoints=cat(1,cc.Centroid);

bx1=floor(fixedPoints(1));
bx5=floor(fixedPoints(5));
by1=floor(fixedPoints(6));
by5=floor(fixedPoints(10));
base_crop=imcrop(n_im_base,[bx1 by1 bx5 by5]);
figure,imshow(base_crop);

2 个答案:

答案 0 :(得分:1)

Imcrop采用以下参数imcrop(bw,[xmin ymin width height])

因此,您需要确定xminyminwidthheight

%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

如果您的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));

答案 1 :(得分:0)

这是一种解决方案,不需要图像处理工具箱,而且可能更简单。

% Read input image
img = imread('swq1I.jpg');

% Get rid of JPG artifacts ("im2bw", "imbinarize")
img(img < 100) = 0;
img(img > 100) = 255;

figure(1);
imshow(img);

% Row and column wise summation for detection of xmin, etc. 
x = sum(img, 1);
y = sum(img, 2);

% First and last non-zero elements per row and column describe the bounding rect
xmin = find(x, 1, 'first');
xmax = find(x, 1, 'last');
ymin = find(y, 1, 'first');
ymax = find(y, 1, 'last');

% Crop by simple array indexing
crop = img(ymin:ymax, xmin:xmax);

figure(2);
imshow(crop);

结果如下:

Output