我有一张图片,里面有一些形状。我使用霍夫线检测到了线条。如何检测哪些线是平行的?
答案 0 :(得分: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 );
你有矢量线,它存储极坐标中检测到的线的参数(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
}
获得这两行后,你可以计算出它的等式。
答案 1 :(得分:1)
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