I am having difficulty finding the lines on a chessboard in this image using HoughLinesP with OpenCV in Python.
In an attempt to understand the parameters of HoughLi
import cv2
import numpy as np
img = cv2.imread('sudoku.png', cv2.IMREAD_COLOR)
# Convert the image to gray-scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the edges in the image using canny detector
edges = cv2.Canny(gray, 50, 200)
# Detect points that form a line
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=10, maxLineGap=250)
# Draw lines on the image
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 3)
# Show result
img = cv2.resize(img, dsize=(600, 600))
cv2.imshow("Result Image", img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
Ok, I finally found the problem and thought I would share the solution for anyone else driven nuts by this. The issue is that in the HoughLinesP function, there is an extra parameter, "lines" which is redundant because the output of the function is the same:
cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])
This is causing issues 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 specify them inside the function like so:
lines = cv2.HoughLinesP(E,rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 100,maxLineGap = 50)
This totally fixed my problem and I hope it will help others.
cv2.HoughLinesP(image,rho, theta, threshold, np.array ([ ]), minLineLength=xx, maxLineGap=xx)
This will also work.
It is not HoughLinesP
issue, using that method will only get all the lines detected in the picture and return to you.
To be able to get the lines you want,you will need to smooth the image before you use the method. However if you smooth too much, there won't be any edges for HoughLinesP to detect.
You can know more about Smoothing Effects of OpenCV here.