18

画像内の線を検出するためのハフ変換のc++実装を作成しました。見つかった行は、ウィキペディアで説明されているように、rho、thetaを使用して表されます。

「パラメータrは線と原点の間の距離を表し、θは原点からこの最も近い点までのベクトルの角度です。」

r、θを使用して記述された2本の線のx、y空間で交点を見つけるにはどうすればよいですか?

参考までに、ハフ空間に変換したりハフ空間から変換したりするための現在の関数は次のとおりです。

//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);
}

これが明らかな場合は、事前に申し訳ありません。

4

2 に答える 2

20

ウィキペディアのページを見ると、与えられたr、θのペアに対応する直線の方程式は次のようになります。

r=xcosθ+ysinθ

したがって、私が理解している場合、2つのペアr1、θ1とr2、θ2が与えられた場合、交差を見つけるには、未知数x、yについて次の線形2x2システムを解く必要があります。

xcosθ1+ysinθ1=r1
xcosθ2+ysinθ2=r2

つまり、AX = bです。ここで、

A=[cosθ1sinθ1]b= | r1 | X = | x |
    [cosθ2sinθ2]|r2 | | y |
于 2008-12-20T17:47:02.647 に答える
15

これまで行列数学に遭遇したことがなかったので、フレドリコの答えの手順を理解するために少し調査と実験を行いました. ありがとう、とにかく行列について学ぶ必要がありました。^^

パラメータ化された 2 つの線が交差する場所を見つける関数:

//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);
    }
}
于 2009-01-06T13:46:22.020 に答える