C++ OpenCV ROI错误

C++ OpenCV ROI错误,c++,opencv,C++,Opencv,我试图制作一个程序,通过在图像上滑动一个ROI并将ROI与模板进行比较来识别模式,它将比较ROI和模板的像素值,并在每个像素匹配时将计数器增加1,然后我将计数器与阈值进行比较,如果它通过,将绘制一个矩形,如果不通过,它将继续在图像中滑动,如果我在其上运行调试器,则在图像中滑动时不会显示任何错误,但如果我正常运行,则会引发下一个异常: OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width &&a

我试图制作一个程序,通过在图像上滑动一个ROI并将ROI与模板进行比较来识别模式,它将比较ROI和模板的像素值,并在每个像素匹配时将计数器增加1,然后我将计数器与阈值进行比较,如果它通过,将绘制一个矩形,如果不通过,它将继续在图像中滑动,如果我在其上运行调试器,则在图像中滑动时不会显示任何错误,但如果我正常运行,则会引发下一个异常:

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 Mat, file /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
terminate called after throwing an instance of 'cv::Exception'
what():  /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/core/src/matrix.cpp:323: error: (-215) 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 function Mat

OpenCV错误:断言失败(0这意味着您正试图将ROI区域移出图像平面。您应该确保ROI区域位于图像平面内,以避免崩溃

对于您的情况,您可以这样做:

// check the box within the image plane
if (0 <= box.x
    && 0 <= box.width
    && box.x + box.width <= iOrig.cols
    && 0 <= box.y
    && 0 <= box.height
    && box.y + box.height <= iOrig.rows){
    // box within the image plane
    Mat region(iOrig, box);
}
else{
    // box out of image plane, do something...
}
//选中图像平面内的复选框

如果(0)谢谢,我已经纠正了它,是的,我的错误在循环中,我超出了图像的界限
// check the box within the image plane
if (0 <= box.x
    && 0 <= box.width
    && box.x + box.width <= iOrig.cols
    && 0 <= box.y
    && 0 <= box.height
    && box.y + box.height <= iOrig.rows){
    // box within the image plane
    Mat region(iOrig, box);
}
else{
    // box out of image plane, do something...
}