2

While copying one Mat into the region of interest of another I came accross an error I've never seen before. Googling it didn't turn up many results and none of them seems to be relevant.

I have included a screenshot of the error as well as some properties of the Mat's.

enter image description here

This is the code:

    std::cout << "size height,width: " << size.height << ", " << size.width << std::endl;
    cv::Mat tempResult(size.width, size.height, result.type());

    std::cout << "tempResult cols,rows: " << tempResult.cols << ", " << tempResult.rows << std::endl;
    std::cout << "tempResult type: " << tempResult.type() << std::endl;
    std::cout << "tempResult channels: " << tempResult.channels() << std::endl;

    std::cout << "result cols,rows: " << result.cols << ", " << result.rows << std::endl;
    std::cout << "result type: " << result.type() << std::endl;
    std::cout << "result channels: " << result.channels() << std::endl;

    cv::Rect rect(0, 0, result.cols-1, result.rows-1);

    std::cout << "rect size: " << rect.size() << std::endl;
    result.copyTo(tempResult(rect));
Tom
  • 950
  • 3
  • 15
  • 27

2 Answers2

1

The cv::Mat::operator(cv::Rect roi) method extract a submatrix with the same size of the cv::Rect roi. But you defined a cv::Rect object with 1 row and 1 col missing, so the output matrix returned by tempResult(rect) is smaller the the matrix result. cv::Mat::CopyTo launch an exception because the input to copy is smaller than the output argument.

To fix this :

cv::Rect rect(0, 0, result.cols, result.rows);
Marcassin
  • 1,386
  • 1
  • 11
  • 21
0

For cv::Rect, its format is (x, y, width, height), not (x1, y1, x2, y2). That's why, in my opinion, you get the error.

If yes, you will need to change rect to:

cv::Rect rect(0, 0, result.cols, result.rows);

If not (i.e. you really means rect(x, y, width-1, height-1)), you can do like this:

result(rect).copyTo(tempResult(rect));
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174