HoughLinesP OpenCV options

I find it difficult to find the lines on the chessboard in this image using HoughLinesP with OpenCV in Python.

In an attempt to understand the parameters of HoughLinesP, I got the following code:

import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image I = image.imread('chess.jpg') G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) # Canny Edge Detection: Threshold1 = 150; Threshold2 = 350; FilterSize = 5 E = cv2.Canny(G, Threshold1, Threshold2, FilterSize) Rres = 1 Thetares = 1*np.pi/180 Threshold = 1 minLineLength = 1 maxLineGap = 100 lines = cv2.HoughLinesP(E,Rres,Thetares,Threshold,minLineLength,maxLineGap) N = lines.shape[0] 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(I,(x1,y1),(x2,y2),(255,0,0),2) plt.figure(),plt.imshow(I),plt.title('Hough Lines'),plt.axis('off') plt.show() 

The problem I am facing is that it raises only one line. If I reduce maxLineGap to 1, it will take thousands.

I understand why this may be, but how to choose the appropriate set of parameters to combine all these line lines? Did I miss something?

I would like to keep the code simple, as I use it as an example of this function in action.

Thanks in advance for your help!

Update: This works great with HoughLines.

And it seems that there are no problems with border detection, since Canny is working fine.

However, I still need to get HoughLinesP to work. Any ideas?

Images here: Results

+6
source share
2 answers

Well, I finally found the problem and thought that I would share a solution for someone else driven by nuts. The problem is that the HoughLinesP function has an additional parameter, โ€œstringsโ€, which is redundant, since the output of the function is the same:

cv2.HoughLinesP (image, rho, theta, threshold [, lines [, minLineLength [, maxLineGap]]])

This causes problems with the parameters as they are read in the wrong order. To avoid confusion with the order of the parameters, the simplest solution is to define them inside such a function:

 lines = cv2.HoughLinesP(E,rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 100,maxLineGap = 50) 

This completely fixed my problem and I hope this helps others.

+15
source

This is not a HoughLinesP problem, using this method will only get all the lines found in the picture and get back to you.

To get the lines you need, you need to smooth the image before using this method. However, if you smooth too much, there will be no boundaries for detecting HoughLinesP.

You can learn more about the smoothing effects of OpenCV here .

-1
source

All Articles