Matlab 从图像裁剪椭圆

Matlab 从图像裁剪椭圆,matlab,image-processing,computer-vision,face-detection,matlab-cvst,Matlab,Image Processing,Computer Vision,Face Detection,Matlab Cvst,我想最好在MATLAB中从图像中提取椭圆区域(从图像中提取人脸部分的一部分): 例如,在此图像中,我想提取红色边界内的区域。 有人能帮我吗?修剪很容易,你所要做的就是涂上合适的面膜。诀窍是创造这样一个面具 假设A是您的图像,请尝试以下操作: %# Create an ellipse shaped mask c = fix(size(A) / 2); %# Ellipse center point (y, x) r_sq = [76, 100] .^ 2; %# Ellipse radii

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

例如,在此图像中,我想提取红色边界内的区域。

有人能帮我吗?

修剪很容易,你所要做的就是涂上合适的面膜。诀窍是创造这样一个面具

假设
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));
%#创建椭圆形状的遮罩
c=固定(尺寸(A)/2);%椭圆中心点(y,x)
r_sq=[76100]。^2;%椭圆半径平方(y轴,x轴)
[X,Y]=meshgrid(1:size(A,2),1:size(A,1));
椭圆掩模=(r_sq(2)*(X-c(2))^2+。。。

r_sq(1)*(Y-c(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);

请详细说明、举例、显示图像等。您想手动或自动查找面吗?椭圆是如何给出的?是使用
imellipse
插入的?您知道它的几何图形吗(位置+长轴和短轴)?您可以对生成的
imellipse
对象使用
createMask
方法。我尝试了您的代码。它在赋值A(:)中的最后一行出现错误:A_裁剪(椭圆_mask)=A=B,A和B中的元素数必须相同。您的代码现在可以工作了。但现在我得到的是一个完全不透明的红色椭圆,背景为黑色。原始图像在
imshow(A)
中显示正常吗?
imagesc
对我来说工作正常,并且正确显示了裁剪过的面。请注意,如果遮罩无法重复使用,请注意(例如,因为每个图像只有一个椭圆),您可以通过不显式存储它并在适当位置进行裁剪来节省一些时间和内存:A(~(r_sq(2)*(X-c(2)等))=0;