OpenCV C++ crashes on close - c++

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

Related

Access violation when opening OpenCV VideoCapture (release only)

I have an application written in visual studio, utilizing OpenCV 2.4.10. The program executes flawlessly in debug mode, but in release mode it crashes on the first command following a VideoCapture::open() call, regardless of whether the call was completed in the constructor or with the method call. I recognize that this is a pretty common occurrence, and that a dll linkage may be the issue, but have been unable to fully identify the problem. Any assistance is greatly appreciated.
Here is the error message:
Unhandled exception at 0x03DE7D43 (opencv_ffmpeg2410.dll) in OpenCVLaserDot.exe: 0xC0000005: Access violation reading location 0x00000000.
Here is the segment of code surrounding the VideoCapture instantiation:
const string DEFAULT_INPUT_FILE = "C:/Users/Tyler/Documents/Visual Studio 2013/Projects/OpenCVLaserDot/Release/a.avi";
VideoCapture cap;
if (inputFile == "1" || inputFile == ""){
cap.open(DEFAULT_INPUT_FILE);
} else {
cap.open(inputFile);
}
if (!cap.isOpened()){
printf("Failed to open video\n");
return -1;
}

opencv :: Multiple unwanted window with Garbage name

i am using opencv to capture video from webcam and display it in namedWindow.
cv::Mat rawImage;
cv::VideoCapture captureDevice;
captureDevice.open(0);
cv::namedWindow("webcam", 1);
bool running = true;
while(running)
{
captureDevice >> rawImage;
if(!rawImage.data)
{
continue;
}
cv::imshow("webcam", rawImage);
char ch = cv::waitKey(33);
if(ch == 'e')
running = false;
}
initially code runs fine but after random (5 sec approx) period older named window freezes and new window with some garbage name pops up and start showing webcam images. this continues to happen and i am getting multiple unwanted named window. What is the reason for popping up for those unwanted windows?
Have you tried the solution described here?
http://www.ridgesolutions.ie/index.php/2013/09/26/opencv-display-window-title-corrupted-and-multiple-windows-show/
They also indicate that they don't understand why, but that it was manually fixed by adding preprocessor definition _ITERATOR_DEBUG_LEVEL=0 to VS2012
Had the same issue with opencv 2410 and visual studio 2013 , window 7 64 bit.
Resolved when I linked debug libraries for debug configuration.
e.g. opencv_highgui2410d.lib
No waitkey() is existed in OpenCV, only waitKey() (maybe you have a typo here). Anyway, try to change
char ch = cv::waitkey(33);
to
char ch = cv::waitKey(33);
and test again. I have tested it on my PC (under VS2010), it worked fine.

Problems using cv::split

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.

Function call causes C++ program to freeze unless stepped-through in debugger

I have this short C++ program which takes snapshot images from a camera in a loop and displays them:
void GenericPGRTest::execute()
{
// connect camera
Camera *cam = Camera::Connect();
// query resolution and create view window
const Resolution res = cam->GetResolution();
cv::namedWindow("View");
c = 0;
// keep taking snapshots until escape hit
while (c != 27)
{
const uchar *buf = cam->SnapshotMono();
// create image from buffer and display it
cv::Mat image(res.height, res.width, CV_8UC1, (void*)buf);
cv::imshow("Camera", image);
c = cv::waitKey(1000);
}
}
This uses a class (Camera) for camera control I created using the Point Grey SDK and functions from the OpenCV library to display the images. I'm not necessarily looking for answers relating to the usage of either of these libraries, but rather some insight on how to debug a bizarre problem in general. The problem is that the application freezes (not crashes) on the cam->SnapshotMono() line. Of course, I ran through the function with a debugger. Here is the contents:
const uchar* Camera::SnapshotMono()
{
cam_.StartCapture();
// get a frame
Image image;
cam_.RetrieveBuffer(&image);
cam_.StopCapture();
grey_buffer_.DeepCopy(&image);
return grey_buffer_.GetData();
}
Now, every time I step through the function in the debugger, everything works OK. But the first time I do a "step over" instead of "step into" SnapshotMono(), bam, the program freezes. When I pause it at that time, I notice that it's stuck inside SnapshotMono() at the RetrieveBuffer() line. I know it's a blocking call so it theoretically can freeze (no idea why but it's possible), but why does it block when running normally and not when being debugged? This is one of the weirdest kinds of behaviour under debugging I've seen so far. Any idea why this could happen?
For those familiar with FlyCapture, the code above doesn't break as is, but rather only when I use StartCapture() in callback mode, then terminate it with StopCapture() before it.
Compiled with MSVC2010, OpenCV 2.4.5 and PGR FlyCapture 2.4R10.
Wild guess ... but may it be that StartCapture already starts the process that
ends up with having the buffer in ìmage, and if you step you leave it some
time until you get to RetrieveBuffer. That's not the case if you run it all at once ...

Using cvQueryFrame and boost::thread together

I need to call cvQueryFrame (to capture a frame from a webcam with opencv) instead a thread created with boost. Here is a little example code:
void testCVfunc(){
IplImage* frame;
CvCapture *capture;
capture = cvCreateCameraCapture(CV_CAP_ANY);
if(!capture){
exit(1);
}
frame = cvQueryFrame(capture);
cvNamedWindow("testCV", 1);
while(frame = cvQueryFrame(capture)){
if(!frame){
exit(2);
}
cvShowImage("testCV", frame);
cvWaitKey(1);
}
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
}
int main(){
//Method 1: without boost::thread, works fine
testCVfunc();
//Method 2: with boost::thread, show black screen
char entree;
boost::thread threadTestCV = boost::thread(&testCVfunc);
std::cin >> entree;
}
As the comments say, testCVfunc does its job if I don't call it from a boost::thread, but I get a black screen if I use boost::thread.
I don't get the problem, maybe someone does?
Thank you for your help.
I've seen some problems when OpenCV is being executed from a secondary thread and it's difficult to pinpoint the origin of the problem when the behavior is not consistent on all platforms.
For instance, your source code worked perfectly with OpenCV 2.3.0 on Mac OS X 10.7.2. I don't know what platform you are using, but the fact that it worked on my computer indicates that OpenCV has some implementation issues with the platform you are using.
Now, if you can't move OpenCV's code to the primary thread, then you might want to start thinking about creating a 2nd program to handle all OpenCV related tasks, and use some sort of IPC mechanism to allow this program to communicate with your main application.
I solved the problem by calling
cvCreateCameraCapture(CV_CAP_ANY);
in the main thread, even if it doesn't really answer the question:
why is this not working? question.
Hope this can help someone else.
Try calling cv::startWindowThread(); in the main app and then creating a window within your thread. This worked for me.