提取椭圆区域并在Matlab中手动裁剪

时间:2014-07-16 16:07:37

标签: matlab image-processing crop extraction matlab-cvst

我有很多脸部图像,我想通过手动从图像中提取椭圆区域并裁剪并自动保存(如imcrop但不是矩形)。 你能帮我解决这个问题吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以使用imellipse功能。

答案 1 :(得分: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);