【问题标题】:In what order, OpenCV's HoughLines lists the detected lines in the [rho,theta] matrix?OpenCV 的 HoughLines 以什么顺序列出 [rho,theta] 矩阵中检测到的线?
【发布时间】:2017-12-15 06:53:56
【问题描述】:

当给定的带有线条的图像被传递到 OpenCV 的 HoughLine 变换时,它会返回一个 rho 和 theta 对的列表,每对定义一条单独的线。在这个 rho,theta 对列表中,行的顺序是什么。

例如,当这个 8 行的图像在 python 中使用时, image with 8 line

接下来的 rho,theta 矩阵为八行返回。

[[ 461.            1.48352981]
 [ 380.            1.48352981]
 [ 212.            1.48352981]
 [ 112.            1.48352981]
 [  65.            1.48352981]
 [ 334.            1.48352981]
 [ 269.            1.48352981]
 [ 508.            1.48352981]]

在此矩阵中列出的行的顺序是如何由 openCV 确定的?

【问题讨论】:

    标签: python opencv image-processing straight-line-detection houghlines


    【解决方案1】:

    来自OpenCV源代码https://github.com/opencv/opencv/blob/master/modules/imgproc/src/hough.cpp

    函数 HoughLinesStandard 从第 80 行开始实现标准霍夫变换。

    如果我们再向下滚动一点(第 166 行),我们会发现:

     // stage 3. sort the detected lines by accumulator value
        std::sort(_sort_buf.begin(), _sort_buf.end(), hough_cmp_gt(accum));
    

    现在行列表按累加器值升序排序。最好的linesMax 结果被放入输出缓冲区。

     // stage 4. store the first min(total,linesMax) lines to the output buffer
        linesMax = std::min(linesMax, (int)_sort_buf.size());
        double scale = 1./(numrho+2);
        for( i = 0; i < linesMax; i++ )
        {
            LinePolar line;
            int idx = _sort_buf[i];
            int n = cvFloor(idx*scale) - 1;
            int r = idx - (n+1)*(numrho+2) - 1;
            line.rho = (r - (numrho - 1)*0.5f) * rho;
            line.angle = static_cast<float>(min_theta) + n * theta;
            lines.push_back(Vec2f(line.rho, line.angle));
    

    如果您不知道累加器的值是什么,请阅读霍夫变换的工作原理。 https://en.wikipedia.org/wiki/Hough_transform

    它基本上说明了有多少像素促成了该 rho theta 对。

    【讨论】:

    • 我对返回的行的顺序做了一个简单的测试。它首先给出更长的行。应该是因为sort函数使用了hough_cmp_gt
    【解决方案2】:

    它们可能是按字典顺序返回的 (r, Θ) 或 (Θ, r) 顺序,因此您的平行线将通过与原点的距离增加或随机出现(角度的顺序不可预测)。

    函数的设计者没有理由强制执行特定的顺序,因为在一般情况下没有逻辑的线(平行或准平行线是一个例外)。

    如果您想要一个特定的订单,您可以指定和实施它。例如,通过增加 r 进行排序,注意在 Θ 转半圈时分配一个负号。您还可以对交叉点的纵坐标进行排序。


    在小猪找到后,他们被力量归还。我之前的段落仍然适用。

    【讨论】:

      猜你喜欢
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      相关资源
      最近更新 更多