如何在视频捕获对象(C++)中查找某个frame_no?

How to seek to a certain frame_no in VideoCapture object (C++)?

本文关键字:查找 frame no C++ 视频 对象      更新时间:2023-10-16

有没有办法在VideoCapture对象中寻找某个索引? 我想在特定位置开始阅读流。

你必须使用

cap.set(CV_CAP_PROP_POS_FRAMES, index);

我下面的代码做你的目标

#include <iostream> // for standard I/O
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O
using namespace std;
using namespace cv;
int main()
{
string sourceVideoPath;
sourceVideoPath = "C:\opencvVid\video.mp4";
VideoCapture cap(sourceVideoPath);
Mat frame;
char c;
cout << "Stream frame numbre : " << cap.get(CV_CAP_PROP_FRAME_COUNT)<<endl;
if (!cap.isOpened())
{
cout  << "Could not open reference " << sourceVideoPath << endl;
return -1;
}
int seek_step = 750 ; // start at frame 750
int frameNum = 0;
cap.set(CV_CAP_PROP_POS_FRAMES, seek_step );
while (1){
cap >> frame;
if (frame.empty())
{
cout << " < < <  Channel stream ended!  > > > ";
break;
}
cv::putText(frame, "Frame "+to_string(frameNum+seek_step),
cvPoint(30,30), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(0,0,255), 1, CV_AA);
imshow("frameNum", frame );
c = (char)cvWaitKey(22);
if (c == 27) break;
++frameNum;
}
return 0;
}