简体   繁体   中英

Assertion failed when trying to copy part of OpenCV Matrix

Im trying to copy a part of Mat to other matrix, this is my code:

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 (data from cascade detector)

This is return:

OpenCV Error: Assertion failed (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\\ core\\src\\matrix.cpp, line 495

You need to initialize OcrProcess of the same size of the rectangles before calling copyTo :

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

Or, more simply, using clone :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM