使用OpenCV播放视频

#include <opencv2/opencv.hpp>                                                                                                                                                       
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
  //open the video file for reading                                                                                                                                               
 VideoCapture cap("/home/CORPUSERS/xp025362/Videos/dog.mp4"); 

// if not success, exit program
 if (cap.isOpened() == false)  
 {
     cout << "Cannot open the video file" << endl;
     cin.get(); //wait for any key press
     return -1;
 }

 //Uncomment the following line if you want to start the video in the middle
 //cap.set(CAP_PROP_POS_MSEC, 300); 

 //get the frames rate of the video
 double fps = cap.get(CV_CAP_PROP_FPS); 
 cout << "Frames per seconds : " << fps << endl;

 String window_name = "My First Video";

 namedWindow(window_name, WINDOW_NORMAL); //create a window

 while (true)
 {
     Mat frame;
     bool bSuccess = cap.read(frame); // read a new frame from video 

     //Breaking the while loop at the end of the video
     if (bSuccess == false) 
     {
        cout << "Found the end of the video" << endl;
        break;
     }

     //show the frame in the created window
    imshow(window_name, frame);

    //wait for for 10 ms until any key is pressed.  
    //If the 'Esc' key is pressed, break the while loop.
    //If the any other key is pressed, continue the loop 
    //If any key is not pressed withing 10 ms, continue the loop
    if (waitKey(10) == 27)
     {
        cout << "Esc key is pressed by user. Stoppig the video" << endl;
        break;
    }
}
 return 0;
} 

注意不同version的opencv cv的变量name可能不同,比如上述的CV_CAP_PROP_FPS, 否则会编译error,需要在官网查阅

OpenCV api
我的version是在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值