2014-07-16 84 views
0

我有很多人臉圖像,我想通過手動和裁剪從圖像中提取一個橢圓區域並自動保存(像imcrop但不是矩形)。 你能幫我解釋一下嗎?提取橢圓區域並用Matlab手動裁剪它

感謝您的幫助

+0

在最新版本的計算機視覺系統工具箱中有一個用於在圖像中標記對象的應用程序,但它只做矩形。 http://www.mathworks.com/help/vision/ug/label-images-for-classification-model-training.html – Dima

+0

可能的重複http://stackoverflow.com/questions/11079781/cropping-an-ellipse -from-an-image – beaker

+0

是的,我知道矩形的解決方案,但我需要一個橢圓。 imellipse函數給我的座標? @Dima – user951487

回答

2

可以使用imellipse功能。

2
%This is what you would do after creating the mask to get the coordinates. 

structBoundaries = bwboundaries(binaryImage); 
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates. 
x = xy(:, 2); % Columns. 
y = xy(:, 1); % Rows. 

但是,更好的方法是使用下面的代碼。實質上,它要求用戶選擇圖像,然後用戶手動裁剪圖像,然後將其保存到原始圖像的位置。

[FileName,PathName] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files'},'Please Select an Image'); 
image = imread([PathName FileName]); 
imshow(image) %needed to use imellipse 
user_defined_ellipse = imellipse(gca, []); % creates user defined ellipse object. 
wait(user_defined_ellipse);% You need to click twice to continue. 
MASK = double(user_defined_ellipse.createMask()); 
new_image_name = [PathName 'Cropped_Image_' FileName]; 
new_image_name = new_image_name(1:strfind(new_image_name,'.')-1); %removing the .jpg, .tiff, etc 
new_image_name = [new_image_name '.png']; % making the image .png so it can be transparent 
imwrite(image, new_image_name,'png','Alpha',MASK); 
msg = msgbox(['The image was written to ' new_image_name],'New Image Path'); 
waitfor(msg);