6

I want to extract an elliptical region from an image (a portion of a face portion from an image) preferably in MATLAB:

enter image description here

For example, in this image, I want to extract the region within red boundary.
Can anyone help me with this ?

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が3Dの場合)。

于 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 に答える