我是使用 Visual Studio 和 openCv 编程的新手。我编写了一个简单的程序来显示图像的红色通道,但每次运行代码时都会抛出“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;
}
Error:
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)
您绝对确定图像已正确加载吗?
我认为它没有正确加载,因此向量rgb
是空的,反过来,元素rgb[0]
不存在会触发异常...
我注意到以下几点:
-
使用斜杠 (/
) 用于包含语句而不是反斜杠 (\
), i.e.
#include <opencv2\core.hpp> // Bad!
#include <opencv2/core.hpp> // Good!
-
在你的支票中
if (!image.data) { ... }
do not假使,假设image.data
被设定为NULL
or nullptr
对于空图像。相反检查
if (!image.empty()) { ... }
确保调用cv::imshow(...)
随后调用cv::waitKey( /* delay in ms or 0 to wait for user input */ )
,参见中的注释OpenCV 参考 http://docs.opencv.org/3.1.0/d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563.
while (1);
——这是故意的吗?你想要的可能是cv::waitKey( 0 )
(见 3)。
UPDATE:
-
确定向量rgb
已初始化为通道数,即
vector<Mat> rgb(image.channels());
split(image, rgb);
// ...
更新2:
你能告诉我这个错误到底意味着什么吗?
三件事:
- 默认构造函数
std::vector<T>
创建一个empty vector.
- 显然,
cv::split()
期望调用者,即you,为输出分配数据。如果你不这样做,可能会激起分段故障 https://stackoverflow.com/questions/2346806/what-is-segmentation-fault.
- 对于调试构建,一些编译器会在内存中的对象周围添加填充或安全内存,而这些对象永远不会被触及。如果这个填充内存在运行时被改变,程序就会“知道”发生了一些不好的事情,并抛出一个像你看到的那样的异常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)