尝试复制部分 OpenCV 矩阵时断言失败

Assertion failed when trying to copy part of OpenCV Matrix

本文关键字:断言 失败 OpenCV 复制部      更新时间:2023-10-16

我试图将Mat的一部分复制到其他矩阵,这是我的代码:

Mat OCRprocess;
OCRImage(Rect(plates[i].x, plates[i].y, plates[i].width, plates[i].height)).copyTo(OCRprocess);

ROI: x: 1200 y: 608 w: 356 h: 89 (数据来自级联检测器)

这是返回:

OpenCV 错误:断言失败 (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\ 核心\SRC\矩阵.cpp,第 495 行

在调用copyTo之前,您需要初始化OcrProcess相同大小的矩形:

// Your rect
Rect r(plates[i].x, plates[i].y, plates[i].width, plates[i].height);
// Initialize the destination image
Mat OCRprocess(r.height, r.width, OCRImage.type());
// Copy
OCRImage(Rect).copyTo(OCRprocess);

或者,更简单地说,使用 clone

Mat OCRprocess = OCRImage(Rect(plates[i].x, plates[i].y, plates[i].width, plates[i].height)).clone();