我是使用 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)
最佳答案
您确定图像已正确加载吗?
我认为它没有被正确加载,因此 vector rgb
是空的,反过来,元素 rgb[0]
不存在触发异常...
我注意到的几件事:
对 include 语句使用斜杠 (
/
) 而不是反斜杠 (\
),即#include <opencv2\core.hpp> // Bad! #include <opencv2/core.hpp> // Good!
在你的支票上
if (!image.data) { ... }
不要假设
image.data
设置为NULL
或nullptr
对于空图像。而是检查if (!image.empty()) { ... }
确保调用
cv::imshow(...)
之后是对cv::waitKey( /* delay in ms or 0 to wait for user input */ )
的调用,比照。 OpenCV reference 中的注释.while (1);
——是故意的吗?你要的大概是cv::waitKey( 0 )
(见 3.)。
更新:
确保 vector
rgb
已经初始化为 channel 数,即vector<Mat> rgb(image.channels()); split(image, rgb); // ...
更新 2:
Can you tell me what exactly the error meant ?
三件事:
-
std::vector<T>
的默认构造函数创建一个空 vector 。 - 显然,
cv::split()
期望调用者,即你,为输出分配数据。如果您不这样做,可能会引发 segmentation fault . - 对于调试版本,一些编译器在内存中永远不会被触及的对象周围添加填充或安全内存。如果这个填充内存在运行时被改变,程序“知道”发生了一些不好的事情并抛出一个像你看到的那样的异常。
关于c++ - 调试断言失败 OpenCv is_block_type_valid(header->_block_use),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760254/