# Line detectionin opencv C++

This post is a wiki. Anyone with karma >50 is welcome to improve it.

Hi my friends

I am a new comer in opencv.I want to detect the lines in image...but I want to select which kind of line (vertical or Horizental)... It means when I want to detect Horizental lines , only horizental lines are shown to me.and does for Vertical lines.

Description: I am working in opencv C++ and I have used HoughLineP function.but this function shows me all the lines which are detected but I want to choose lines between horizental and vertical lines...How can I seperate these lines?

then How can I understand the angle of one of the specific lines with x axis?

edit retag close merge delete

Sort by ยป oldest newest most voted

for hough line code is -: HoughLinesP(dst,lines,1,CV_PI/180,80,30,10); now make a conditional statement like-:

if( theta>CV_PI/180*170 || theta<CV_PI/180*10)
{ Point pt1, pt2;
..........
line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}


this while restrict the scope of detected lines and then you will be able to show your horizontal lines only.

more

use HoughLines instead of HoughLinesP. This will give you the angle of the line and its perpendicular distance from the origin. This way you can simple filter lines according to their angles without any additional calculations.

more

This post is a wiki. Anyone with karma >50 is welcome to improve it.

If you know the end points of line you can easily find-out the angle of line using atan2() function in C++ and also you can decide whether the line is vertical or horizontal. Suppose you have two end points of line P1 and P2 then using the below code you can calculate the angle

    double Angle = atan2(P2.y - P1.y,P2.x - P1.x) * 180.0 / CV_PI;
if(Angle<0) Angle=Angle+360;


Regards...

more

Official site

GitHub

Wiki

Documentation