c++ - 调试断言失败 OpenCv is_block_type_valid(header->_block_use)

标签 c++ opencv visual-studio-2015 visual-studio-debugging

我是使用 Visual Studio 和 openCv 进行编程的新手。我写了一个简单的程序来显示图像的红色 channel ,但每次运行代码时都会抛出“DEBUG ASSERTION FAILED”错误。

#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>

#include <iostream>

using namespace std;
using namespace cv;

int main() {
    Mat image;
    image = imread("C:/Users/siddartha/Pictures/sample.jpg");
    if (!image.data) {
        cout << "Cannot load image";
        return -1;
    }
    else {
        if (image.channels() >= 3) {
            vector<Mat> rgb;
            split(image, rgb);
            namedWindow("r");
            imshow("r", rgb[0]);

        }
    }
    while (1);
    return 0;
}

错误:

Debug Assertion Failed!

Program: ...sual Studio 2015\Projects\sampleOpenCV\Debug\sampleOpenCV.exe
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 892

Expression: is_block_type_valid(header->_block_use)

Error Window

最佳答案

您确定图像已正确加载吗?

我认为它没有被正确加载,因此 vector rgb是空的,反过来,元素 rgb[0]不存在触发异常...

我注意到的几件事:

  1. 对 include 语句使用斜杠 ( / ) 而不是反斜杠 ( \ ),即

    #include <opencv2\core.hpp> // Bad!
    #include <opencv2/core.hpp> // Good!
    
  2. 在你的支票上

    if (!image.data) { ... } 
    

    不要假设 image.data设置为 NULLnullptr对于空图像。而是检查

    if (!image.empty()) { ... }
    
  3. 确保调用 cv::imshow(...)之后是对 cv::waitKey( /* delay in ms or 0 to wait for user input */ ) 的调用,比照。 OpenCV reference 中的注释.

  4. while (1); ——是故意的吗?你要的大概是cv::waitKey( 0 ) (见 3.)。

更新:

  1. 确保 vector rgb已经初始化为 channel 数,即

    vector<Mat> rgb(image.channels());
    split(image, rgb);
    // ...
    

更新 2:

Can you tell me what exactly the error meant ?

三件事:

  1. std::vector<T> 的默认构造函数创建一个 vector 。
  2. 显然,cv::split()期望调用者,即,为输出分配数据。如果您不这样做,可能会引发 segmentation fault .
  3. 对于调试版本,一些编译器在内存中永远不会被触及的对象周围添加填充或安全内存。如果这个填充内存在运行时被改变,程序“知道”发生了一些不好的事情并抛出一个像你看到的那样的异常。

关于c++ - 调试断言失败 OpenCv is_block_type_valid(header->_block_use),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760254/

相关文章:

c++ - GL_POINTS 在粒子模拟中相互遮挡

c++ - C 函数从 iOS 设备设置中查找 iOS 用户 12/24 小时偏好

python - OpenCV Python HoughCircles 错误

OpenCV Yolo V3 微型

ios - 从 Visual Studio 2015 构建适用于 iOS 的 Cordova 应用程序时如何解锁钥匙串(keychain)

c# - 未找到与约束契约(Contract)名称 microsoft.visualstudio.portable library Android Portable Project 匹配的导出

c++ - Gtksourceviewmm 语法突出显示不起作用

C++,返回对 std::set 项的 const 和非 const 引用

python - jupyter 上没有模块 cv

Visual Studio 2015 中的向导中缺少 PostgreSQL 数据提供程序