如何在使用 C++ 的 OpenCV 中計算經過時間?


我們將在此瞭解如何使用 OpenCV 計算經過時間。

以下程式使用 C++ 在 OpenCV 中計算經過時間。

示例

#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
#include<iostream>
using namespace std;
using namespace cv;
int main() {
   Mat myImage;//Declaring a matrix to load the frames//
   namedWindow("Video Player");//Declaring the video to show the video//
   VideoCapture cap("video.mp4");//Declaring an object to load video from device//
   if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
      cout << "No video stream detected" << endl;
      system("pause");
      return-1;
   }
   while (true){ //Taking an everlasting loop to show the video//
      cap >> myImage;
      int elapsed_time;//Declaring an integer variable to store the elapsed time//
      elapsed_time=cap.get(CAP_PROP_POS_MSEC);//Reading the elapsed time//
      cout << "Ellapsed time(in second):" << elapsed_time / 1000 << endl;//Showing the elapsed time in seconds//
      if (myImage.empty()){ //Breaking the loop if no video frame is detected//
         break;
      }
      imshow("Video Player", myImage);//Showing the video//
      char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop//
         break;
      }
   }
   cap.release();//Releasing the buffer memory//
   return 0;
}

該程式將播放影片,並在控制檯視窗顯示經過時間。

輸出


更新於: 10-Mar-2021

370 次瀏覽

開啟你的 職業

完成本課程並獲得認證

開始
廣告
© . All rights reserved.