【问题标题】:OpenCV copyTo assert errorOpenCV 复制到断言错误
【发布时间】:2015-03-23 18:29:58
【问题描述】:

在将一个 Mat 复制到另一个 Mat 的感兴趣区域时,我遇到了一个我以前从未见过的错误。谷歌搜索并没有找到很多结果,而且似乎没有一个是相关的。

我已经包含了错误的屏幕截图以及 Mat 的一些属性。

这是代码:

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

【问题讨论】:

  • 尝试结果(rect).copyTo(tempResult(rect));

标签: c++ opencv mat


【解决方案1】:

cv::Mat::operator(cv::Rect roi) 方法提取与 cv::Rect roi 大小相同的子矩阵。 但是您定义了一个缺少 1 行和 1 个列的 cv::Rect 对象,因此tempResult(rect) 返回的输出矩阵小于矩阵resultcv::Mat::CopyTo 启动异常,因为要复制的输入小于输出参数。

解决这个问题:

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

【讨论】:

    【解决方案2】:

    对于cv::Rect,其格式为(x, y, width, height),而不是(x1, y1, x2, y2)。这就是为什么,在我看来,你会得到错误。

    如果是,您需要将rect 更改为:

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

    如果不是(即你真的是指rect(x, y, width-1, height-1)),你可以这样做:

    result(rect).copyTo(tempResult(rect));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      相关资源
      最近更新 更多