有没有办法在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;
        }

关于c++ - 如何在VideoCapture对象(C++)中寻找某个frame_no?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50703816/

10-10 17:02