Home > Blockchain >  OpenCV HoughLines Produces Too Many Lines Python
OpenCV HoughLines Produces Too Many Lines Python

Time:01-07

I have been working on using python and computer vision to detect the board state of a gameboard in a game called enter image description here

Hough Image

I assume that the though HoughLines function just produces so many lines that it covers the screen, but I can't seem to get a normal amount of lines. I'm not sure if this bit will be useful but I have to go to extremely high values of threshold compared to any tutorial or example I can find online to avoid an almost completely red screen, but even then only like 5 lines show up. I could just not use the HoughLines function but the next step of the paper depends on this result and so I either have to solve this or find a completely different implementation of this. Any help is appreciated on this. Thanks!

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)

EDIT:

Here's dst since some people asked

dst

CodePudding user response:

With the code on enter image description here

Maybe you can start from that code...

  •  Tags:  
  • Related