[英]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)
我是使用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;
}
錯誤:
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]
不存在,觸發異常......
我注意到的一些事情:
使用斜杠( /
)表示包含語句而不是反斜杠( \\
),即
#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 */ )
的調用cv::waitKey( /* delay in ms or 0 to wait for user input */ )
,參見 OpenCV參考中的注釋。
while (1);
- 那是故意的嗎? 你想要的可能是cv::waitKey( 0 )
(見3.)。
更新:
確保向量rgb
已初始化為通道數,即
vector<Mat> rgb(image.channels()); split(image, rgb); // ...
更新2:
你能告訴我這個錯誤究竟是什么意思嗎?
三件事:
std::vector<T>
的默認構造函數創建一個空向量。 cv::split()
期望調用者(即您)為輸出分配數據。 如果你沒有這樣做,它可能會引發分段錯誤 。 它正在編譯對我來說很好。 我在視覺工作室 - 2013年。
在這里你有一個類似於你的案例,也許它會有所幫助: debug-assertion-failed
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.