如何获得笛卡尔坐标系中终点的坐标和线段极坐标系中的相应(rho,theta)

时间:2014-02-27 02:39:17

标签: opencv line edge-detection

我使用Canny方法获取图像的边缘。然后我应用了近似的方法来近似边缘,并得到了一组折线(不是多边形)和线段。每条折线都是由线段组成的。我的目的是获取笛卡尔坐标系(2D平面)中每个线段的终点坐标和极坐标中的相应参数(rho,theta)。任何想法?谢谢!

BTW:我知道我们可以使用HoughLines方法查找线(不是线段)并获取极坐标中的参数(rho,theta),或者我们可以使用HoughLinesP方法查找线段和最终坐标但是这两种方法不能同时得到线段终点的坐标和相应的参数(rho,theta)。

1 个答案:

答案 0 :(得分:0)

这里有一些示例C ++代码,用于解释OpenCV HoughLines中的一行。如果需要斜率,只需找到两个点并计算运行中的上升量即可。

不幸的是,重要的公式没有在文档中那么明显,它是:rho = x*cos(theta) + y*sin(theta)

float yForHoughLine(float x, const Vec2f houghLine) {
    float rho = houghLine[0];
    float theta = houghLine[1];
    if (theta == 0)
       return NAN;
    return (rho - (x * cos(theta))) / sin(theta);
}

float xForHoughLine(float y, const Vec2f houghLine) {
    float rho = houghLine[0];
    float theta = houghLine[1];
    float cosTheta = cos(theta);
    if (cosTheta == 0)
        return NAN;
    return (rho - (y * sin(theta))) / cosTheta;
}

以下是等效的Python:

def y_for_line(x, r, theta):
    if theta == 0:
        return np.nan
    return (r - (x * np.cos(theta))) / np.sin(theta)


def x_for_line(y, r, theta):
    cos_theta = np.cos(theta)
    if cos_theta == 0:
        return np.nan
    return (r - (y * np.sin(theta))) / cos_theta