从图像裁剪椭圆 [英] Cropping an ellipse from an image

查看:233
本文介绍了从图像裁剪椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从图像中提取椭圆区域(图像的一部分)。





例如,在此图片中,我想提取

任何人都可以帮我解决这个问题。

解决方案

裁剪很容易,必须做的是应用适当的掩码。



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

 %#创建一个椭圆形掩码
c = fix(size(A)/ 2); %#椭圆中心点(y,x)
r_sq = [76,100]。 %#椭圆半径平方(y轴,x轴)
[X,Y] = meshgrid(1:size(A,2),1:size(A,1)
ellipse_mask =(r_sq(2)*(X-c(2))。^ 2 + ...
r_sq(1)* prod(r_sq));

%#将掩码应用到图像
A_cropped = bsxfun(@times,A,uint8(ellipse_mask));裁剪后的图像将存储在 A_cropped 中。
使用中心坐标和半径值直到获得所需的结果。



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


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

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

解决方案

Cropping is easy, all you have to do is apply a proper mask. The trick is to create such a mask.

Assuming A is your image, try this:

%# 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));

The cropped image will be stored in A_cropped. Play with the coordinates of the center and the values of the radii until you get the desired result.

EDIT: I extended the solution for RGB images (if matrix A is 3-D).

这篇关于从图像裁剪椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆