在图像矩阵上创建椭圆蒙版

时间:2016-03-18 08:12:47

标签: matlab

我一直试图在以下代码中使用imellipse在2D图像上绘制椭圆。有没有办法在不调用figure或imellipse的情况下创建椭圆蒙版?删除对图的调用会导致错误,但我需要确保椭圆位于sizeAreaI大小的2D矩阵中。

laFig = figure();
imshow(largestAreaI);
lAEllipse = imellipse(gca, [xLA yLA hortLA hortLA]);

lAMask=lAEllipse.createMask();
close(laFig);

lAMask=imrotate(lAMask, statsLA.Orientation,'crop');

1 个答案:

答案 0 :(得分:1)

我假设您正在使用二进制蒙版以乘以椭圆形状的图像?

X0=0; %Coordinate X
Y0=0; %Coordinate Y
l=25; %Length
w=15; %Width
phi=45; %Degree you want to rotate
[X Y] = meshgrid(-50:50,-50:50); %make a meshgrid: use the size of your image instead
ellipse = ((X-X0)/l).^2+((Y-Y0)/w).^2<=1; %Your Binary Mask which you multiply to your image, but make sure you change the size of your mesh-grid
RotateEllipse = imrotate(ellipse,phi);

要确保椭圆位于边界,只需执行以下操作:

X0 + lX0 -l必须在X方向上的图片的最大值和最小值内,同样Y0 + wY0 - w必须在您的最大值和最小值范围内Y方向的图像。

 if X0-l < 1 | X0 + l > size(image,2) | Y0-w < 1 | Y0 + w > size(image,1)
 ellipse has elements outside image
 end

imagesc(RotateEllipse); 

result looks like:

由于插值问题,旋转会导致一些粗糙的边缘,尝试使用&#39; bicubic&#39;选项:

RotateEllipse = imrotate(ellipse,phi,'bicubic');

Bicubic Option