一、基于多段线绘制近似椭圆
- 如果需要对椭圆渲染进行更多控制,或者绘制粗略的椭圆边界,可以使用 cv.ellipse2Poly 检索曲线,然后使用多段线进行渲染或使用fillPoly进行填充。
cv.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta[,]) → pts
参数说明:
- img:输入输出图像,允许单通道灰度图像或多通道彩色图像
- center:椭圆中心点的坐标,(x, y) 格式的元组
- axes:椭圆半轴长度,(hfirst, hsecond) 格式的元组-
- angle: 椭圆沿 x轴方向的旋转角度(角度制,顺时针方向)
- arcStart:椭圆弧的起始角度(角度制)
- endAngle:椭圆弧的终止角度(角度制)
- delta:与下一线段之间的角度,用于控制近似精度,角度越小越接近椭圆
- pts:输出向量,逼近椭圆弧的多段线顶点坐标的数组
二、例程
- A4.6 基于多段线绘制近似椭圆
import cv2
import numpy as np
from matplotlib import pyplot as plt
# A4.6 基于多段线绘制近似椭圆
img = np.ones((400, 600, 3), np.uint8)*224
cx, cy = 100, 150
halfAxesLength = (70, 40)
angle, startAng, endAng = 30, 0, 360
delta = [10, 20, 30, 40]
for i in range(len(delta)):
color = (i*60, i*60, 255-i*60)
pts = cv2.ellipse2Poly((cx+140*i, cy), halfAxesLength, angle, startAng, endAng, delta[i]) # (351,2)
points = np.array(pts)
cv2.polylines(img, [points], True, color, thickness=1) # 绘制近似多边形
points[:,1] += 160
cv2.fillPoly(img, [points], color) # 绘制填充近似多边形
text1 = "delta={}".format(delta[i])
text2 = "num={}".format(pts.shape)
cv2.putText(img, text1, (140*i+25, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)
cv2.putText(img, text2, (140*i+25, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)
print(pts.shape, points.shape)
plt.figure(figsize=(9, 6))
plt.title("Polygon approximated ellipse"), plt.axis('off')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

三、资料
youcans_的博客:
https://blog.csdn.net/youcans/article/details/125468099