title: Python/OpenCV - Detect lines in a tennis court using two differents methods of Hough Line in OpenCV - Get differents results
body: <p>I'm trying to detect lines in a tennis court using openCV and hough transform. I would like to find horizontal and vertical lines in order to find intersection and finally detect the corner of the tennis court.</p>
  
  <p>Here the original image.
  <img src="https://i.stack.imgur.com/UGs2N.jpg" alt="enter image description here"></p>
  
  <p>But i have some problems.</p>
  
  <p>1)I tried to use HoughLineP .
  Here the code :</p>
  
  <pre><code>gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  
  edges = cv2.Canny(gray,100,200,apertureSize = 3)
  lines = cv2.HoughLinesP(edges, 1, np.pi/2, 6, None, 50, 10);
  for line in lines[0]:
      pt1 = (line[0],line[1])
      pt2 = (line[2],line[3])
      cv2.line(img, pt1, pt2, (0,0,255), 2)
  cv2.imshow('dst',img)
  
  return res
  </code></pre>
  
  <p>Here the result :
  <a href="http://www.hostingpics.net/viewer.php?id=726975houghP.png" rel="nofollow noreferrer">Result of houghLineP</a></p>
  
  <p>2)I tried to use HoughLines
  Here the code</p>
  
  <pre><code>gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
  
  edges = cv2.Canny(gray,100,200,apertureSize = 3)
  
  
  #Lignes
  lines = cv2.HoughLines(edges,1,np.pi/70,110)
  for rho,theta in lines[0]:
      if (np.pi/70 &lt;= theta &lt;= np.pi/7) or (2.056 &lt; theta &lt; 4.970) or (1.570 &lt;= theta &lt;= 1.600): #(2,6 &lt;=theta &lt;= 26) or (theta &gt;118 and theta &lt;= 285)
  
          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(res,(x1,y1),(x2,y2),(0,0,255),1)
  </code></pre>
  
  <p>Here the result :
  <a href="http://www.hostingpics.net/viewer.php?id=282743houghline.png" rel="nofollow noreferrer">Result of houghLine</a></p>
  
  <p>In the first case, i have only little lines, and i thought about prolong them but i didn't find...I tried using fitLine but it only works with find contours (findContours method is awful in this picture)</p>
  
  <p>In the second case, it works well but i have lots of lines, almost the same and on the bottom right, i don't have any intersection in order to detect the corner...</p>
  
  <p>Maybe i'm on the wrong way...</p>
  
  <p>Do you have some ideas or something that could be works ? At the end, i would like only interest points concerning only the tennis court.</p>
  
  <p>ps : I did a method which calculate the intersection between the horizontal lines and the vertical lines.
  <img src="https://i.stack.imgur.com/nkjzi.png" alt="enter image description here"></p>
  
  <p>Many thanks, </p>