我一直在使用 Matlab 和 OpenCV/labview 的应用程序中使用霍夫变换,发现对于某些图像,霍夫变换给出了明显错误的线拟合(一致)
Here are the test and overlayed images. The angle seem right, but the rho is off.
在下图中,您将看到顶部图像尝试将一条线拟合到原始图像的左侧,而底部图像尝试将一条线拟合到图像的右侧。
在Matlab中,我通过调用Hough函数
[H1D,theta1D,rho1D] = hough(img_1D_dilate,'ThetaResolution',0.2);
在 C++ 中,我修剪了 OpenCV HoughLines 函数,因此最终只有我们填充累加器的部分。请注意,因为我的 theta 分辨率为 0.2,所以我有 900 个角度需要分析。 tabSin 和 tabCos 在函数之前定义,因此它们只是角度的正弦和余弦。
请注意,这些例程通常运行良好,但仅对于特定情况,它才会按照我所示的方式执行。
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;itheta_prec);
tabCos[i] = cos(itheta_prec);
}
任何建议将不胜感激