如何在VideoCapture对象(C ++)中寻找某个frame_no?

问题描述 投票:1回答:1

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

c++ opencv
1个回答
1
投票

你必须使用

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;
        }
© www.soinside.com 2019 - 2024. All rights reserved.