title: Python cv2 HoughLines grid line detection
body: <p>I have a simple grid in an image, I am trying to determine the grid size, e.g. 6x6, 12x12, etc. Using Python and cv2.</p>
  
  <p><img src="https://i.stack.imgur.com/1GClp.png" alt="enter image description here"></p>
  
  <p>I am testing it with the above 3x3 grid, I was planning to determine the grid size by counting how many vertical / horizontal lines there are by detecting them in the image:</p>
  
  <pre><code>import cv2
  import numpy as np
  
  im = cv2.imread('photo2.JPG')
  gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  
  imgSplit = cv2.split(im)
  flag,b = cv2.threshold(imgSplit[2],0,255,cv2.THRESH_OTSU) 
  
  element = cv2.getStructuringElement(cv2.MORPH_CROSS,(1,1))
  cv2.erode(b,element)
  
  edges = cv2.Canny(b,150,200,3,5)
  
  while(True):
  
      img = im.copy()
  
      lines = cv2.HoughLinesP(edges,1,np.pi/2,2, minLineLength = 620, maxLineGap = 100)[0]
  
      for x1,y1,x2,y2 in lines:        
          cv2.line(img,(x1,y1),(x2,y2),(0,255,0),1)
  
      cv2.imshow('houghlines',img)
  
      if k == 27:
          break
  
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>My code detects the lines, as can be seen below, however there are multiple lines detected for each line in my image:</p>
  
  <p><img src="https://i.stack.imgur.com/pMD7u.png" alt="enter image description here"></p>
  
  <p>(there are two 1px green lines drawn for every line in the image)</p>
  
  <p>I cannot simply divide the number of lines by two because (depending on the grid size) sometimes just the one line will be drawn.</p>
  
  <p>How can I more accurately detect and draw a single line for every line detected in the original image?</p>
  
  <p>I have tweaked threshold settings, reducing the image to black and white, yet I still get multiple lines. I assume this is because of the canny edge detection?</p>