[英]How can I crop a portion from an image specified by 4 corner points in MATLAB
我用谷歌搜索來找到您的原始圖像文件 :-
現在,如果我正確理解了您的問題,那么您正在尋找的是:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.