您有定义圆弧的三条信息:圆上的两个点(定义chord该圆的距离)和凸出距离(称为sagitta的圆弧)。
参见下图:
Here s是矢车菊,l是弦长的一半,并且r当然是半径。其他重要的未标记位置是弦与圆相交的点、矢状面与圆相交的点以及半径从其延伸的圆心。
对于 OpenCV 来说ellipse() https://docs.opencv.org/3.0-beta/modules/imgproc/doc/drawing_functions.html#ellipse函数,我们将使用以下原型:
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → img
其中大部分参数由下图描述:
由于我们绘制的是圆形而不是椭圆形弧,因此长轴/短轴将具有相同的尺寸,并且旋转它没有区别,因此轴将是(radius, radius)
和angle
为了简化应该为零。那么我们需要的唯一参数就是圆心、半径以及绘制的起始角度和结束角度,对应于弦的点。这些角度很容易计算(它们只是圆上的一些角度)。所以最终我们需要找到圆的半径和圆心。
求半径和圆心与求圆方程相同,因此有很多方法可以实现。但由于我们在这里编程,最简单的方法(IMO)是在圆上定义第三个点,即矢形接触圆的位置,然后从这三个点求解圆。
因此,首先我们需要获得弦的中点,获得与该中点的垂直线,并将其延伸到矢状面的长度以到达第三点,但这很容易。我将开始给出pt1 = (x1, y1)
and pt2 = (x2, y2)
作为我在圆上的两点和sagitta
是“凸出深度”(即您拥有的参数):
# extract point coordinates
x1, y1 = pt1
x2, y2 = pt2
# find normal from midpoint, follow by length sagitta
n = np.array([y2 - y1, x1 - x2])
n_dist = np.sqrt(np.sum(n**2))
if np.isclose(n_dist, 0):
# catch error here, d(pt1, pt2) ~ 0
print('Error: The distance between pt1 and pt2 is too small.')
n = n/n_dist
x3, y3 = (np.array(pt1) + np.array(pt2))/2 + sagitta * n
现在我们得到了圆上的第三个点。请注意,矢矢只有一定的长度,因此它可以向任一方向移动——如果矢矢为负,则它会从弦向一个方向移动,如果为正,则它会向另一个方向移动。不确定这是否是给你这个距离的方式。
那么我们可以简单地使用行列式求解半径和中心 https://math.stackexchange.com/a/1460096/246399.
# calculate the circle from three points
# see https://math.stackexchange.com/a/1460096/246399
A = np.array([
[x1**2 + y1**2, x1, y1, 1],
[x2**2 + y2**2, x2, y2, 1],
[x3**2 + y3**2, x3, y3, 1]])
M11 = np.linalg.det(A[:, (1, 2, 3)])
M12 = np.linalg.det(A[:, (0, 2, 3)])
M13 = np.linalg.det(A[:, (0, 1, 3)])
M14 = np.linalg.det(A[:, (0, 1, 2)])
if np.isclose(M11, 0):
# catch error here, the points are collinear (sagitta ~ 0)
print('Error: The third point is collinear.')
cx = 0.5 * M12/M11
cy = -0.5 * M13/M11
radius = np.sqrt(cx**2 + cy**2 + M14/M11)
最后,由于我们需要起始角度和结束角度来用 OpenCV 绘制椭圆,所以我们可以使用atan2()
获取从中心到初始点的角度:
# calculate angles of pt1 and pt2 from center of circle
pt1_angle = 180*np.arctan2(y1 - cy, x1 - cx)/np.pi
pt2_angle = 180*np.arctan2(y2 - cy, x2 - cx)/np.pi
所以我将这一切打包成一个函数:
def convert_arc(pt1, pt2, sagitta):
# extract point coordinates
x1, y1 = pt1
x2, y2 = pt2
# find normal from midpoint, follow by length sagitta
n = np.array([y2 - y1, x1 - x2])
n_dist = np.sqrt(np.sum(n**2))
if np.isclose(n_dist, 0):
# catch error here, d(pt1, pt2) ~ 0
print('Error: The distance between pt1 and pt2 is too small.')
n = n/n_dist
x3, y3 = (np.array(pt1) + np.array(pt2))/2 + sagitta * n
# calculate the circle from three points
# see https://math.stackexchange.com/a/1460096/246399
A = np.array([
[x1**2 + y1**2, x1, y1, 1],
[x2**2 + y2**2, x2, y2, 1],
[x3**2 + y3**2, x3, y3, 1]])
M11 = np.linalg.det(A[:, (1, 2, 3)])
M12 = np.linalg.det(A[:, (0, 2, 3)])
M13 = np.linalg.det(A[:, (0, 1, 3)])
M14 = np.linalg.det(A[:, (0, 1, 2)])
if np.isclose(M11, 0):
# catch error here, the points are collinear (sagitta ~ 0)
print('Error: The third point is collinear.')
cx = 0.5 * M12/M11
cy = -0.5 * M13/M11
radius = np.sqrt(cx**2 + cy**2 + M14/M11)
# calculate angles of pt1 and pt2 from center of circle
pt1_angle = 180*np.arctan2(y1 - cy, x1 - cx)/np.pi
pt2_angle = 180*np.arctan2(y2 - cy, x2 - cx)/np.pi
return (cx, cy), radius, pt1_angle, pt2_angle
使用这些值,您可以使用 OpenCV 绘制弧线ellipse()
功能。然而,这些都是浮点值。ellipse()
确实可以让您使用以下命令绘制浮点值shift
争论,但如果你不熟悉它,那就有点奇怪了,所以我们可以借用解决方案这个答案 https://stackoverflow.com/a/44892317/5087436定义一个函数
def draw_ellipse(
img, center, axes, angle,
startAngle, endAngle, color,
thickness=1, lineType=cv2.LINE_AA, shift=10):
# uses the shift to accurately get sub-pixel resolution for arc
# taken from https://stackoverflow.com/a/44892317/5087436
center = (
int(round(center[0] * 2**shift)),
int(round(center[1] * 2**shift))
)
axes = (
int(round(axes[0] * 2**shift)),
int(round(axes[1] * 2**shift))
)
return cv2.ellipse(
img, center, axes, angle,
startAngle, endAngle, color,
thickness, lineType, shift)
然后要使用这些功能就很简单:
img = np.zeros((500, 500), dtype=np.uint8)
pt1 = (50, 50)
pt2 = (350, 250)
sagitta = 50
center, radius, start_angle, end_angle = convert_arc(pt1, pt2, sagitta)
axes = (radius, radius)
draw_ellipse(img, center, axes, 0, start_angle, end_angle, 255)
cv2.imshow('', img)
cv2.waitKey()
再次注意,负矢矢给出了另一个方向的弧:
center, radius, start_angle, end_angle = convert_arc(pt1, pt2, sagitta)
axes = (radius, radius)
draw_ellipse(img, center, axes, 0, start_angle, end_angle, 255)
center, radius, start_angle, end_angle = convert_arc(pt1, pt2, -sagitta)
axes = (radius, radius)
draw_ellipse(img, center, axes, 0, start_angle, end_angle, 127)
cv2.imshow('', img)
cv2.waitKey()
最后,为了扩展,我在convert_arc()
功能。第一的:
if np.isclose(n_dist, 0):
# catch error here, d(pt1, pt2) ~ 0
print('Error: The distance between pt1 and pt2 is too small.')
这里的错误是因为我们需要得到一个单位向量,所以我们需要除以长度,而长度不能为零。当然,这只有在以下情况下才会发生pt1
and pt2
是相同的点,因此您只需在函数顶部检查它们是否是唯一的,而不是在此处检查。
Second:
if np.isclose(M11, 0):
# catch error here, the points are collinear (sagitta ~ 0)
print('Error: The third point is collinear.')
仅当三个点共线时才会发生这种情况,而只有当 sagitta 为 0 时才会发生这种情况。因此,您可以在函数的顶部检查这一点(也许会说,好吧,如果它是 0,则只需从pt1
to pt2
或任何你想做的事)。