2
votes

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. alt text

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.

alt text

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
Any code? I don't like guessing from images :)Karel Petranek
Don't you need another loop for numrho?nimcap
the dimensions of the accumulator are numrho and numtheta. in the calculation, we are going through all the thetas and finding the rho that would be associated to it. Then once you have theta and rho, you just increment that bin.Denis
Now I see your code is from OpenCV sources. I used cvHoughLines2 with CV_HOUGH_STANDARD. The result is flawless: imgur.com/x2BNr.png . Maybe there is something wrong with your drawing function?nimcap
Can you post all of your code?nimcap

1 Answers

1
votes

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