mans
mans

Reputation: 18228

why this code is crashing on cvtColor?

I have this code:

void * imageBuffer = reinterpret_cast<void *>(exposureBuffer + imageHeader->imgoffset);
cv::Mat imageRaw(imageHeader->height, imageHeader->width, CV_8UC1, imageBuffer);
cv::Mat imageColour;
cv::cvtColor(imageRaw, imageColour, cv::COLOR_BayerGR2BGR); 

when I run this and stops debugger on this line:

cv::Mat imageColour;

I can see that imageRaw has a valid image in it (I can see the image in image view and it is a valid image.)

but then the application crashes on this line:

 cv::cvtColor(imageRaw, imageColour, cv::COLOR_BayerGR2BGR); 

and it seems that a mat file was created but not enough memory allocated for it.

The error message is:

Unhandled exception at 0x00007FF7503F992B in test_PictureProcessing.exe: 0xC0000005: Access violation reading location 0x0000000000000023.

I am using OpenCv 3. I have similar code which runs successfully on openCV 2.

Edit1

I changed the code to this one to make sure that imagebuffer is a valid buffer and the fact that I am not initializing imageColour is not the problem:

void *imageBuffer = new char[imageHeader->height* imageHeader->width];
cv::Mat imageRaw(imageHeader->height, imageHeader->width, CV_8UC1, imageBuffer);
cv::Mat imageColour = imageRaw.clone();

but I am still getting error on this line:

cv::Mat imageColour = imageRaw.clone();

Edit 2

This is also crashing!

cv::Mat imageRaw(imageHeader->height, imageHeader->width, CV_8UC1);
cv::Mat imageColour = imageRaw.clone();

Why this simple code crashing?

Upvotes: 0

Views: 1484

Answers (1)

mans
mans

Reputation: 18228

I found the problem which is very strange!

I forgot to include opencv.hpp to my source file after adding it, it worked perfectly.

It is strange as I did not get any compile error, but I got run time error.

If you see your openCV behave strangely, make sure that you included opencv.hpp to your source code. it may help you to solve your problem! Not all problems are coming from missing this header as CroCo mentioned.

Upvotes: 1

Related Questions