hough transform error in matlab and openCV?

别来无恙 提交于 2019-12-11 03:35:51

问题


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)

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.

In Matlab, I call the Hough function through

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

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 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; for (i = 0;i<180;i++) { tabSin[i] = sin(itheta_prec); tabCos[i] = cos(itheta_prec); }

any suggestions would be greatly appreciated


回答1:


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

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.

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

the function is

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


来源:https://stackoverflow.com/questions/4372259/hough-transform-error-in-matlab-and-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!