Python openCV检测并行线

vto*_*mak 9 python opencv hough-transform

我有一个图像,它有一些形状.我使用霍夫线检测到了线条.如何检测哪些线是平行的?

Ann*_*ova 13

笛卡尔坐标系中的方程:

y = k*x + b

如果k1 = k2,则两条线y = k1*x + b1,y = k2*x + b2是平行的.

因此,您需要为每个检测到的线计算系数k.

为了唯一地识别线的方程,您需要知道属于线的两个点的坐标.

找到HoughLines(С++)后的行:

vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
Run Code Online (Sandbox Code Playgroud)

你有矢量线,它存储极坐标中检测到的线的参数(r,theta).您需要在笛卡尔坐标中传输它们:

这里是C++的例子:

for( size_t i = 0; i < lines.size(); i++ )
{
  float rho = lines[i][0], theta = lines[i][1];
  Point pt1, pt2;
  double a = cos(theta), b = sin(theta);
  double x0 = a*rho, y0 = b*rho;
  pt1.x = cvRound(x0 + 1000*(-b)); //the first point
  pt1.y = cvRound(y0 + 1000*(a)); //the first point
  pt2.x = cvRound(x0 - 1000*(-b)); //the second point
  pt2.y = cvRound(y0 - 1000*(a)); //the second point
}
Run Code Online (Sandbox Code Playgroud)

获得这两行后,你可以计算出它的等式.

  • 是否足够检查检测线的θ值是否平行?我们可以说θ值等于线是平行的吗? (3认同)

joh*_*jik 6

HoughLines 以极坐标返回其结果。因此只需检查角度的第二个值即可。无需转换为 x,y

def findparallel(lines):

lines1 = []
for i in range(len(lines)):
    for j in range(len(lines)):
        if (i == j):continue
        if (abs(lines[i][1] - lines[j][1]) == 0):          
             #You've found a parallel line!
             lines1.append((i,j))


return lines1
Run Code Online (Sandbox Code Playgroud)