简体   繁体   English

OpenCV C ++多线程可提高帧率

[英]OpenCV C++ Multi-threading to Improve Framerate

I have an opencv program where the image processing is sensitive to having a stable and relatively high framerate in the video capture. 我有一个opencv程序,在该程序中,图像处理对视频捕获中具有稳定且相对较高的帧速率很敏感。 Unfortunately, all the image processing I do is slowing down the framerate significantly, resulting in erroneous behavior in my program. 不幸的是,我所做的所有图像处理都大大降低了帧速率,导致程序出现错误行为。 I believe that putting the camera on a separate thread and having the image processing happen on its own thread would improve framerate, but I am not sure how to do this. 我相信将相机放在单独的线程上并在其自己的线程上进行图像处理会提高帧速率,但是我不确定如何做到这一点。 Can anyone guide me through the process? 谁能指导我完成整个过程?

UPDATE: After doing some research on threads, I managed to implement threading to where the final video feed post-processed is displayed. 更新:在对线程进行了一些研究之后,我设法实现了线程化,以显示最终经过处理的最终视频提要。 However, somehow my implementation of it is causing the image processing methods to fail (ex. before I could successfully track a moving object, now it is erroneous whether or not it is tracked). 但是,以某种方式实现它会导致图像处理方法失败(例如,在我可以成功跟踪运动对象之前,现在不管是否跟踪它都是错误的)。 I suspect this has something to do with the image processing algorithms not being able to process each frame fast enough as new frames are read in. How can I improve this implementation so that my processing methods worked as they did without multithreading? 我怀疑这与图像处理算法在读取新帧时不能足够快地处理每个帧有关。我如何改进此实现,以使我的处理方法可以像在没有多线程的情况下那样工作?

void CaptureFrames() {
  VideoCapture capture(0);
  if (!capture.isOpened()) {
      cout << "cannot open camera" << endl;
  }

  while (true) {
      //CAMERAFRAME is a global Mat defined at the top of my program
      capture.read(CAMERAFRAME);
      if (waitKey(30) >= 0) { break; }
  }
}

void ProcessFrames() {

  while (true) {



    Mat hsvFrame;
    Mat binMat;
    Mat grayFrame;
    Mat grayBinMat;

    if (!CAMERAFRAME.empty()) {
        //do image processing functions (these worked before implementing threads and are not causing errors)

        imshow("gray binary", grayBinMat);
        imshow("multithread test", CAMERAFRAME);
    }



    if (waitKey(30) >= 0) { break; }
   }
}

int main(int argc, char** argv[]) {
  thread t1(CaptureFrames);
  thread t2(ProcessFrames);

  while(true) {
    if(waitKey(30) >= 0) {break;}
  }

  return 0;
}

Try the older version again but remove this last line from the ProcessFrames function. 再次尝试使用旧版本,但请从ProcessFrames函数中删除最后一行。

if (waitKey(30) >= 0) { break; }

On showing images don't make it wait again for 30 m-seconds, the while loop will be enough 在显示图像时,不要让它再次等待30毫秒,while循环就足够了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM