在多线程应用程序中使用opencv waitKey() [英] using opencv waitKey() in a multithreading application

查看:747
本文介绍了在多线程应用程序中使用opencv waitKey()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Qt5.7和OpenNI用C ++编写的多线程应用程序.它具有一个主线程,该主线程启动了第二个线程,该线程从.oni录制文件(华硕xtion pro live)捕获帧进行一些处理,并通过Qt信号槽机制将帧传递给主线程,然后使用. 我想做的是实现一个暂停键,因此按下例如"p"表示处理暂停.我在想这样的事情:

I have a multi-threading application written in C++ with Qt5.7 and OpenNI. It has a main thread that starts a second thread which capture frame from a .oni recording file (asus xtion pro live) does some processing and through the Qt signal-slot mechanism pass the frame to the main thread, which display it using imshow(). What I want to do is to implement a pause key, so pressing for example 'p' the processing pause. I am thinking of something like this:

void Camera::run(){
  while(!cameraStop && this->device.isValid())
    {
      try {
        if (!buttonPause) {
            getFrame();
            process();
            emit sigFrameImageReady(frame);
            if (cv::waitKey(1)==112){
              setButtonPause(!(getButtonPause()));
            }
          }

      }
      catch(std::exception &ex) {
        std::cerr << "getFrame()" << ex.what() << std::endl;
      }
    }
}

以这种方式不起作用,我认为这是因为框架是由另一个线程(主线程)显示的,因此waitKey()在这里只是阻塞了整个过程,但是如果我将其放在主线程中,就在imshow()之后:

In this way it doesn't work, I think that's because the frame is displayed by another thread (the main one), the waitKey() here simply blocks the entire process, but if I put it in the main thread, just after imshow() in this way:

void Process::FrameImageReady(cv::Mat FrameImage)
{
  if (modedebug)
    cv::imshow("bgr", FrameImage);
  if (cv::waitKey(1)==112){
    cam->setButtonPause(!(getButtonPause()));
  }
} 

waitkey似乎被忽略了(图像显示效果很好)..有什么想法吗?

waitkey seems to be ignored (image displaying works fine).. any idea?

编辑 GUI部分仅用于调试目的.

EDIT The GUI part is only for debugging purpose.

推荐答案

您应该在显示线程中实现线程安全FIFO缓冲区或循环缓冲区.来自摄像头线程的信号会将图像推送到此缓冲区,而显示线程将其取出并将它们显示在单独的循环中.只有这样,您才能将相机事件循环与显示线程分开.

You should implement thread safe FIFO bufer or circular buffer in your displaying thread. Signal from the camera thread would be pushing images to this buffer and the displaying thread would be taking them out and display them in a separate loop. Only that way you separate the camera event loop from the display thread.

这篇关于在多线程应用程序中使用opencv waitKey()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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