title: Python and OpenCV - Improving my lane detection algorithm
body: <p>I'll need to detect road lane from a video. Here is my way of doing it.</p>
  
  <ol>
  <li>Determine the Region of Interest (ROI) by slicing the image (focus the middle part)</li>
  <li>Grayscale the ROI</li>
  <li>Equalized the grayscaled ROI with <code>cv2.equalizeHist</code></li>
  <li>Apply Gaussian blur to (3)</li>
  <li>Threshold (4) by using <code>cv2.adaptiveThreshold</code></li>
  <li>Skeletonize (5) by using <code>skimage.morphology.skeletonize</code></li>
  <li>Apply the <code>cv2.HoughLines</code> on (6)</li>
  </ol>
  
  <p>For the <code>cv2.HoughLines</code>, I set up so that:</p>
  
  <ol>
  <li>If <code>rho</code> is positive (which means the straight line is sloping to the right (bottom-up), it will only draw the line if it is at certain angles (I set up the range of the angle))</li>
  <li>If <code>rho</code> is negative (straight line is sloping to the left (bottom-up), it will only draw the line if it is at certain angles)</li>
  </ol>
  
  <p>This is my code for drawing the lines:</p>
  
  <pre><code>lines = cv2.HoughLines(image_bin, 1, np.pi/180, 50)
      try:
          range = lines.shape[0]
      except AttributeError:
          range = 0
  
      for i in xrange(range):
          for rho, theta in lines[i]:
              if rho &gt; 0 and (np.pi*1/10 &lt; theta &lt; np.pi*4/10):
                  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(roi, (x1, y1), (x2, y2), (0, 255, 0))
  
              if rho &lt; 0 and (np.pi*7/10 &lt; theta &lt; np.pi*9/10):
                  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(roi, (x1, y1), (x2, y2), (0, 255, 0))
  </code></pre>
  
  <p>If I didn't do what I just did above for the <code>cv2.HoughLines</code> function, I believe there will be a lot of unneeded lines drawn.</p>
  
  <p>After adjusting the parameters and such, I got a quite good result but that is for just one picture. I don't think it will be that good for a video that will keep changing. 
  What bother me the most is about my algorithm for drawing the needed lines (that is the road lane). Is there any better way? At least better than mine.</p>
  
  <p>This is my result:</p>
  
  <p>The original image:
  <a href="https://i.stack.imgur.com/MLU0T.jpg"><img src="https://i.stack.imgur.com/MLU0T.jpg" alt="The original image"></a></p>
  
  <p>Equalized histogram, thresholded, and skeletonized image of the ROI:
  <a href="https://i.stack.imgur.com/5b59h.png"><img src="https://i.stack.imgur.com/5b59h.png" alt="Equalized Histogram, thresholded, and skeletonized"></a></p>
  
  <p>Final result:
  <a href="https://i.stack.imgur.com/CdwKV.jpg"><img src="https://i.stack.imgur.com/CdwKV.jpg" alt="Final result"></a></p>