MATLAB: Draw ellipse in image

ellipse imagefaq

I would like to draw an ellipse (black-filled) in a white canvas given the center coordinates of the ellipse.
For rectangle, I did it this way (need to fill the rectangle) but for ellipse it seems to be more difficult.
width = 300;
objectWidth = 60;
canvas = ones(width, width);
figure, imshow (canvas);
square = rectangle('Position', [60-objectWidth/2, 40-objectWidth/2, objectWidth, objectWidth], ...
'EdgeColor', [0.5 0.5 0.2]);
I was thinking of the following formula for the ellipse:
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w):
plot(x,y,'y-')
axis equal
Any hints would be great. Btw. Happy New Year!

Best Answer