3 cv2.minEnclosingCircle
作用:使用迭代算法( iterative algorithm)查找包含2D点集的最小区域的圆(Finds a circle of the minimum area enclosing a 2D point set)。
cv2.minEnclosingCircle(points) -> center, radius
参数:
points - 2D点矢量(vector of 2D points)
返回值:
center - 圆心 (x, y)
radius - 半径 r
如:((426.0, 415.5), 321.7628173828125)
代码示例:
# calculate center and radius of minimum enclosing circle
# 会返回一个二元组,
# 第一个元素为圆心的坐标组成的元组,第二个元素为圆的半径值。
(x, y), radius = cv2.minEnclosingCircle(c)
# 转为整数 cast to integers
center =(int(x), int(y))
radius = int(radius)
# 绘圆 draw the circle
img = cv2.circle(img, center, radius, (0, 255, 0), 2)
或者
cen, rad = cv2.minEnclosingCircle(c)
cen =tuple(np.int0(cen))
rad = np.int32(rad)
img = cv2.circle(img, cen, rad, (0, 255, 0), 2)
运行
注意:
cv2.circle() 函数使用时,圆