简体   繁体   中英

Create ellipse mask over image matrix

Have been trying to draw my ellipse over a 2D image with imellipse in the following code. Is there a way to create the ellipse mask without calling figure or imellipse? Removing the call for figure would cause an error, though I need to make sure that the ellipse lies in a 2D matrix of size of largestAreaI.

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

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

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

I assume you are after a binary mask to multiply to your image in the shape of an ellipse?

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

To make sure that the ellipse lies in boundary, simply do something like:

X0 + l and X0 -l must be within the max and min of your image in the X direction and similarly Y0 + w and Y0 - w must be within the max and min of your image in the Y direction.

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

结果看起来像:

Rotate causes some rough edges due to interpolation issues, trying the 'bicubic' option:

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

双三次期权

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM