0

我在 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);
}

以下是我尝试过的:

  1. 最后添加imshow("img",BinaryImage);,没有任何变化;

  2. 评论此行↓,一切顺利

    findContours(BinaryImage, hand_contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

  3. 单步执行代码,一切都很好,直到下面的 '}'

    waitkey(1); }

  4. hand_contours.~vector();在waitkey(1)之前添加(destruct function);Debug Assertion Failed 显示无论它在哪里;

最后,我通过将局部变量“hand_contours”更改为全局变量来解决它。 但我仍然想知道为什么它解决了。感谢阅读:)

忽略它,调试中的图像

4

1 回答 1

0

您的问题出在此处:

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

您将 Bi​​naryImage 创建为 CV_8UC1,这很好,但我感觉您的 GrayImage 并不总是“......一个 8 位单通道图像”。根据 文档的要求。在某个地方,它可能没有被正确截断。

您的 GrayImage 来自彩色图像,可能偶尔会有一些空通道。检查以确保您的 dst 和 src Mat 的格式都是正确的(即 99.9% 的时间是断言失败的原因)。

至于如何通过改变全球来解决这个问题?如果没有看到其余的代码,真的没有办法说出来。我最好的猜测是,它以某种方式导致您的某些功能将您的 MAT 的内容更改为您想要的格式,然后到达您在上面向我们展示的功能。但是没有看到它真的没有办法说出来。

但是,故事的寓意是,只要您可以检查 src 和 dst 的格式是否正确,您将避免大多数断言失败。

于 2016-07-19T15:52:00.937 回答