title: Having trouble with Hough Transform in C
body: <p>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).</p>
  <p>I somewhat understand its concept and why it is important to use <code>Rho = x*cos(Theta) + y*sin(Theta)</code> to obtain the edges coordinates in a parameter space.</p>
  <p>However, I'm having issues with Rho and my accumulator array.
  Here is my code so far:</p>
  <pre><code>void hough(SDL_Surface* image_surface)
  {
  
      unsigned int width = image_surface-&gt;w;
      unsigned int height = image_surface-&gt;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 &lt; Rhos; i++)
      {
          for (size_t j = 0; j &lt; Thetas; j++)
              acc_array[i][j] = 0;
      }
      Uint32 pixel;
      Uint8 r, g, b;
  
      //go through each pixels
      for (size_t x = 0; x &lt; width; x++)
      {
          for (size_t y = 0; y &lt; height; y++)
          {
              pixel = get_pixel(image_surface, x, y);
              SDL_GetRGB(pixel, image_surface-&gt;format, &amp;r, &amp;g, &amp;b);
                    
              //if white
              if (r+g+b == 765)
              {
                  //p = x*cos(t) + y*sin(t)
                  //check for every t
                  for (int t = 0; t &lt; 180;t++)
                  {
                      unsigned int p = x*cos(t) + y*sin(t);
                      acc_array[p][t]++;
                  }
              }
          }
      }
  
  //rest of the code below...
  </code></pre>
  <p>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.</p>
  <p>Can anyone help me out with this?</p>