title: What to do with negative rho values in hough transform?
body: <p>Here is my code for creating the <code>hough accumulator</code> for lines in image :</p>
  
  <pre><code>void hough_lines_acc(cv::Mat img_a_edges, std::vector&lt;std::vector&lt;int&gt; &gt; &amp;hough_acc) {
    for (size_t r = 0; r &lt; img_a_edges.rows; r++) {
      for (size_t c = 0; c &lt; img_a_edges.cols; c++) {
        int theta = static_cast&lt;int&gt; (std::atan2(r, c) * 180 / M_PI);
        int rho = static_cast&lt;int&gt; ((c * cos(theta)) + (r * sin(theta)));
        if (theta &lt; -90) theta = -90;
        if (theta &gt; 89) theta = 89;
  
        ++hough_acc[abs(rho)][theta];
      }
    }
  
    cv::Mat img_mat(hough_acc.size(), hough_acc[0].size(), CV_8U);
  
    std::cout &lt;&lt; hough_acc.size() &lt;&lt; "  " &lt;&lt; hough_acc[0].size() &lt;&lt; std::endl;
    for (size_t i = 0; i &lt; hough_acc.size(); i++) {
      for (size_t j = 0; j &lt; hough_acc[0].size(); j++) {
        img_mat.at&lt;int&gt; (i,j) = hough_acc[i][j];
      }
    }
  
    imwrite("../output/ps1-­2-­b-­1.png", img_mat);
  }
  </code></pre>
  
  <p><code>theta</code> varies from <code>-90 to 89</code>. I am getting negative rho values. Right now I am just replacing the negative who with a positive one but am not getting a correct answer. What do I do to the negative rho? Please explain the answer.</p>
  
  <p>theta = arctan (y / x)<br>
  rho = x * cos(theta) + y * sin(theta)</p>
  
  <p>Edited code :</p>
  
  <pre><code>bool hough_lines_acc(cv::Mat img_a_edges, std::vector&lt;std::vector&lt;int&gt; &gt; &amp;hough_acc,\
     std::vector&lt;double&gt; thetas, std::vector&lt;double&gt; rhos, int rho_resolution, int theta_resolution) {
    int img_w = img_a_edges.cols;
    int img_h = img_a_edges.rows;
  
    int max_votes = 0;
    int min_votes = INT_MAX;
  
    for (size_t r = 0; r &lt; img_h; r++) {
      for (size_t c = 0; c &lt; img_w; c++) {
        if(img_a_edges.at&lt;int&gt;(r, c) == 255) {
          for (size_t i = 0; i &lt; thetas.size(); i++) {
            thetas[i] = (thetas[i] * M_PI / 180);
            double rho = ( (c * cos(thetas[i])) + (r * sin(thetas[i])) );
            int buff = ++hough_acc[static_cast&lt;int&gt;(abs(rho))][static_cast&lt;int&gt;(i)];
  
            if (buff &gt; max_votes) {
              max_votes = buff;
            }
            if (buff &lt; min_votes) {
              min_votes = buff;
            }
          }
        }
      }
    }
  
    double div = static_cast&lt;double&gt;(max_votes) / 255;
    int threshold = 10;
    int possible_edge = round(static_cast&lt;double&gt;(max_votes) / div) - threshold;
  
    props({
      {"max votes", max_votes},
      {"min votes", min_votes},
      {"scale", div}
    });
    // needed for scaling intensity for contrast
    // not sure if I am doing it correctly
    for (size_t r = 0; r &lt; hough_acc.size(); r++) {
      for (size_t c = 0; c &lt; hough_acc[0].size(); c++) {
        double val = hough_acc[r][c] / div;
        if (val &lt; 0) {
          val = 0;
        }
  
        hough_acc[r][c] = static_cast&lt;int&gt;(val);
      }
    }
  
  
    cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
  
    for (size_t i = 0; i &lt; hough_acc.size(); i++) {
      for (size_t j = 0; j &lt; hough_acc[0].size(); j++) {
        img_mat.at&lt;uint8_t&gt; (i,j) = static_cast&lt;uint8_t&gt;(hough_acc[i][j]);
      }
    }
  
    imwrite("../output/ps1-­2-­b-­1.png", img_mat);
    return true;
  }
  </code></pre>
  
  <p>Still not correct output. What is the error here?</p>