Reverse video playback in OpenCV

后端 未结 3 1047
慢半拍i
慢半拍i 2020-12-09 17:46

Is it possible to play videos backwards in OpenCV? Either by an API call or by buffering video frames and reversing the order into a new video file.

Thanks

3条回答
  •  抹茶落季
    2020-12-09 18:01

    Yes, it's possible. See code with comments:

    //create videocapture
    VideoCapture cap("video.avi");
    
    //seek to the end of file
    cap.set(CV_CAP_PROP_POS_AVI_RATIO, 1);
    
    //count frames
    int number_of_frames = cap.get(CV_CAP_PROP_POS_FRAMES);
    
    //create Mat frame and window to display
    Mat frame;
    namedWindow("window");
    
    //main loop
    while (number_of_frames > 0)
    {
        //decrease frames and move to needed frame in file
        number_of_frames--;
        cap.set(CV_CAP_PROP_POS_FRAMES, number_of_frames);
    
        //grab frame and display it
        cap >> frame;
        imshow("window", frame);
    
        //wait for displaying
        if (waitKey(30) >= 0)
        {
            break;
        }
    }
    

    Also read this article.

提交回复
热议问题