从图像中裁剪椭圆

时间:2012-06-18 09:08:15

标签: matlab image-processing computer-vision face-detection matlab-cvst

我想从图像中提取椭圆区域(图像中脸部的一部分),最好是在MATLAB中:

enter image description here

例如,在此图像中,我想提取红色边界内的区域 任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:11)

裁剪很容易,你所要做的就是使用适当的面膜。诀窍是创建这样的面具。

假设A是你的形象,试试这个:

%# Create an ellipse shaped mask
c = fix(size(A) / 2);   %# Ellipse center point (y, x)
r_sq = [76, 100] .^ 2;  %# Ellipse radii squared (y-axis, x-axis)
[X, Y] = meshgrid(1:size(A, 2), 1:size(A, 1));
ellipse_mask = (r_sq(2) * (X - c(2)) .^ 2 + ...
    r_sq(1) * (Y - c(1)) .^ 2 <= prod(r_sq));

%# Apply the mask to the image
A_cropped = bsxfun(@times, A, uint8(ellipse_mask));

裁剪后的图片将存储在A_cropped中。 使用中心坐标和半径值进行游戏,直到获得所需的结果。

编辑:我扩展了RGB图像的解决方案(如果矩阵A是3-D)。

答案 1 :(得分:2)

这是我用来将面裁成椭圆形的方法。它使背景透明。

[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);
相关问题