python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > OpenCV视频播放

基于OpenCV实现视频循环播放

作者:求则得之,舍则失之

这篇文章主要为大家介绍了如何利用OpenCV实现视频的循环播放,本文为大家提供了两种方式,一个是利用Python语言实现,一个是利用C++语言实现,需要的可以参考一下

介绍

本文将介绍基于OpenCV实现视频的循环播放。

有以下三个步骤:

视频总帧数:CAP_PROP_FRAME_COUNT

设置当前的帧:CAP_PROP_POS_FRAMES

VideoCaptureProperties通用属性标识符参考地址

1.Python+OpenCV实现

import cv2

cap = cv2.VideoCapture("001.mp4")
frame_counter = 0

while (cap.isOpened()):
   ret, frame = cap.read()

   frame_counter += 1
   if frame_counter == int(cap.get(cv2.CAP_PROP_FRAME_COUNT)):
      frame_counter = 0
      cap.set(cv2.CAP_PROP_POS_FRAMES, 0)

   cv2.imshow("frame", frame)
   key = cv2.waitKey(1)
   # ESC
   if key == 27:
      break
cap.release()
cv2.destroyAllWindows()

2.C++ + OpenCV实现

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main() {
 
    cv::Mat frame;
    cv::VideoCapture cap("001.mp4");   
      
    int frame_counter = 0;
    while (true)
    {
      cap >> frame;
      if (!frame.data)
      {
         printf("Image not loaded");
         return -1;
      }
      
      frame_counter += 1;
      if (frame_counter == int(cap.get(cv::CAP_PROP_FRAME_COUNT))){
          frame_counter = 0;
          cap.set(cv::CAP_PROP_POS_FRAMES, 0);
      }

      cv::imshow("demo", frame);
      char(key)=(char)cv::waitKey(1);
      if(key==27)
          break;
     }

 
    return 0;
}

补充

当然,OpenCV不仅能实现视频的循环播放,还能实现视频的倒放

下面将用C语言实现视频的倒放,以下是示例代码

#include <stdio.h>
#include<math.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[]) {undefined
    int i = 0, j = 0, k = 0;
    cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);
    CvCapture* capture = 0;
    capture = cvCreateFileCapture("gr18.avi");
    
    if (!capture) {undefined
    return -1;
    }
    
    
    IplImage *out = cvQueryFrame(capture);//Init the video read

                                          //用于确定帧数
    while ((out = cvQueryFrame(capture)) != NULL) {undefined
        k++;
    }
    cvReleaseCapture(&capture);
    capture = cvCreateFileCapture("gr18.avi");
    out = cvQueryFrame(capture);//没有它J会多一帧。

    
    double fps = cvGetCaptureProperty(
        capture,
        CV_CAP_PROP_FPS
        );
    
    CvSize size = cvSize(
        (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
        (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)
        );
    
    CvVideoWriter *writer = cvCreateVideoWriter(
        "gr13.avi",
        CV_FOURCC('M', 'J', 'P', 'G'),
        fps,
        size
        , 1
        );
    IplImage* logpolar_frame = cvCreateImage(
        size,
        IPL_DEPTH_8U,
        3
        );
    //out = cvCloneImage(img);

    int booll = 1;
    while ((out = cvQueryFrame(capture)) != NULL)
    {undefined
        
        i = 0;
    
        j++;;

        booll = 0;
        if (j == k)//用于从第J帧开始写入
        {undefined
            cvShowImage("Example3", out);
            cvWaitKey(1);

            cvWriteFrame(writer, out);
            k--; j = 0;

            //重复初始化
            capture = cvCreateFileCapture("gr18.avi");
            out = cvQueryFrame(capture);
        }
        //j++;


        if (k == 0)break;
    }
    printf("||%d %d", k, j); getchar();
    cvReleaseVideoWriter(&writer);
    cvReleaseImage(&logpolar_frame);
    cvReleaseCapture(&capture);
    //cvReleaseCapture(&capture);
    cvDestroyWindow("Example3");
    

    return(0);
}

到此这篇关于基于OpenCV实现视频循环播放的文章就介绍到这了,更多相关OpenCV视频播放内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文