我在 OpenCV 中使用 findCountours() 时遇到过这种情况, 调试断言失败 我有很多谷歌但没有任何帮助,以下是我的代码的一部分。
void HandTrack::ProcessFrame(...){
...
//Convert the colorImage into grayImage
Mat GrayImage;
cvtColor(ColorImages, GrayImage, CV_BGR2GRAY);
//Convert grayImage into binaryImage
Mat BinaryImage(GrayImage.rows, GrayImage.cols, CV_8UC1);
threshold(GrayImage, BinaryImage, 254, 255, CV_THRESH_BINARY);
bitwise_not(BinaryImage, BinaryImage);
//Get the contours from binaryImage
vector<vector<Point>> hand_contours;
findContours(BinaryImage, hand_contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
BinaryImage.release();
//Draw the contours
Mat OutlineImage(GrayImage.rows, GrayImage.cols, CV_8UC1);
rectangle(OutlineImage, Point(0, 0), Point(BinaryImage.cols, BinaryImage.rows), Scalar(255, 255, 255),-1,8);
if (hand_contours.size() > 0) {
drawContours(OutlineImage, hand_contours, -1, (0, 0, 0), 1);
}
waitkey(1);
}
以下是我尝试过的:
最后添加
imshow("img",BinaryImage);
,没有任何变化;评论此行↓,一切顺利
findContours(BinaryImage, hand_contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
单步执行代码,一切都很好,直到下面的 '}'
waitkey(1); }
hand_contours.~vector();
在waitkey(1)之前添加(destruct function);Debug Assertion Failed 显示无论它在哪里;
最后,我通过将局部变量“hand_contours”更改为全局变量来解决它。 但我仍然想知道为什么它解决了。感谢阅读:)