title: Parallel Line detection using Hough Transform, OpenCV and python
body: <p>I need help on an algorithm I've been working. I'm trying to detect all the lines in a thresholded image, detect all the lines and then output only those that are parallel. The thresholded image outputs the object of my interest, and then I filter this image through a canny edge detector. This edge image is then passed through the Probabilistic Hough Transform. Now, I want the algorithm to be capable of detecting parallel lines in any image. I had in mind to do this by trying to detect the coordinates of all the lines and calculate their slope (with this then the angle). Parallel lines must have the same or almost the same angle and in that way I could output only the lines with the same angle. I could maybe draw an imaginary line in the image and then use it as reference for all the detected lines in the image? I just don't understand how to use the coordinates of all the lines detected through the function cv2.HoughLinesP(). The documentation of this functions says that the output is a 4D array and this is confusing for me. This is a part of my code:</p>
  
  <h1>Line Detection through Probabilistic Hough Transform</h1>
  
  <pre><code>rho_res = .1 # [pixels]
  
  theta_res = np.pi / 180. # [radians]
  
  threshold = 50 # [# votes]
  
  min_line_length = 100 # [pixels]
  
  max_line_gap = 40 # [pixels]
  
  lines = cv2.HoughLinesP(edge_image, rho_res, theta_res, threshold, np.array([]),
  
                          minLineLength=min_line_length, maxLineGap=max_line_gap)
  </code></pre>
  
  <h1>Draw lines</h1>
  
  <pre><code>    if lines is not None:
                  for i in range(0, len(linesP)):
                      coords = lines[i][0]
                      slope = (float(coords[3]) - coords[1]) / (float(coords[2]) - coords[0])
                      cv2.line(img, (coords[0], coords[1]), (coords[2], coords[3]), (0,0,255), 2, cv2.LINE_AA)
  </code></pre>
  
  <p>Any idea on how I could extrapolate all the detected lines and then output only those that are parallel? I have tried a few algorithms online but none seems to work. Again, my problem is understanding and working with the output variables of the function cv2.HoughLinesP(). I have also find a code that is supposed to calculate the slope. I tried this but is just giving me one value (one slope). I want the slope of all the lines in the image.</p>