Display image in second thread, OpenCV? Display image in second thread, OpenCV? multithreading multithreading

Display image in second thread, OpenCV?


Ok. So embarrassingly my question is also its own answer.

Using CreateThread(), CvShowImage() and CvWaitKey() as described in my question actually works-- contrary to some postings on the web which suggest otherwise.

In any event, I implemented something like this:

/** Global Variables **/bool DispThreadHasFinished;bool MainThreadHasFinished;iplImage* myImg;/** Main Loop that loops at >100fps **/main() {  DispThreadHasFinished = FALSE;  MainThreadHasFinished = FALSE;  CreateThread(..,..,Thread,..);  while( IsTheUserDone() ) {    myImg=AcquireFrame();    DoProcessing();    TakeAction();  }  MainThreadHasFinished = TRUE;  while ( !DisplayThreadHasFinished ) {     CvWaitKey(100);  }  return;}/** Thread that displays image at ~30fps **/Thread() {  while ( !MainThreadHasFinished ) {    cvShowImage(myImg);    cvWaitKey(30);  }DispThreadHasFinished=TRUE;return;}

When I originally posted this question, my code was failing for unrelated reasons. I hope this helps!


Since the frame grabbing doesn't need to use the UI, I'd set up a secondary thread to handle the frame grabbing, and have the original thread that handles the UI display the sample frames. If you tried to display the frame currently be grabbed, you'd have to lock the data (which is generally fairly slow). To avoid that, I'd display a frame one (or possibly two) "behind" the one currently being grabbed, so there's no contention between grabbing and displaying the data. You'll still have to ensure that incrementing the current frame number is thread-safe, but that's pretty simple -- use InterlockedIncrement in the capture thread.


I'm sorry I can't give you a better answer right now, but it seems that your question is not about the structure of your program but rather about the tool you should use to implement multithreading. For this I would recommend Qt. I have been using Qt for a while but I'm just now getting into multithreading.

It seems to me that your best bet might be a QReadWriteLock. This allows you to read from an image but the reader thread will give up its lock when the writer thread comes along. In this case you could keep a copy of the image you last displayed and display it if the image is locked for writing.

Sorry again that I can't be more detailed but, like I said, I'm just getting into this as well. I'm basically trying to do the same thing that you are, but not nearly as fast :). Good luck!