如何使用 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 current_Frame;//Declaring an integer variable to store the position of the current frame//
      current_Frame = cap.get(CAP_PROP_POS_FRAMES);//Reading the position of current frame//
      if (myImage.empty()){ //Breaking the loop if no video frame is detected//
         break;
      }
      cout << "Current Frame Number:" << current_Frame << endl;
      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;
}

此程式將播放影片,並顯示當前幀在控制檯視窗中的位置。

輸出

更新於:2021 年 3 月 10 日

1000+ 瀏覽

開啟你的職業生涯生涯

完成課程認證

開始學習
廣告
© . All rights reserved.