我一直在使用 Matlab 和 OpenCV/labview 的应用程序中使用霍夫变换,发现对于某些图像,霍夫变换给出了明显错误的线拟合(一致)
这是测试和叠加图像。角度似乎正确,但 rho 不对。
在下图中,您会看到顶部图像试图在原始图像的左侧拟合一条线,底部图像在图像的右侧拟合一条线。
在Matlab中,我通过调用霍夫函数
[H1D,theta1D,rho1D] = hough(img_1D_dilate,'ThetaResolution',0.2);
在 C++ 中,我修剪了 OpenCV HoughLines 函数,所以我最终只得到了我们填充累加器的部分。请注意,因为我的 theta 分辨率是 0.2,所以我有 900 个角度要分析。 tabSin 和 tabCos 在函数之前定义,因此它们只是角度的 sin 和 cos。
请注意,这些例程通常运行良好,但仅针对特定情况,它会按照我展示的方式执行。
double start_angle = 60.0;
double end_angle = 120.0;
double num_theta = 180;
int start_ang = num_theta * start_angle/180;
int end_ang = num_theta * end_angle/180;
int i,j,n,index;
for (i = 0;i<numrows;i++)
{
for (j = 0;j<numcols;j++)
{
if (img[i*numcols + j] == 100)
{
for (n = 0;n<180;n++)
{
index = cvRound((j*tabCos[n] + i * tabSin[n])) + (numrho-1)/2;
accum[(n+1) * (numrho+2) + index+1]++;
}
}
}
}
TabCos 和 tabSin 在 Labview 中用这段代码定义 int32我; float64 theta_prec; float64 tabSin[180]; float64 tabCos[180];
theta_prec = 1/180*3.14159; 对于 (i = 0;i<180;i++) { tabSin[i] = sin(itheta_prec); tabCos[i] = cos(itheta_prec);
任何建议将不胜感激
最佳答案
我想我会记下这个问题的答案。
我正在将 rho 和 theta 转换为 m 和 b,然后根据 m 和 b 计算 x 和 y 的值。我相信这可能在某处造成了一些精度错误。
通过直接从 rho 和 theta 获取 x 和 y 而不是通过 m 和 b 来修复此错误。
函数是
y = -cos(theta)/sin(theta)*x + rho/sin(theta);
关于matlab - matlab 和 openCV 中的 hough 变换错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372259/