两条直线在(rho/theta)参数化下的交点

19

我已经创建了一个C++实现的Hough变换来检测图像中的直线。找到的直线使用rho,theta表示,如维基百科所述:

“参数r表示直线与原点之间的距离,而θ是从原点到最近点的向量的角度”

如何通过使用r和θ描述的两条直线,在x、y空间中找到它们的交点?

以下是我当前用于在Hough空间中进行转换的功能的参考:

//get 'r' (length of a line from pole (corner, 0,0, distance from center) perpendicular to a line intersecting point x,y at a given angle) given the point and the angle (in radians)
inline float point2Hough(int x, int y, float theta) {
    return((((float)x)*cosf(theta))+((float)y)*sinf(theta));
}

//get point y for a line at angle theta with a distance from the pole of r intersecting x? bad explanation! >_<
inline float hough2Point(int x, int r, float theta) {
    float y;
    if(theta!=0) {
            y=(-cosf(theta)/sinf(theta))*x+((float)r/sinf(theta));
    } else {
            y=(float)r; //wth theta may == 0?!
    }
    return(y);
}

提前说对不起,如果这是显而易见的事情...

2个回答

20

查看维基百科页面,我发现给定r、θ对应一条直线的方程是:

r = x cosθ + y sinθ

因此,如果我理解正确,给定两个r1、θ1和r2、θ2对,要找到它们相交的点,必须解出未知数x、y的下列线性2x2系统:

x cos θ1 + y sin θ1 = r1
x cos θ2 + y sin θ2 = r2

即AX = b,其中

A = [cos θ1  sin θ1]   b = |r1|   X = |x|
    [cos θ2  sin θ2]       |r2|       |y|

小心,这个解决方案不完整。对于平行或重合的线条没有解决方案。 @eirsu提供的答案更好。 - David

16

以前从未接触过矩阵数学,在查阅和实验一番后,终于弄清了Fredrico答案的步骤。感谢他,我本来也需要学习矩阵知识。

编写一个函数来查找两个参数化直线相交的位置:

//Find point (x,y) where two parameterized lines intersect :p Returns 0 if lines are parallel 
int parametricIntersect(float r1, float t1, float r2, float t2, int *x, int *y) {
    float ct1=cosf(t1);     //matrix element a
    float st1=sinf(t1);     //b
    float ct2=cosf(t2);     //c
    float st2=sinf(t2);     //d
    float d=ct1*st2-st1*ct2;        //determinative (rearranged matrix for inverse)
    if(d!=0.0f) {   
            *x=(int)((st2*r1-st1*r2)/d);
            *y=(int)((-ct2*r1+ct1*r2)/d);
            return(1);
    } else { //lines are parallel and will NEVER intersect!
            return(0);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接