[英]How to draw ellipse on an image?
我正在使用这个 function 来画一个圆。 如何修改以绘制椭圆?
def circle_points(resolution, center, radius):
"""
Generate points which define a circle on an image.Centre refers to the centre of the circle
"""
radians = np.linspace(0, 2*np.pi, resolution)
c = center[1] + radius*np.cos(radians)#polar co-ordinates
r = center[0] + radius*np.sin(radians)
return np.array([c, r]).T
一种简单的方法是通过不同的因子拉伸 x 轴和 y 轴,即将radius
替换为x_semiaxis
和y_semiaxis
,表示椭圆的半轴:
def ellipse_points(resolution, center, x_semiaxis, y_semiaxis):
"""
Generate points which define a ellipse on an image.
Centre refers to the centre of the ellipse.
"""
radians = np.linspace(0, 2 * np.pi, resolution)
x = center[0] + x_semiaxis * np.cos(radians)
y = center[1] + y_semiaxis * np.sin(radians)
return np.array([x, y])
请注意,我使用更传统的公式让x
为余弦, y
为正弦,并且我避免包括转置,只是为了使绘图更容易。 你可以很容易地把它带回来。
import matplotlib.pyplot as plt
plt.scatter(*ellipse_points(1000, (10, 20), 200, 100))
plt.scatter((10,), (20,)) # plot the center too
plt.gca().set_aspect('equal')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.