欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于OpenCV實(shí)現(xiàn)視頻循環(huán)播放

 更新時(shí)間:2022年02月25日 08:49:40   作者:求則得之,舍則失之  
這篇文章主要為大家介紹了如何利用OpenCV實(shí)現(xiàn)視頻的循環(huán)播放,本文為大家提供了兩種方式,一個(gè)是利用Python語言實(shí)現(xiàn),一個(gè)是利用C++語言實(shí)現(xiàn),需要的可以參考一下

介紹

本文將介紹基于OpenCV實(shí)現(xiàn)視頻的循環(huán)播放。

有以下三個(gè)步驟:

  • 首先設(shè)置一個(gè)frame的設(shè)置參數(shù)frame_counter,值為0
  • 在讀幀時(shí)間,將每次加一
  • 當(dāng) frame_counter 達(dá)到視頻總幀數(shù)時(shí),將當(dāng)前的幀設(shè)置為 0

視頻總幀數(shù):CAP_PROP_FRAME_COUNT

設(shè)置當(dāng)前的幀:CAP_PROP_POS_FRAMES

VideoCaptureProperties通用屬性標(biāo)識(shí)符參考地址。

1.Python+OpenCV實(shí)現(xiàn)

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實(shí)現(xiàn)

#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;
}

補(bǔ)充

當(dāng)然,OpenCV不僅能實(shí)現(xiàn)視頻的循環(huán)播放,還能實(shí)現(xiàn)視頻的倒放

下面將用C語言實(shí)現(xiàn)視頻的倒放,以下是示例代碼

#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

                                          //用于確定幀數(shù)
    while ((out = cvQueryFrame(capture)) != NULL) {undefined
        k++;
    }
    cvReleaseCapture(&capture);
    capture = cvCreateFileCapture("gr18.avi");
    out = cvQueryFrame(capture);//沒有它J會(huì)多一幀。

    
    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;

            //重復(fù)初始化
            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);
}

到此這篇關(guān)于基于OpenCV實(shí)現(xiàn)視頻循環(huán)播放的文章就介紹到這了,更多相關(guān)OpenCV視頻播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論