Using cvQueryFrame and boost :: thread together

I need to call cvQueryFrame (to capture a frame from a webcam with opencv) instead of the stream created with boost. Here is a small code example:

 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 boost::thread , but I get a black screen if I use boost::thread . I don’t understand, maybe someone is doing?

Thank you for your help.

0
source share
3 answers

I saw some problems when OpenCV is executed from a secondary thread, and it is difficult to determine the origin of the problem when the behavior is incompatible on all platforms.

For example, your source code worked fine with OpenCV 2.3.0 on Mac OS X 10.7.2 . I don’t know which platform you use, but the fact that it worked on my computer indicates that there are some problems with the implementation of the platform you are using in OpenCV.

Now, if you cannot move the OpenCV code to the main thread, you can start thinking about creating a second program to handle all the tasks associated with OpenCV, and use some kind of IPC mechanism to allow this program to communicate with your main application.

0
source

I solved the problem by calling

 cvCreateCameraCapture(CV_CAP_ANY); 

in the main thread, even if it does not answer the question:

why doesn't it work? question.

Hope this helps someone else.

0
source

Try calling cv :: startWindowThread (); in the main application, and then create a window in your stream. It worked for me.

0
source

All Articles