已经创建了Hough变换的c ++实现来检测图像中的线条。找到的线条用rho,theta表示,如维基百科上所述:
“参数r表示直线与原点之间的距离,而θ是从原点到该最近点的矢量角度”
如何使用r,θ?
在两个线的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);
}
如果这很明显,请提前抱歉..
答案 0 :(得分:16)
看Wikipedia page,我看到对应给定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|
答案 1 :(得分:10)
之前从未遇到过矩阵数学,因此需要进行一些研究和实验来确定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);
}
}