OpenCV copyTo assert error

青春壹個敷衍的年華 提交于 2020-01-06 03:26:12

问题


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.

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

回答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);



回答2:


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


来源:https://stackoverflow.com/questions/29217722/opencv-copyto-assert-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!