title: Road Lane Detection program failing to detect lane properly
body: <p>I am trying to develop a program that can detect lanes on the road. I have experimented with both Hough Line Transform and Probabilistic Hough Line Transform. However none of these are getting the results that I want. </p>
  
  <p>Original Image:</p>
  
  <p><a href="https://i.stack.imgur.com/f38Xm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f38Xm.png" alt="enter image description here"></a></p>
  
  <p>Hough Line Transform</p>
  
  <p><a href="https://i.stack.imgur.com/j9Hm9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j9Hm9.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/C6FQV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C6FQV.jpg" alt="enter image description here"></a></p>
  
  <p>Probabilistic Hough Line Transform</p>
  
  <p><a href="https://i.stack.imgur.com/mGdA7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mGdA7.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/OlKJ5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OlKJ5.jpg" alt="enter image description here"></a></p>
  
  <p>It seems that for Hough Line Transform, I can at least detect the entire lane, but unfortunately, the line just goes on infinitely (until they move off the picture), to the point where the lines intersect with each other, which is not a good graphical lane detection marker.</p>
  
  <p>I also tried Probalistic Hough Line Transform, and the green line used for lane detection does not go off to infinitely like the other one, but it fails to mark and detect the entire lane.</p>
  
  <p>I am trying to replicate results here (by writing it in Python)</p>
  
  <p><a href="http://www.transistor.io/revisiting-lane-detection-using-opencv.html" rel="nofollow noreferrer">http://www.transistor.io/revisiting-lane-detection-using-opencv.html</a></p>
  
  <p>What can I do to fix this problem?</p>
  
  <p>Code:</p>
  
  <pre><code>import numpy as np
  import cv2
  from matplotlib import pyplot as plt
  from PIL import Image
  import imutils
  
  def invert_img(img):
      img = (255-img)
      return img
  
  def canny(imgray):
      imgray = cv2.GaussianBlur(imgray, (5,5), 200)
      canny_low = 5
      canny_high = 150
  
      thresh = cv2.Canny(imgray,canny_low,canny_high)
      return thresh
  
  def filtering(imgray):
      thresh = canny(imgray)
  
      minLineLength = 1
      maxLineGap = 1
  
  
      lines = cv2.HoughLines(thresh,1,np.pi/180,0)
      #lines = cv2.HoughLinesP(thresh,2,np.pi/180,100,minLineLength,maxLineGap)
      print lines.shape
  
      # Code for HoughLinesP
      '''
      for i in range(0,lines.shape[0]):
          for x1,y1,x2,y2 in lines[i]:
              cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
      '''
  
      # Code for HoughLines
  
      for i in range(0,5):
          for rho,theta in lines[i]:
              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)
  
  
      return thresh
  
  
  img = cv2.imread('images/road_0.bmp')
  
  
  imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  img = imutils.resize(img, height = 500)
  imgray = imutils.resize(imgray, height = 500)
  
  thresh = filtering(imgray)
  
  cv2.imshow('original', img)
  cv2.imshow('result', thresh)
  cv2.waitKey(0)
  </code></pre>