6

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

在此处输入图像描述

例如,在这张图片中,我想提取红色边界内的区域。
谁能帮我这个 ?

4

2 回答 2

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

于 2012-06-18T11:18:21.947 回答
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);
于 2015-10-24T09:03:15.047 回答