Home > Net >  Having trouble with Hough Transform in C
Having trouble with Hough Transform in C

Time:10-25

I am trying to make a code that detects sudoku grids. This means that I have to apply lots of filters on the image and then apply the Hough Transform algorithm on it following the output of Sobel algorithm (which makes the edges appear as white pixels on a black image).

I somewhat understand its concept and why it is important to use Rho = x*cos(Theta) y*sin(Theta) to obtain the edges coordinates in a parameter space.

However, I'm having issues with Rho and my accumulator array. Here is my code so far:

void hough(SDL_Surface* image_surface)
{

    unsigned int width = image_surface->w;
    unsigned int height = image_surface->h;
    
    unsigned int Rhos, Thetas;
    Rhos = sqrt(width * width   height * height);
    Thetas = 180;

    //initialise accumulator array
    unsigned int acc_array[Rhos][Thetas];
    
    for (size_t i = 0; i < Rhos; i  )
    {
        for (size_t j = 0; j < Thetas; j  )
            acc_array[i][j] = 0;
    }
    Uint32 pixel;
    Uint8 r, g, b;

    //go through each pixels
    for (size_t x = 0; x < width; x  )
    {
        for (size_t y = 0; y < height; y  )
        {
            pixel = get_pixel(image_surface, x, y);
            SDL_GetRGB(pixel, image_surface->format, &r, &g, &b);
                  
            //if white
            if (r g b == 765)
            {
                //p = x*cos(t)   y*sin(t)
                //check for every t
                for (int t = 0; t < 180;t  )
                {
                    unsigned int p = x*cos(t)   y*sin(t);
                    acc_array[p][t]  ;
                }
            }
        }
    }

//rest of the code below...

My problem is that when, for example, I am on the pixel (20, 1882) of the image and my theta (or t) = 4, p (or Rho) becomes -1437. If Rho is negative, then I cannot increment its place in the accumulator array since an index cannot be negative.

Can anyone help me out with this?

CodePudding user response:

The cos() and sin() functions expect the angle to be in radians. Based on your for loop range it looks t is a value in degrees.

CodePudding user response:

You don't need to go through 180 degrees, just 90. The negative is coming from thetas > 90.

  • Related