答案 0 :(得分:3)
我使用谷歌搜索找到您的original image file: -
现在,如果我理解你的问题,这就是你要找的东西:
I=imread('nYNKB.jpg');
Isize = size(I);
mask = poly2mask([43 214 227 123],[131 22 112 198],Isize(1,1),Isize(1,2));
% where [x1,x2,x3,x4] = [43 214 227 123] & [y1,y2,y3,y4] = [131 22 112 198]
I_masked = bsxfun(@times,I,cast(mask,class(I)));
subplot(1,2,1)
imshow(I);
title('Original Image')
subplot(1,2,2)
imshow(I_masked)
title('Masked Image')
如果您不想要黑色背景,那么您最接近的就是:
figure(2)
mask = bsxfun(@eq,I_masked,reshape([0 0 0],1,1,3));
image(I_masked,'alphadata',1-double(all(mask,3)));
axis off
title('Masked Image')
<强>输出: - 强>
替代解决方案(如果输出不必是RGB): -
I = rgb2gray(imread('nYNKB.jpg'));
imshow(I);
h = imfreehand;
M = ~h.createMask();
I(M) = 0;
imshow(I);