在遍历一张图片的轮廓点集时,出现了如下错误。
最后排查之后,发现是在for循环这里出错。
for (size_t i = 0; i < contours.size(); i++)
{
boundRect[i] = boundingRect(Mat(contours[i]));
//rectangle(ThreadholdImageRIO, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 0, 255), 2, 8, 0);
}
去百度了一下,发现是容器下标越界,然后输出了我的点集数,发现是19,并没有越界。
//cout << contours.size() << endl;
最后发现是把容器定义在了寻找轮廓之前
vector
findContours(ThreadholdImageRIO, contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
这里的boundRect(contours.size())其实相当于boundRect(0),也就是这个最小矩形边界容器的大小为1,所以在下面for循环的时候会出错。
for (size_t i = 0; i < contours.size(); i++)
{
boundRect[i] = boundingRect(Mat(contours[i]));
//rectangle(ThreadholdImageRIO, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 0, 255), 2, 8, 0);
}
将容器定义在发现轮廓函数后面就行了
findContours(ThreadholdImageRIO, contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector
参考:https://blog.csdn.net/zhulinzhulinlin/article/details/79363735