在第二个线程中显示图像,OpenCV? [英] Display image in second thread, OpenCV?

查看:803
本文介绍了在第二个线程中显示图像,OpenCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,从250帧/秒的高速帧图像获取图像。

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

同时,我希望用户能够正在进行。用户仅需要以大约30fps(或更小)的速率观看图像。如何设置第二个线程,每隔一段时间显示当前帧?

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 **/
}

四核英特尔机器使用MinGW,gcc和OpenCV 1.1。主要的标准是显示线程必须离我的主处理循环尽可能少的时间。每一毫秒计数。

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.

我尝试使用 CreateThread()创建一个新线程 cvShowImage() cvWaitKey()显然这些函数不是线程安全的

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

我正在考虑使用OpenMP,但有些人报告OpenMP和OpenCV的问题。我也考虑试图使用DirectX directDraw,因为显然它是非常快。但它看起来复杂,显然有使用带有MinGw的Windows DLL的问题

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?

推荐答案

好的。所以尴尬我的问题也是它自己的答案。

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

使用 CreateThread() CvShowImage() CvWaitKey()如我的问题中所描述的实际上是有效的 - 与网上的一些发布,否则建议相反。

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!

这篇关于在第二个线程中显示图像,OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆