Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python How to detect vertical and horizontal lines in an image with HoughLines with OpenCV?

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?

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)

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

enter image description here

like image 331
user3601754 Avatar asked Sep 28 '16 15:09

user3601754


People also ask

How do you find the vertical line in Python?

Detect lines from an image using Hough transform If it is going above the origin, instead of taking angle greater than 180, angle is taken less than 180, and rho is taken negative. Any vertical line will have 0 degree and horizontal lines will have 90 degree.

What algorithm is used to detect lines in OpenCV?

The Hough Transform is a method that is used in image processing to detect any shape, if that shape can be represented in mathematical form. It can detect the shape even if it is broken or distorted a little bit.

How do I find the lines of an image in Python?

Use the HoughLinesP() Function of OpenCV to Detect Lines in an Image in Python. The HoughLinesP() function uses probabilistic Hough line transform to detect lines. We have to read the given image using the imread() function, convert it into grayscale, and then find its edges using Canny() .

What does cv2 HoughLines return?

cv2. HoughLines returns lines in Polar coordinate system. Converting them to Cartesian coordinate system helps in calculating points on the line and point of intersection between two lines. The Cartesian line equations can be used to find points on the edge of the image that lie on the line detected by cv2.


2 Answers

You are using too small value for rho.

Try the below 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)

Note, the change in rho value, pi value and maxLineGap to reduce outliers.

Input Image Input Image

Edges Image Edges Image

Output Image Output Image

Miscellaneous - Tips for Beginners

  1. A lot of Computer Vision algorithms assume certain assumptions, well, in how the input should be. When building Proof-of-Concept, always try to view intermediate inputs you generate before applying such algorithms.

  2. For quick hack, if an algorithm accepts some parameters, use a for loop on possible values of these parameters and see how the results varies. Link to an answer on how to quickly generate these possible values.

  3. To really understand the algorithm, read on wiki or even better sources where if necessary. And then again/still do the above hack(point 2). It will further clear your understanding.

like image 153
saurabheights Avatar answered Oct 02 '22 12:10

saurabheights


I would rather write this as a comment but unfortunately I can't. You should change the minLineLength and minLineGap. Or what if its just sqaures that you have to find, I would get all the lines and check the angles between them to get lines only along squares. I have worked with HoughLineP before and it is pretty much based on the above two arguments. Additionally, try using Bilateral filtering. I really helps when the sharpening using median filter doesn't help.

Bilateral Filter

like image 44
Rick M. Avatar answered Oct 02 '22 12:10

Rick M.