darth vader
darth vader

Reputation: 321

Unable to resolve malloc error in opencv code

My code :

  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];
    }
  }

The error is in the line img_mat.at<int> (i,j) = hough_acc[i][j];. Error is :

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)

I have checked size of Mat img_mat and hough_acc[][] and both are equal. The values in hough_acc are also all integers. Don't understand why the error is there.

Full code :

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);
}

Upvotes: 3

Views: 623

Answers (1)

Kostas
Kostas

Reputation: 127

The malloc error you are getting can be caused by writing out of bounds before the suspected line. Is it possible that

    int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];

writes outside the bounds of hough_acc? Something is "fishy" since the array is of size 180 but you calculate the angles in radians so it should (probably?) go up to 6.

Upvotes: 1

Related Questions