OpenCV C ++多线程提高帧率

时间:2016-06-24 16:28:25

标签: c++ multithreading opencv image-processing

我有一个opencv程序,其中图像处理对于在视频捕获中具有稳定且相对高的帧速率敏感。不幸的是,我所做的所有图像处理都显着降低了帧速率,导致我的程序出现错误行为。我相信将相机放在一个单独的线程上并在其自己的线程上进行图像处理会提高帧率,但我不知道如何做到这一点。任何人都可以指导我完成整个过程吗?

更新:在对线程进行一些研究之后,我设法将线程实现到显示后处理的最终视频源的位置。然而,不知何故,我的实现导致图像处理方法失败(例如,在我成功跟踪移动对象之前,现在无论是否跟踪它都是错误的)。我怀疑这与图像处理算法有关,因为在读入新帧时无法足够快地处理每一帧。我如何改进这种实现,以便我的处理方法像没有多线程一样工作?

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

1 个答案:

答案 0 :(得分:0)

再次尝试旧版本,但从Private Sub tryagain() Dim con As ADODB.Connection Dim rs As ADODB.Recordset Set con = New ADODB.Connection With con .Provider = "Microsoft.ACE.OLEDB.12.0" .Open "C:\Users\Ashleysaurus\Desktop" & "\" & "xyzmanu3.accdb" End With con.Execute "Invoice Query" 'How do output to Worksheet? rs.Close cmd.ActiveConnection.Close End Sub 功能中删除最后一行。

ProcessFrames

在显示图像时,不要再等待30秒,while循环就足够了