Pra*_*wal 5 python opencv image-processing straight-line-detection houghlines
当给定的带有线条的图像传递给 OpenCV 的 HoughLine 变换时,它会返回一个 rho 和 theta 对的列表,每对定义一条单独的线。在这个 rho,theta 对列表中列出的行的顺序是什么。
例如,当这个带有 8 行的图像在 python 中使用时, 带有 8 行的图像
接下来,为八行返回了 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]]
Run Code Online (Sandbox Code Playgroud)
openCV 如何确定此矩阵中此处列出的行的顺序?
来自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));
Run Code Online (Sandbox Code Playgroud)
现在行列表按累加器值升序排序。并将最好的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));
Run Code Online (Sandbox Code Playgroud)
如果您不知道累加器值是多少,请阅读霍夫变换的工作原理。https://en.wikipedia.org/wiki/Hough_transform
它基本上说明了有多少像素贡献了该 rho theta 对。