Python 在图像上绘制hough变换线
我试图自己实现Hough线变换,但我无法完成最后一步,即绘制高票θ/rhos值。我试着自己做数学运算,但仍然得到错误的输出。当我检查其他人的一些实现时,他们总是使用这种方法从极坐标转换到笛卡尔坐标以找到两点Python 在图像上绘制hough变换线,python,computer-vision,hough-transform,Python,Computer Vision,Hough Transform,我试图自己实现Hough线变换,但我无法完成最后一步,即绘制高票θ/rhos值。我试着自己做数学运算,但仍然得到错误的输出。当我检查其他人的一些实现时,他们总是使用这种方法从极坐标转换到笛卡尔坐标以找到两点 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) #
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)
以前的代码来自。
我没有得到的是那些方程x1=int(x0+1000*(-b))
&y2=int(y0-1000*(a))
。通过将这些方程转换为数学形式:x1=r cos(θ)+r(-sin(θ))||
y1=r sin(θ)+r(cos(θ))
这两个方程式对我来说太奇怪了。”当我们从极坐标变换到笛卡尔坐标时,r cos(θ)是正常的。然而,下一部分还不清楚。有人能解释它背后的原始数学吗?(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表示图像大小我不确定你的数学,但你为什么要把所有的东西都乘以1000?不。。不,我已经用另一个等式实现了。我只是想了解上面的实现,因为它几乎已经被hough行实现所使用[就像我上面提到的链接一样]。另外,我按原样编写了这段代码,然后它也工作了。这就是为什么我对它的数学如此困惑。嗯,我在问这两个方程的驱动力!!X1=x-t sin!!y也是一样,它是一个直线方程。U用x+t代替x并计算y。变化的t在直线上给出不同的点。为了方便起见,这里使用的t是-t sin。但是你不能用同样的方法驱动这两个点,x0,x1,x2,y0,y1,y2,这里是常数。唯一的随机变量是tI仍然没有得到它!!!是的,我知道使用t=+/-1000来确保在图像上绘制线。然而,为什么我们用x+t来代替x!!!我只想了解它背后的数学,仅此而已。从逻辑上讲,这在数学上有点奇怪,因为直线方程'r=xcos+ysin'和
x1=x0+rsin
|y1=y0+rcos
没有证明(驱动力)。我的意思是术语rsin
,如果我们在图上观察它,我们只需在x0点的值上添加一部分y坐标,反之亦然,在y0点上。这就是为什么我不能理解这部分。这里的罪与你无关。让我们从直线方程开始:r=xcos+ysin。这里,u可以用一个值代替x,用y代替直线上的一个点。对吗?让我们用(r cos-t sin)代替x。我们得到y=(r-r cos2+t sincos)/sin=(r sin2+r cos2-r cos2+t sincos)/sin=r sin+t sincos。对吗?