views:

530

answers:

2

I have made a program that has two threads, soon to be three, that is trying to get an image from a web cam from both threads at the same time. Is there a way to do this or is there a better way to do this? (using openCV 1.1)

A: 

If you want both threads to be able to work with the image you should write a method that gives you the current image. You can get the image from the cam the first time the method is called and block calls until the image is loaded. Then you can return the buffered image until you want to fetch the next image. This way both threads take the same route to the cam and OpenCV doesn't have to try opening to connections to the camera and if you are only blocking while the image isn't loaded multiple threads can get the image data after it was saved at the same time.

Janusz
+1  A: 

Might I propose just having one of your threads (lets call it the primary one) talk to the camera, get the image and get it all set. Once it is set and placed in a location in memory, you could notify the second thread where that location is (use a mutex in doing this), and both threads could go off and do their processing. Once both threads are done with that image (use a mutex here too), you could have the primary thread get another image and start the process over.

If your camera is producing a lot of images, and you want to start working on them before both threads have finished, you might want to go to three threads...one just to get the image, and the other two just to do the two types of processing. This will get a lot more complicated because you'll need to have multiple buffers. You'll also probably need multiple mutexes and possibly a couple of queues too, its all doable though :-)

teeks99