【问题标题】:Cropping an ellipse from an image从图像中裁剪椭圆
【发布时间】:2012-06-20 05:48:11
【问题描述】:

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

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

【问题讨论】:

  • 请详细说明、举例、展示图片等...
  • 是手动还是自动找人脸?
  • 椭圆是怎么给出的?是使用imellipse 插入的吗?你知道它的几何形状(位置+长轴和短轴)吗?您可以对生成的 imellipse 对象使用 createMask 方法。

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


【解决方案1】:

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

[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);

【讨论】:

    【解决方案2】:

    裁剪很简单,您所要做的就是戴上合适的面具。诀窍是创建这样的掩码。

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

    【讨论】:

    • 我试过你的代码。最后一行出现错误: A_cropped(ellipse_mask) = A; ???在赋值 A(:) = B 中,A 和 B 中的元素个数必须相同。
    • 您的代码现在可以工作了。但现在我只得到一个黑色背景的完全不透明的红色椭圆。
    • imshow(A) 可以显示原图吗? imagesc 对我来说很好,并且可以正确显示裁剪后的脸。
    • 请注意,如果无法重复使用蒙版(例如,因为每个图像只有一个椭圆),您可以通过不显式存储它并就地裁剪来节省一些时间和内存:A( ~(r_sq(2) * (X - c(2) 等)) = 0;
    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    相关资源
    最近更新 更多