簡體   English   中英

如何從MATLAB中4個角點指定的圖像中裁剪一部分

[英]How can I crop a portion from an image specified by 4 corner points in MATLAB

我只想裁剪此圖中用紅線封閉的部分:-

我有四個角點(x1,y1)(x2,y2)(x3,y3)(x4,y4) 我試過imcrop但這是用於矩形作物。 輸出圖像必須是RGB。

我用谷歌搜索來找到您的原始圖像文件 :-

原始文件

現在,如果我正確理解了您的問題,那么您正在尋找的是:

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')

輸出:-

輸出1



替代解決方案(如果輸出不必是RGB):-

I = rgb2gray(imread('nYNKB.jpg'));
imshow(I);
h = imfreehand; 
M = ~h.createMask();
I(M) = 0;
imshow(I);


gif_output

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM