Display image in second thread, OpenCV?

后端 未结 4 1384
鱼传尺愫
鱼传尺愫 2021-02-05 15:46

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();
  DoPr         


        
相关标签:
4条回答
  • 2021-02-05 16:28

    I'm not sure why this happens, but I've added a cvWaitKey after every cvShowImage and the picture was displayed properly.

    cvShowImage(myImage);
    cvWaitKey(1);
    
    0 讨论(0)
  • 2021-02-05 16:30

    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.

    0 讨论(0)
  • 2021-02-05 16:37

    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!

    0 讨论(0)
  • 2021-02-05 16:38

    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!

    0 讨论(0)
提交回复
热议问题