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

dar*_*der 5 c++ opencv computer-vision

这是我在图像中创建hough accumulatorfor 行的代码:

\n\n
void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) {\n  for (size_t r = 0; r < img_a_edges.rows; r++) {\n    for (size_t c = 0; c < img_a_edges.cols; c++) {\n      int theta = static_cast<int> (std::atan2(r, c) * 180 / M_PI);\n      int rho = static_cast<int> ((c * cos(theta)) + (r * sin(theta)));\n      if (theta < -90) theta = -90;\n      if (theta > 89) theta = 89;\n\n      ++hough_acc[abs(rho)][theta];\n    }\n  }\n\n  cv::Mat img_mat(hough_acc.size(), hough_acc[0].size(), CV_8U);\n\n  std::cout << hough_acc.size() << "  " << hough_acc[0].size() << std::endl;\n  for (size_t i = 0; i < hough_acc.size(); i++) {\n    for (size_t j = 0; j < hough_acc[0].size(); j++) {\n      img_mat.at<int> (i,j) = hough_acc[i][j];\n    }\n  }\n\n  imwrite("../output/ps1-\xc2\xad2-\xc2\xadb-\xc2\xad1.png", img_mat);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

theta变化自-90 to 89. 我得到负 rho 值。现在我只是用积极的人代替消极的人,但没有得到正确的答案。对于负值 rho 该怎么办?请解释一下答案。

\n\n

θ = arctan (y / x)
\nrho = x * cos(θ) + y * sin(θ)

\n\n

编辑后的代码:

\n\n
bool hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc,\\\n   std::vector<double> thetas, std::vector<double> rhos, int rho_resolution, int theta_resolution) {\n  int img_w = img_a_edges.cols;\n  int img_h = img_a_edges.rows;\n\n  int max_votes = 0;\n  int min_votes = INT_MAX;\n\n  for (size_t r = 0; r < img_h; r++) {\n    for (size_t c = 0; c < img_w; c++) {\n      if(img_a_edges.at<int>(r, c) == 255) {\n        for (size_t i = 0; i < thetas.size(); i++) {\n          thetas[i] = (thetas[i] * M_PI / 180);\n          double rho = ( (c * cos(thetas[i])) + (r * sin(thetas[i])) );\n          int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(i)];\n\n          if (buff > max_votes) {\n            max_votes = buff;\n          }\n          if (buff < min_votes) {\n            min_votes = buff;\n          }\n        }\n      }\n    }\n  }\n\n  double div = static_cast<double>(max_votes) / 255;\n  int threshold = 10;\n  int possible_edge = round(static_cast<double>(max_votes) / div) - threshold;\n\n  props({\n    {"max votes", max_votes},\n    {"min votes", min_votes},\n    {"scale", div}\n  });\n  // needed for scaling intensity for contrast\n  // not sure if I am doing it correctly\n  for (size_t r = 0; r < hough_acc.size(); r++) {\n    for (size_t c = 0; c < hough_acc[0].size(); c++) {\n      double val = hough_acc[r][c] / div;\n      if (val < 0) {\n        val = 0;\n      }\n\n      hough_acc[r][c] = static_cast<int>(val);\n    }\n  }\n\n\n  cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));\n\n  for (size_t i = 0; i < hough_acc.size(); i++) {\n    for (size_t j = 0; j < hough_acc[0].size(); j++) {\n      img_mat.at<uint8_t> (i,j) = static_cast<uint8_t>(hough_acc[i][j]);\n    }\n  }\n\n  imwrite("../output/ps1-\xc2\xad2-\xc2\xadb-\xc2\xad1.png", img_mat);\n  return true;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

Sne*_*ear 1

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

同样对于霍夫变换,我认为您希望所有内容都相对于一个点(即本例中的 0,0)。我认为为此你实际上想要使 theta=90-atan2(r,c)

不可否认,我有点困惑,因为我认为你必须编码线方向,而不仅仅是“边缘点”。即,我认为在每个边缘点,您必须提供一组离散的猜测线轨迹,并计算每个轨迹的 rho 和 theta,并将所有这些放入累加器中。原样...我不确定你在计算什么。