无法解析opencv代码中的malloc错误

时间:2017-01-06 17:24:01

标签: c++ opencv

我的代码:

  cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
  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++) {
      std::cout << hough_acc[i][j] << std::endl;
      img_mat.at<int> (i,j) = hough_acc[i][j];
    }
  }

错误在行img_mat.at<int> (i,j) = hough_acc[i][j];中。 错误是:

ps1: malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

我已检查Mat img_mathough_acc[][]的大小,两者相等。 hough_acc中的值也都是整数。不明白为什么会出现错误。

完整代码:

void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) {
  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(true) {
        for (size_t angle = 0; angle < 180; angle++) {
          double theta = (angle * M_PI / 180);
          double rho = ( (c * cos(theta)) + (r * sin(theta)) );
          int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];

          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;

  cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
  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++) {
      std::cout << hough_acc[i][j] << std::endl;
      img_mat.at<int> (i,j) = hough_acc[i][j];
    }
  }

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

1 个答案:

答案 0 :(得分:0)

您遇到的malloc错误可能是由于在可疑行之前写出越界而引起的。是否可能

storeLoction

写在hough_acc的范围之外? 有些东西是&#34; fishy&#34;因为数组的大小是180,但是你以弧度计算角度,所以它应该(可能是?)达到6。