为啥我解决了“Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)”

Posted

技术标签:

【中文标题】为啥我解决了“Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)”【英文标题】:WHY I solved “Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)”为什么我解决了“Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)” 【发布时间】:2016-07-19 14:37:17 【问题描述】:

我在 OpenCV 中使用 findCountours() 时遇到过这种情况, Debug Assertion Failed 我有很多谷歌,但没有任何帮助,以下是我的代码的一部分。

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

    在waitkey(1)之前添加hand_contours.~vector();(destruct fuction); Debug Assertion Failed 显示在哪里;

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

ignore it,images in debuging

【问题讨论】:

问题出在您的配置中。请务必在调试模式下链接到调试 OpenCV 库,并在发布时发布库。还要确保使用使用与您的项目相同的编译器编译的 OpenCV。现在你还没有解决问题,它只是被隐藏了 你好三木,谢谢你的回答,我之前也找到了一些类似的答案。我不擅长配置,但我会尝试检查一下,谢谢。 【参考方案1】:

您的问题出在此处:

//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 位单通道图像”。根据documentation. 的要求,它可能不会被正确截断。

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

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

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

【讨论】:

感谢您的回答!我再次调试,我确信 GrayImage 和 BinaryImage 是一个通道图像(i.stack.imgur.com/tc00Q.png)。我贴的代码都是相关的,你可以发现 GrayImage、BinaryImage 是 new();除了前面部分的 ColorImage(CV_8UC4)。本来想全部贴的,但是太长了。再次感谢:)

以上是关于为啥我解决了“Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)”的主要内容,如果未能解决你的问题,请参考以下文章

为啥我有内存泄漏?我无法解决

为啥我得到 null 而不是 Map 对象?如何解决这个问题?

回调解决未处理的承诺,为啥?

为啥我的收藏视图在向下或向上滚动时很慢?如何解决性能问题?我已经清理了我的 UI 代码

为啥在重新构建解决方案后构建速度更快?

为啥这个结构类型“不完整”,它真的用指针解决了吗?