首頁 > 軟體

基於OpenCV實現視訊迴圈播放

2022-02-25 10:01:17

介紹

本文將介紹基於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(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視訊播放內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com