Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image in second thread, OpenCV?

I have a loop to take in images from a high speed framegrabbger at 250fps.

/** Loop processes 250 video frames per second **/
while(1){
  AcquireFrame();
  DoProcessing();
  TakeAction();
}

At the same time, I would like the user to be able to monitor what is going on. The user only needs to see images at around 30 fps (or less). How do I set up a second thread that displays the current frame every so often?

Thread(){
  cvShowImage();
  Wait(30); /** Wait for 30 ms **/
}

I am on Windows on a quad core Intel machine using MinGW, gcc and OpenCV 1.1. The main criteria is that the display thread must take as little time away from my main processing loop as possible. Every millisecond counts.

I have tried using CreateThread() to create a new thread with cvShowImage() and cvWaitKey() but apparently those functions are not threadsafe.

I am considering using OpenMP, but some people report problems with OpenMP and OpenCV. I also am considering trying to use DirectX directDraw because apparently it is very fast. but it looks complicated and evidentally there are problems using Windows DLL's with MinGw.

Which of these avenues would be the best place to start?

like image 216
AndyL Avatar asked Jan 12 '10 16:01

AndyL


1 Answers

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!

like image 155
AndyL Avatar answered Sep 18 '22 03:09

AndyL