why this code is crashing on cvtColor? - c++

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?

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.

Related

Opencv with cuda GpuMat::create() call causes access violation

I compiled (debug and release) opencv 4.1.0 with contrib modues and enabled cuda.
I'm trying to create a GpuMat and upload host data from cv::Mat.
This works properly when running in debug mode. However when I run in release mode (even when turning off optimizations) I get an access violation in the GpuMat::create() function (gpu_mat.cu line 174):
bool allocSuccess = allocator->allocate(this, rows, cols, esz);
"Exception thrown: read access violation.
this->allocator was 0x21A."
My code for creating the GpuMat:
cv::Mat raw = cv::Mat(inImg->height, inImg->width, CV_16UC1, inImg->host_data);
g_inImg.create(inImg->height, inImg->width, CV_16UC1);
The crash is only happens in Release mode.
Do anyone has a clue what could be the reason?
Thank you in advance.

Opencv cv::Subtract method throwing System.Runtime.InteropServices.SEHException Exception

I am currently trying to implement the Lucy Richardson algorithm in Opencv, when it comes to running the 'cv::subtract' method in my program it throws an InteropServices exception (stack trace below)
************** Exception Text **************
System.Runtime.InteropServices.SEHException (0x80004005): External has thrown an exception.
at cv.Mat.=(Mat* , MatExpr* expr) in e:\opencv\opencv\build\include\opencv2\core\mat.inl.hpp:line 3107
at LucyRichardson.LucyRich(LucyRichardson* , Mat* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* imagePath) in e:\documents\development\realtimeimageprocessing\imageprocessing\imageprocessing\lucyrichardson.cpp:line 63
Below is the block of code where the error occurs, it is thrown on the second line.
im_correction = cv::Mat (cvSize(383, 357), 8, 1);
cv::subtract(im, im_conv_kernel, im_correction);
cv::namedWindow("Sub");
cv::imshow("Sub", im_correction);
The variables im and im_conv_kernel are both of type cv::Mat and are correctly populated and the variable im_correction I have tried creating a version of before I save the result of the subtraction in.
I am using cv::subtractions fine in other parts of the program.
Does anyone know why this error occurs and how I could fix it? Or if there is a different method I could try for the subtraction?
I have worked out where the problem was I needed to make sure all of the images where of the same type. - After adding the below three lines before performing the subtract it worked fine.
im.convertTo(im, CV_8UC1);
im_conv_kernel.convertTo(im_conv_kernel, CV_8UC1);
im_correction.convertTo(im_correction, CV_8UC1);

How do I guard against failure of cvLoad?

I am writing a program that uses OpenCV and involves intrinsic and distortion parameters. These parameters are loaded from .xml files saved on disc. I use the following commands in my opening declarations to load the files:
CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
This works fine as long as the files are in the program's working directory. When they are not, the program crashes without any indication of the nature of the error. I have made the mistake of not having the xml files located correctly multiple times before, and I would like to make this easier to troubleshoot in the future.
I would like to create a guard against the files failing to load. Perhaps if they are not present my program could display an error message and exit gracefully. I saw the method suggested here, and it should work for me, but I was wondering if there was a cleaner way to do it without including another header.
For example, the OpenCV function cvQueryFrame returns 0 if it does not return a frame. I use the code
frame = cvQueryFrame(capture);
if(!frame)
{
printf("ERROR: Could not get frame from webcam.");
exit(-1);
}
to exit the program if cvQueryFrame fails to return a frame. I would like to do something similar with my matrix loading commands. Is this possible, and if so, how should I do it?
I checked the OpenCV documentation and could not find a description of cvLoad's behaviour when it cannot find the file specified so I am not sure how to proceed.
I am writing this project in C++ and running it on Windows 7.
It works. Go ahead and try it yourself:
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
if (!distortion)
{
printf("!!! cvLoad failed");
exit(-1);
}

Opencv cvThreshold bug

Hello I don t know if I am doing something wrong or not but when I do the following:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_8U,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
everything works fine then when I try to use unsigned short values:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_16U,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
my program crashes... I use opencv 2.4.2 I think this could maybe be a bug in opencv.
Somehow if I try the following:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_16S,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
it doesn t crash anymore
It is not crashing. It is throwing an exception and since you are not catching it, your program is aborting.
OpenCV Thresholding does not support 16U.
The supported ones are: 8U, 16S and 32F
See the OpenCV documentation for function threshold. The first parameter is 8 or 32 bit image
P.S. In crash message you should see the reason for crashing

Application crashes on equalizeHist of OpenCV

My MFC app runs various API from OpenCV2. Everything else is working fine. But when my program runs
cv::Mat result;
cv::equalizeHist(m_cvImage,result);
I get following runtime exception.
Unhandled exception at 0x7727fbae in OpenCVTest.exe: Microsoft C++ exception: cv::Exception at memory location 0x0029e944..
"C:\slave\WinInstallerMegaPack\src\opencv\modules\imgproc\src\histogram.cpp:2430: error: (-215) CV_ARE_SIZES_EQ(src, dst) && CV_ARE_TYPES_EQ(src, dst) && CV_MAT_TYPE(src->type) == CV_8UC1"
According to debugger, the exception was thrown in the middle of processing (about 40%) the image in equalizeHist. Is there anything I need to do? FYI: I am using binary OpenCV provided by its web site.
UPDATE:
I've resolved this issue by converting images to gray-level before equalizing it. I just didn't know
the function only works with gray-level image
images that look like gray-level can be non-gray.
I imagine the problem you are encountering is that m_cvImage is a 3-channel image. So, you need to convert it to a grayscale image before you can call equalizeHist.
cvtColor(m_cvImage, m_cvImage, CV_BGR2GRAY);
cv::Mat result;
cv::equalizeHist(m_cvImage, result);
Also, have a look at the EqualizeHist_Demo.cpp tutorial sample to see how it is used.