Problems using cv::split - c++

I am using OpenCV 2.4.6, and trying to generate some histograms. The original example code took an image from local storage, but I modified it to use a VideoCapture object. When the code gets to cv::split(), it brings an unhandled exception. It prompts this:
"Unhandled exception at 0x5465B3D9 (opencv_core246.dll) in
visionProject.exe: 0xC0000005: Access violation writing location
0x1800E633."
The code I have is this:
VideoCapture camera;
camera.open(0);
camera >> src;
if( !src.data )
{ return -1; }
/// Separate the image in 3 places ( B, G and R )
vector <Mat> bgr_planes;
split(src, bgr_planes );
Thanks for your time.

Thanks to assistance from a friend, I was able to make it work. In order for OpenCV to work correctly, I have to set the build to Release in Visual Studio. This way, it works as it should.

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.

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?
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.

OpenCV imwrite params read access violation

A very simple question...why am I getting a read access violation error with this code?
cv::Mat laserSpeckle = Mat::zeros(100,100,CV_8UC1);
imwrite( "C://testimage.jpg", laserSpeckle );
When i attach a debugger and look into it further, it throws the exception at this snippet in grfmt.cpp.
if( params[i] == CV_IMWRITE_JPEG_QUALITY )
{
quality = params[i+1];
quality = MIN(MAX(quality, 0), 100);
}
It occurs with .png and .tiff too. Im an OpenCV newbie, my apologies if this is something really simple. I am using Qt for what its worth.
Do you build OpenCV yourself? If yes, make sure that the option WITH_JPEG is enabled when you configure your build files:
cmake ... -DWITH_JPEG=ON ...
If you want to save image with alpha channel you should use png format. It is described here
It should work with bmp format:
cv::Mat laserSpeckle = cv::Mat::zeros(100,100,CV_8UC1);
cv::imwrite( "C://testimage.bmp", laserSpeckle );
Your code also works on my computer. However, it seems that on some systems it works only for bmp images. I saw similar issues reported here and here.
The problem is with the debugger version (x64) if you build the code using the release version (x64) it works fine for me.
In my case c++ Code Generation settings were wrong
Should have been Multithreaded DEBUG dll MD

OpenCV C++ crashes on close

I'm working on a simple background subtraction program by using OpenCV BackgroundSubtractorMOG2. The codes:
int main()
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat frame,foreground,image,edges;
BackgroundSubtractorMOG2 mog;
namedWindow("Capture", CV_WINDOW_AUTOSIZE);
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
while(1)
{
cap >> frame; // get a new frame from camera
if( frame.empty() )
break;
image=frame.clone();
mog(frame,foreground,-1);
threshold(foreground,foreground,1,250,THRESH_BINARY);
medianBlur(foreground,foreground,9);
erode(foreground,foreground,Mat());
dilate(foreground,foreground,Mat());
imshow( "Capture",image );
imshow("Contours",foreground);
if(waitKey(30) >= 0) break;
}
}
The program works but upon exiting, an error will occur like this:Unhandled exception at 0xfeeefeee in {prog_name}.exe: 0xC0000005: Access violation reading location 0xfeeefeee. and it will point to crtexe.c on line 568:
if (has_cctor == 0)
_cexit();
Any idea what causes the problem? Thanks in advance.
PS: Currently using OpenCV 2.3.4 and VS C++ 2010 Exp. on Windows XP.
PSS: AFAIK each class/pointer including camera will be deinitialized/destroyed upon exit. Correct me if I wrong.
Updates(1): Still got the error, even after adding some lines into it.
Updates(2): Tried DEBUG for every line, still got nothing. Tried delete function to release resources but cannot find any pointer to use it with delete.
Updates(3): It seems like every video processing program I made using OpenCV C++ will crash on exit, even the one I copy-pasted from books/internet. Changed break with return -1, still crashes...
Updates(4): I tried my program on another PC (Ms VS 2010 Pro, Windows Vista 64b) and guess what, NO crash. Definitely something wrong with my computer's setup...

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.