如何使用OpenCV确定HoughLines函数找到的线的角度?

use*_*414 4 opencv computer-vision hough-transform

在OpenCV中使用HoughLines函数,是否可以确定结果线相对于图像底部的角度?

Y.A*_*.AL 12

如果使用HoughLines函数,它将为您提供已由两个参数定义的行:theta和rho,as

vector<Vec2f> lines;
// detect lines
HoughLines(image, lines, 1, CV_PI/180, 150, 0, 0 );

// get lines
for( size_t i = 0; i < lines.size(); i++ )
{
    float rho = lines[i][0], theta = lines[i][1];
   ....
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您应用HoughLinesP函数,您将获得由两个点定义的线,您只需要计算两个点之间关于图像的线的角度,如下所示:

vector<Vec4i> lines;
// detect the lines
HoughLinesP(image, lines, 1, CV_PI/180, 50, 50, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
    Vec4i l = lines[i];
    // draw the lines

    Point p1, p2;
    p1=Point(l[0], l[1]);
    p2=Point(l[2], l[3]);
    //calculate angle in radian,  if you need it in degrees just do angle * 180 / PI
    float angle = atan2(p1.y - p2.y, p1.x - p2.x);
  .......
}
Run Code Online (Sandbox Code Playgroud)