如何处理霍夫变换中的负 rho 值?

What to do with negative rho values in hough transform?

这是我为图像中的线条创建 hough accumulator 的代码:

void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) {
  for (size_t r = 0; r < img_a_edges.rows; r++) {
    for (size_t c = 0; c < img_a_edges.cols; c++) {
      int theta = static_cast<int> (std::atan2(r, c) * 180 / M_PI);
      int rho = static_cast<int> ((c * cos(theta)) + (r * sin(theta)));
      if (theta < -90) theta = -90;
      if (theta > 89) theta = 89;

      ++hough_acc[abs(rho)][theta];
    }
  }

  cv::Mat img_mat(hough_acc.size(), hough_acc[0].size(), CV_8U);

  std::cout << hough_acc.size() << "  " << hough_acc[0].size() << std::endl;
  for (size_t i = 0; i < hough_acc.size(); i++) {
    for (size_t j = 0; j < hough_acc[0].size(); j++) {
      img_mat.at<int> (i,j) = hough_acc[i][j];
    }
  }

  imwrite("../output/ps1-­2-­b-­1.png", img_mat);
}

theta 不同于 -90 to 89。我得到负 rho 值。现在我只是用肯定的人代替否定的人,但没有得到正确的答案。我该如何处理负 rho?请解释答案。

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

编辑代码:

bool hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc,\
   std::vector<double> thetas, std::vector<double> 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 < img_h; r++) {
    for (size_t c = 0; c < img_w; c++) {
      if(img_a_edges.at<int>(r, c) == 255) {
        for (size_t i = 0; i < 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<int>(abs(rho))][static_cast<int>(i)];

          if (buff > max_votes) {
            max_votes = buff;
          }
          if (buff < min_votes) {
            min_votes = buff;
          }
        }
      }
    }
  }

  double div = static_cast<double>(max_votes) / 255;
  int threshold = 10;
  int possible_edge = round(static_cast<double>(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 < hough_acc.size(); r++) {
    for (size_t c = 0; c < hough_acc[0].size(); c++) {
      double val = hough_acc[r][c] / div;
      if (val < 0) {
        val = 0;
      }

      hough_acc[r][c] = static_cast<int>(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 < hough_acc.size(); i++) {
    for (size_t j = 0; j < hough_acc[0].size(); j++) {
      img_mat.at<uint8_t> (i,j) = static_cast<uint8_t>(hough_acc[i][j]);
    }
  }

  imwrite("../output/ps1-­2-­b-­1.png", img_mat);
  return true;
}

仍然不正确的输出。这里有什么错误?

两个正数的 atan2...不应该给你负角,它应该只给你一个范围 0-90

同样对于霍夫变换,我想你想要所有与一个点相关的东西(即本例中的 0,0)。我认为你实际上想要制作 theta=90-atan2(r,c)

不过,不可否认,我有点困惑,因为我认为你必须对线方向进行编码,而不仅仅是 "edge pt"。也就是说,我认为在每个边缘点,你必须提供一个离散的猜测线轨迹数组,并计算每个轨迹的 rho 和 theta,然后将所有这些都放入你的累加器中。照原样...我不确定你在计算什么。