开发者

基于OpenCV实现视频循环播放

目录
  • 介绍
  • 1.python+OpenCV实http://www.cppcns.com
  • 2.C+++OpenCV实现
  • 补充

介绍

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

有以下三个步骤:

  • 首先设置一个frame的设置参数frame_counter,值为0
  • 在读帧时间,将每次加一
  • 当 frame_counter 达到视频总帧数时,将当前的帧设置为 0

视频总帧数: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(iwww.cppcns.comnt 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视频播放内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜