Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV line detection in general

I am new to opencv (in c++) and I am trying to implement line detection.

I have a picture with a couple of lines and I am trying to determine the distance between the lines. I know there is Hough, Canny and so on but how can I get the coordinates of the different lines to calculate the distance between the lines? Should I use opencv contour functions or are there better ways? I do not need complete code samples, but can anyone tell me the best way to get the job done?

like image 736
marc Avatar asked Nov 22 '12 15:11

marc


People also ask

What algorithm is used to detect lines in OpenCV?

Hough transform is a feature extraction method to detect any simple shape, if you can represent that shape in mathematical form. It somehow manage to detect the shape even if it is broken or distorted a little bit. We will see how it works for a line.

How is a line detected in image processing?

In image processing, line detection is an algorithm that takes a collection of n edge points and finds all the lines on which these edge points lie. The most popular line detectors are the Hough transform and convolution-based techniques.

Which method is used for line detection?

The usual method for line detection is the Hough transform [Hough 1962]. Like the Fourier transform, it transposes the image from the spatial domain to another domain, where the information of interest is represented differently.


1 Answers

Line detections lead often to using Hough transform, Canny edge detector and contour detection only act as convenient pre-processors if needed.

If you have parallel lines, use

void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )

for detecting lines where the second parameter will contain the detection:

lines – Output vector of lines. Each line is represented by a two-element vector (ρ, θ) . ρ is the distance from the coordinate origin (0, 0) (top-left corner of the image). θ is the line rotation angle in radians ( 0 ∼ vertical line, π/2 ∼ horizontal line ).
[opencv2refman.pdf]

This means, that the distance between two lines should be abs(rho1-rho2), that the distances are absolute differences between pixel values in the first column of lines. (Note: method should be CV_HOUGH_STANDARD here!)

For non-parallel lines you have to define what you think of as a distance, but then OpenCV may still provide you with the coordinates of endpoints of each detected line.
You just have to use method = CV_HOUGH_PROBABILISTIC.

CV_HOUGH_PROBABILISTIC probabilistic Hough transform (more efficient in case if the picture contains a few long linear segments). It returns line segments rather than the whole line. Each segment is represented by starting and ending points, and the matrix must be (the created sequence will be) of the CV_32SC4 type.
[opencv2refman.pdf]

You can also find a tutorial in opencv_tutorials.pdf within the documentation of your installed OpenCV.

like image 172
Barney Szabolcs Avatar answered Sep 21 '22 14:09

Barney Szabolcs