Ask Your Question

Line detectionin opencv C++

asked 2013-02-04 17:27:15 -0500

this post is marked as community wiki

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?

thanks in advance from your helps

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2013-02-04 23:40:51 -0500

this post is marked as community wiki

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;


edit flag offensive delete link more

answered 2014-05-28 13:54:54 -0500

ganninu93 gravatar image

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.

check this link for more information:

edit flag offensive delete link more

answered 2014-10-18 10:28:25 -0500

Vivek Goel gravatar image

updated 2014-10-18 10:29:31 -0500

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-02-04 17:27:15 -0500

Seen: 5,237 times

Last updated: Oct 18 '14