在MATLAB和OpenCV的霍夫变换错误? [英] hough transform error in matlab and openCV?

查看:143
本文介绍了在MATLAB和OpenCV的霍夫变换错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Matlab和OpenCV/labview在我的应用程序中使用Hough变换,发现对于某些图像,Hough变换明显(一致)给出了错误的线条拟合

I have been using the Hough transform in my application both using Matlab and OpenCV/labview and found that for some images, the hough transform gave an obviously wrong line fit (consistently)

这是测试图像和叠加图像.角度似乎正确,但rho已关闭.

Here are the test and overlayed images. The angle seem right, but the rho is off.

在下面的图像上,您将看到顶部图像试图将一条线拟合到原始图像的左侧,而底部图像试图将一条线拟合到图像的右侧.

On the image below, you will see the top image tries to fit a line to the left side of the original image and the bottom image fits a line to the right side of the image.

在Matlab中,我通过以下方式调用Hough函数

In Matlab, I call the Hough function through

[H1D,theta1D,rho1D] = hough(img_1D_dilate,'ThetaResolution',0.2);

在C ++中,我修剪了OpenCV HoughLines函数,因此最后只剩下了我们要填充累加器的部分.请注意,因为我的theta分辨率为0.2,所以我需要分析900个角度. tabSin和tabCos是在函数之前定义的,因此它们只是角度的正弦和余弦.

in C++, i trimmed the OpenCV HoughLines function so I end up with only the part we are filling the accumulator. Note that because my theta resolution is 0.2, I have 900 angles to analyze. The tabSin and tabCos are defined prior to the function so that they are just a sin and cos of the angle.

请注意,这些例程通常运行良好,但仅在特定情况下,它会按照我所展示的方式进行操作.

Note that these routines generally work well, but just for specific cases it performs the way I have shown.

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];

TabCos and tabSin are defined in Labview with this code int32 i; float64 theta_prec; float64 tabSin[180]; float64 tabCos[180];

theta_prec = 1/180 * 3.14159; 对于(i = 0; i <180; i ++) { tabSin [i] = sin(i theta_prec); tabCos [i] = cos(i theta_prec); }

theta_prec = 1/180*3.14159; for (i = 0;i<180;i++) { tabSin[i] = sin(itheta_prec); tabCos[i] = cos(itheta_prec); }

任何建议将不胜感激

推荐答案

我想我会给出这个问题的答案.

I guess i'll put down the answer to this problem.

我正在将rho和theta转换为m和b,然后根据m和b计算x和y的值.我相信这可能在某处引起了一些精度错误.

I was converting the rho and theta into m and b, then computing the values of x and y from the m and b. I believe this may have caused some precision error somewhere.

通过直接从rho和theta获得x和y而不是通过m和b来解决此错误.

this error was fixed by obtaining x and y directly from rho and theta rather than going through m and b.

功能是

y = -cos(theta)/sin(theta)*x + rho/sin(theta);

这篇关于在MATLAB和OpenCV的霍夫变换错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆