在图像上绘制霍夫变换线
Draw hough transform lines over an image
我正在尝试自己实现 Hough 线变换,但我无法执行最后一步,即绘制高投票 thetas/rhos 值。我试图自己做数学运算,但仍然得到错误的输出。当我检查其他人的一些实现时,他们总是使用这种方法从 Polar 转换为 cartesian 坐标以找到两个点。
for r,theta in lines[0]:
# Stores the value of cos(theta) in a
a = np.cos(theta)
# Stores the value of sin(theta) in b
b = np.sin(theta)
# x0 stores the value rcos(theta)
x0 = a*r
# y0 stores the value rsin(theta)
y0 = b*r
# x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
x1 = int(x0 + 1000*(-b))
# y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
y1 = int(y0 + 1000*(a))
# x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
x2 = int(x0 - 1000*(-b))
# y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
y2 = int(y0 - 1000*(a))
# cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
# (0,0,255) denotes the colour of the line to be
#drawn. In this case, it is red.
cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2)
来自 GeeksForGeeks 的先前代码。
我没有得到的是那些方程 x1 = int(x0 + 1000*(-b))
和 y2 = int(y0 - 1000*(a))
。通过将这些等式转化为数学形式:x1 = r cos(theta) + r (-sin(theta))||
y1 = r sin(theta) + r (cos(theta))
这两个方程对我来说很陌生。 'r cos(theta)' 是正常的,因为我们从极坐标转移到笛卡尔坐标。但是,下一部分尚不清楚。谁能解释一下它背后的原始数学原理?
(r cos, r sin) 是直线上离原点最近的点。它不是线性方程。要画一条线,你需要 2 个点。在这里,他只是沿着线 1000 和 -1000 移动,得到保证在中等尺寸图像之外的两个点
有很多方法可以得到线方程。最简单的是:
r = x cos + y sin
If x1 = x - t sin
y1 = (1 / sin) (r - x cos + t sin cos)
= y + t cos
他使用 t=+/-1000 作为图像大小
我正在尝试自己实现 Hough 线变换,但我无法执行最后一步,即绘制高投票 thetas/rhos 值。我试图自己做数学运算,但仍然得到错误的输出。当我检查其他人的一些实现时,他们总是使用这种方法从 Polar 转换为 cartesian 坐标以找到两个点。
for r,theta in lines[0]:
# Stores the value of cos(theta) in a
a = np.cos(theta)
# Stores the value of sin(theta) in b
b = np.sin(theta)
# x0 stores the value rcos(theta)
x0 = a*r
# y0 stores the value rsin(theta)
y0 = b*r
# x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
x1 = int(x0 + 1000*(-b))
# y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
y1 = int(y0 + 1000*(a))
# x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
x2 = int(x0 - 1000*(-b))
# y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
y2 = int(y0 - 1000*(a))
# cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
# (0,0,255) denotes the colour of the line to be
#drawn. In this case, it is red.
cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2)
来自 GeeksForGeeks 的先前代码。
我没有得到的是那些方程 x1 = int(x0 + 1000*(-b))
和 y2 = int(y0 - 1000*(a))
。通过将这些等式转化为数学形式:x1 = r cos(theta) + r (-sin(theta))||
y1 = r sin(theta) + r (cos(theta))
这两个方程对我来说很陌生。 'r cos(theta)' 是正常的,因为我们从极坐标转移到笛卡尔坐标。但是,下一部分尚不清楚。谁能解释一下它背后的原始数学原理?
(r cos, r sin) 是直线上离原点最近的点。它不是线性方程。要画一条线,你需要 2 个点。在这里,他只是沿着线 1000 和 -1000 移动,得到保证在中等尺寸图像之外的两个点
有很多方法可以得到线方程。最简单的是: r = x cos + y sin
If x1 = x - t sin
y1 = (1 / sin) (r - x cos + t sin cos)
= y + t cos
他使用 t=+/-1000 作为图像大小