简体   繁体   English

如何在图像上绘制椭圆?

[英]How to draw ellipse on an image?

I am using this function to draw a circle.我正在使用这个 function 来画一个圆。 How can I modify to draw ellipse?如何修改以绘制椭圆?

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

A simple way is to stretch x-axis and y-axis by a different factor, ie to replace radius by x_semiaxis and y_semiaxis , to mean the semi-axes of the ellipse :一种简单的方法是通过不同的因子拉伸 x 轴和 y 轴,即将radius替换为x_semiaxisy_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])

Note that I use the more traditional formula to have x by the cosine, and y by the sine, and I avoid including the transposition, just to make plotting easier.请注意,我使用更传统的公式让x为余弦, y为正弦,并且我避免包括转置,只是为了使绘图更容易。 You can easily bring it back.你可以很容易地把它带回来。

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.

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