0

When we have spaced lines on 1px. HoughP transform of python opencv doesn't mark all the points.

I used:

cv2.HoughLinesP(img,1,np.pi/180,400)

Theoretically it should be working fine be it dashed or non dashed. In this case it doesn't mark all the lines if they are on the same height.

HoughP Transfrom Sample Output The Green Lines indicate the white lines that were identified.

I changed the parameters to this:

cv2.HoughLinesP(img,1,np.pi/180,10,10,10)

And got this output, as you can see the detection is still missing some parts. Its unclear how, for a straight line, a shorter line is marked but not a longer line.

*** After the method suggested! After method suggested by Robert

Input Image: Input Image Here is the code:

import numpy as np
import cv2
import time

img=cv2.imread("in.PNG")

img2=np.abs(img)
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
lines = cv2.HoughLinesP(img,rho = 1,theta = 1*np.pi/180,threshold = 
10,minLineLength = 10,maxLineGap = 10)
N = lines.shape[0]
print lines
for i in range(N):
    x1 = lines[i][0][0]
    y1 = lines[i][0][1]    
    x2 = lines[i][0][2]
    y2 = lines[i][0][3]    
    cv2.line(img2,(x1,y1),(x2,y2),(0,255,0),1)
    #cv2.imshow("Window",thresh1)
    cv2.imwrite("out.PNG",img2)
Abhay
  • 19
  • 1
  • 3
  • Experiment with `maxLineGap` parameter - maximum allowed gap between line segments to treat them as single line. – Robert Davy Mar 08 '18 at 22:11
  • @RobertDavy It shouldn't matter as its picking up smaller length lines but why not longer length lines? I want them to be picked as different lines. – Abhay Mar 08 '18 at 22:21
  • Does this help? https://stackoverflow.com/questions/35609719/opencv-houghlinesp-parameters – Robert Davy Mar 09 '18 at 02:18
  • @RobertDavy Sorry, that doesn't help! I have pasted the output of that up in the description have a look. And can you please upvote the question? I am not getting any traction because of this. :/ – Abhay Mar 09 '18 at 14:36
  • It would help if you put up your complete code. A sample image could be created in code using opencv drawing functions, https://docs.opencv.org/3.3.0/dc/da5/tutorial_py_drawing_functions.html This would enable people to reproduce the exact problem and suggest changes. – Robert Davy Mar 11 '18 at 21:10
  • @RobertDavy I updated it. Please have a look. – Abhay Mar 12 '18 at 03:28
  • Not sure if this matters but you are not running hough transform on the thresholded image but the gray scale image. Consider trying higher accuracy on rho and theta (i.e. smaller values). It looks as though your input image is more than 1 pixel wide so hough may be treating them as very thin rectangles. Also consider `cv2.Sobel` to find gradients in the vertical direction. – Robert Davy Mar 13 '18 at 01:38
  • @RobertDavy Hi, nothing worked. why won't this work? Shouldn't this work as expected? Why is it so difficult to tune these parameters? – Abhay Mar 14 '18 at 04:32
  • With your input image, I get good detection of all 15 line segments using `lines = cv2.HoughLinesP(thresh1, rho=0.5, theta=0.001, threshold=50, minLineLength=50, maxLineGap=10)`. It is sensitive to tuning of parameters. As to the whys, I can only speculate. OpenCV has many ways to arrive at the same result. – Robert Davy Mar 15 '18 at 06:08

0 Answers0