我需要绘制半椭圆形(确切地说是半椭圆形磁盘,因此不能使用matplotlib绘制椭圆形弧)。
我发现OpenCV可以使用以下语法执行此操作:cv2.Ellipse(img, center, axes, angle, start_angle, end_angle, color, thickness=1, lineType=8, shift=0)
,但是有一件事确实让我感到困扰。我想使用标准的x和y坐标,而不是像素。另外,我将需要绘制半径为非整数的半椭圆,并且看来OpenCV无法做到这一点(出于同样的原因,像素坐标也是如此)。
因此,我需要一个可以执行OpenCV(具有相同angle + start_angle + end_angle
结构)但不要求以像素为单位的代码。
您可以使用matplotlib弧,如果不想填充它们。对于填充的弧,可以在此处使用定义通用补丁的解决方案,并与matplotlib椭圆示例结合使用,
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy.random as rnd
import numpy as np
def arc_patch(xy, width, height, theta1=0., theta2=180., resolution=50, **kwargs):
# generate the points
theta = np.linspace(np.radians(theta1), np.radians(theta2), resolution)
points = np.vstack((width*np.cos(theta) + xy[0],
height*np.sin(theta) + xy[1]))
# build the polygon and add it to the axes
poly = mpatches.Polygon(points.T, closed=True, **kwargs)
return poly
NUM = 10
arcs = []
for i in range(NUM):
r = rnd.rand()*360.
arcs.append(arc_patch(xy=rnd.rand(2)*10, width=1.,
height=1., theta1=r, theta2=r+180.))
# axis settings
fig, ax = plt.subplots(1,1)
for a in arcs:
ax.add_artist(a)
a.set_clip_box(ax.bbox)
a.set_alpha(0.5)
a.set_facecolor(rnd.rand(3))
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句