Draw ellipse in image

27 ビュー (過去 30 日間)
Kamu
Kamu 2017 年 1 月 1 日
回答済み: sashidhar 2022 年 11 月 16 日
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!

採用された回答

Image Analyst
Image Analyst 2017 年 1 月 2 日
  2 件のコメント
Kamu
Kamu 2017 年 1 月 2 日
編集済み: Kamu 2017 年 1 月 2 日
Thanks, your demos were useful to understand the implementation of ellipse better. However, before I try to apply ellipse I still stuck with rectangle. I would like to have a filled rectangle. With 'patch' function it did not work because of the rectangle function.
Image Analyst
Image Analyst 2017 年 1 月 2 日
What values were you using for the 'FaceColor' and 'Edgecolor' properties in rectangle(). I didn't see where you set those. What did you use?

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2017 年 1 月 2 日
clc; clear all ;
% An ellipse can be defined as the locus of all points that satisfy the equations
% x = a cos t
% y = b sin t
% where:
% x,y are the coordinates of any point on the ellipse,
% a, b are the radius on the x and y axes respectively,
t = linspace(0,2*pi) ;
a = 30 ; b = 15 ;
x = a*cos(t) ;
y = b*sin(t) ;
plot(x,y,'r')
axis equal
  3 件のコメント
Frank Uhlig
Frank Uhlig 2020 年 5 月 18 日
So, this draws an ellipse that has major axes parallel to the coordinate axes and has center at the origin ... . How about a general lay for the axes? and a general lay for the center point?
Jiawei Xu
Jiawei Xu 2021 年 6 月 8 日
Just adding to the answer to KSSV: To tilt the ellipse, you can multiply the points with a rotation matrix generated with the function:
function R = Rot2D(angle)
R = [cos(angle), -sin(angle);
sin(angle), cos(angle)];
end
such that
R = Rot2D(angle);
XY_rotated = R*[x;y];
plot(x,y,'r')
We do not know the `angle` here, but if you have the long radius and short radius available to you in vector form, such as
a = [1;5];
b = [-2,0.4];
you can find this angle by applying
angle = atan2(a(2), a(1));

サインインしてコメントする。


sashidhar
sashidhar 2022 年 11 月 16 日
clc
clear all
t=linspace(0,2*pi);
a=input('Enter the xcoordinate of the ellipse: ');
b=input('Enter the ycoordinate of the ellipse: ');
x=a*cos(t);
y=b*sin(t);
plot(x,y)
axis equal

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by