I am new to Programming using Visual Studio and openCv. I wrote a simple program to display the red channel of an image, but every time i run the code it throws "DEBUG ASSERTION FAILED" error.
#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)
Are you absolutely sure that the image has been loaded correctly?
I would think that it hasn't been loaded correctly and because of that the vector rgb
is empty and, in turn, the element rgb[0]
doesn't exist which triggers the exception ...
A couple of things I noted:
Use slashes (/
) for include-statements not backslashes (\
), i.e.
#include <opencv2\core.hpp> // Bad!
#include <opencv2/core.hpp> // Good!
In your check
if (!image.data) { ... }
do not assume that image.data
is set to NULL
or nullptr
for empty images. Instead check
if (!image.empty()) { ... }
Make sure that calls to cv::imshow(...)
are followed by a call to cv::waitKey( /* delay in ms or 0 to wait for user input */ )
, cf. the note in the OpenCV reference.
while (1);
-- is that intentional? What you want is probably cv::waitKey( 0 )
(see 3.).
UPDATE:
Make sure the vector rgb
has been initialized to the number of channels, i.e.
vector<Mat> rgb(image.channels());
split(image, rgb);
// ...
UPDATE 2:
Can you tell me what exactly the error meant ?
Three things:
std::vector<T>
creates an empty vector.cv::split()
expects the caller, i.e. you, to allocate data for the output. If you fail to do so, it's likely provoke a segmentation fault.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With