Python How to detect vertical and horizontal lines in an image with HoughLines with OpenCV?
<p>I m trying to obtain a threshold of the calibration chessboard. I cant detect directly the chessboard corners as there is some dust as i observe a micro chessboard.
I try several methods and HoughLinesP seems to be the easiest approach. But the results are not good, how to improve my results?</p>

<pre><code>import numpy as np
import cv2

img = cv2.imread('lines.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100)

a,b,c = lines.shape
for i in range(a):
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
    cv2.imwrite('houghlines5.jpg',img)
</code></pre>

<p>As you can see on figure below, i cant obtain my chessboard, the lines are plotted in a lot of directions... (the original picture : <a href="https://s22.postimg.org/iq2b91xq9/droite_Image_00000.jpg" rel="nofollow">https://s22.postimg.org/iq2b91xq9/droite_Image_00000.jpg</a>)</p>

<p><a href="http://i.stack.imgur.com/Qmksl.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/Qmksl.jpg" alt="enter image description here"></a></p>

<p>You are using too small value for rho.</p>

<p><strong>Try the below code:-</strong></p>

<pre><code>import numpy as np
import cv2

gray = cv2.imread('lines.jpg')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)

a,b,c = lines.shape
for i in range(a):
    cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
    cv2.imwrite('houghlines5.jpg',gray)
</code></pre>

<p>Note, the change in <strong>rho value, pi value and maxLineGap</strong> to reduce outliers.</p>

<p><strong>Input Image</strong>
<a href="http://i.stack.imgur.com/4wQjD.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/4wQjD.jpg" alt="Input Image"></a></p>

<p><strong>Edges Image</strong>
<a href="http://i.stack.imgur.com/Z1LHg.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/Z1LHg.jpg" alt="Edges Image"></a></p>

<p><strong>Output Image</strong>
<a href="http://i.stack.imgur.com/TGbc8.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/TGbc8.jpg" alt="Output Image"></a></p>

python opencv camera-calibration hough-transform