在图像处理中,经常会遇到程序运行中断,直接报如下错误:
这是选取的roi
不合法导致的越界。
可以加上上面提示的内容,进行合法性判断:
cv::Mat img;
cv::Rect roi(x,y,w,h);
bool is_rect =
(0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= img.cols &&
0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= img.rows);
if (is_rect) {
// 合法时,进行截取roi的处理
cv::imshow("roi",img(roi));
} else {
// 不合法,此时continue、break或者return.
continue;
}