OpenCV HoughLines 产生太多线 Python
OpenCV HoughLines Produces Too Many Lines Python
我一直致力于使用 python 和计算机视觉来检测名为 Go. Based on the data collected here, I planned to base my implementation off of this paper's algorithm(s). However, I ran into trouble when I got to section 3.1.2 in the paper and had to compute a Hough Transform on my image. I tried using OpenCV's Hough Line function 的游戏中游戏板的棋盘状态,但得到的图像充满了我无法看到的线条原图。
我为以前的功能尝试了各种线条粗细和不同的阈值,但我似乎总是以太多的线条或几乎没有线条而告终。例如,当使用顶部图像时,我得到了它下面的图像以及我粘贴在最底部的代码
我假设虽然 HoughLines 函数只是产生了太多的线条,以至于它覆盖了屏幕,但我似乎无法获得正常数量的线条。我不确定这个位是否有用,但与我可以在网上找到的任何教程或示例相比,我必须达到极高的阈值以避免几乎完全红屏,但即使这样也只显示 5 行。我可以不使用 HoughLines 函数,但论文的下一步取决于这个结果,所以我要么必须解决这个问题,要么找到一个完全不同的实现。任何帮助表示赞赏。谢谢!
import cv2
import numpy as np
img = cv2.imread(pathreal, 1)
middlex = img.shape[1]/2
middley = img.shape[0]/2
def gaussianweights(image):
newarr = [[0 for i in range(image.shape[1])] for j in range(image.shape[0])]
for i in range(image.shape[1]):
for j in range(image.shape[0]):
x,y = i,j
filtered = np.exp(((x-middlex)**2)/((middlex**2)/2)+((y-middley)**2)/((middley**2)/2))
newarr[j][i] = image[j][i]*filtered
return newarr
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
weighted_img = gaussianweights(img_gray)
filtered_img = cv2.filter2D(img_gray,-1, np.array([[1,1,1,1,1],[1,1,1,1,1],[1,1,-24,1,1],[1,1,1,1,1],[1,1,1,1,1]]))
dst = cv2.Canny(filtered_img, 600, 800, None, 3)
lines = cv2.HoughLines(dst,1,np.pi/180,100)
for line in lines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow("Hough Image",img)
cv2.waitKey(0)
编辑:
有人问了,这里是夏令时
代码在OpenCV's Hough Line function I acheive this :
也许你可以从那个代码开始...
我一直致力于使用 python 和计算机视觉来检测名为 Go. Based on the data collected here, I planned to base my implementation off of this paper's algorithm(s). However, I ran into trouble when I got to section 3.1.2 in the paper and had to compute a Hough Transform on my image. I tried using OpenCV's Hough Line function 的游戏中游戏板的棋盘状态,但得到的图像充满了我无法看到的线条原图。
我为以前的功能尝试了各种线条粗细和不同的阈值,但我似乎总是以太多的线条或几乎没有线条而告终。例如,当使用顶部图像时,我得到了它下面的图像以及我粘贴在最底部的代码
我假设虽然 HoughLines 函数只是产生了太多的线条,以至于它覆盖了屏幕,但我似乎无法获得正常数量的线条。我不确定这个位是否有用,但与我可以在网上找到的任何教程或示例相比,我必须达到极高的阈值以避免几乎完全红屏,但即使这样也只显示 5 行。我可以不使用 HoughLines 函数,但论文的下一步取决于这个结果,所以我要么必须解决这个问题,要么找到一个完全不同的实现。任何帮助表示赞赏。谢谢!
import cv2
import numpy as np
img = cv2.imread(pathreal, 1)
middlex = img.shape[1]/2
middley = img.shape[0]/2
def gaussianweights(image):
newarr = [[0 for i in range(image.shape[1])] for j in range(image.shape[0])]
for i in range(image.shape[1]):
for j in range(image.shape[0]):
x,y = i,j
filtered = np.exp(((x-middlex)**2)/((middlex**2)/2)+((y-middley)**2)/((middley**2)/2))
newarr[j][i] = image[j][i]*filtered
return newarr
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
weighted_img = gaussianweights(img_gray)
filtered_img = cv2.filter2D(img_gray,-1, np.array([[1,1,1,1,1],[1,1,1,1,1],[1,1,-24,1,1],[1,1,1,1,1],[1,1,1,1,1]]))
dst = cv2.Canny(filtered_img, 600, 800, None, 3)
lines = cv2.HoughLines(dst,1,np.pi/180,100)
for line in lines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow("Hough Image",img)
cv2.waitKey(0)
编辑:
有人问了,这里是夏令时
代码在OpenCV's Hough Line function I acheive this :
也许你可以从那个代码开始...