zoukankan      html  css  js  c++  java
  • OpenCV学习:播放avi视频文件

    #if 0
    //播放avi视频文件(IplImage)  
    #include <opencv2/opencv.hpp>  
    using namespace std;  
      
    #pragma comment(linker, "/subsystem:"windows" /entry:"mainCRTStartup"")  
      
    int main()  
    {  
        const char *pstrAviFileName = ".\Res\Microsoft_split.avi";  
        const char *pstrWindowsTitle = "OpenCV.avi";  
      
        // 从文件中读取图像
        CvCapture* pCapture = cvCaptureFromFile(pstrAviFileName);
        if (!pCapture)
        {
            cout << "Fail to capture avi file!" << endl;
            return -1;
        }
        IplImage *pImage = NULL; 
    
        //创建窗口 
        cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);  
    
        while(1)
        {
            pImage = cvQueryFrame(pCapture);
            if (!pImage)
            {
                cout << "Fail to query avi frame image!" << endl;
                break;
            }
            //在指定窗口中显示图像  
            cvShowImage(pstrWindowsTitle, pImage);  
            if (cvWaitKey(30) >= 0)
            {
                break;
            }
        }
    
        cvReleaseCapture(&pCapture);
        cvDestroyWindow(pstrWindowsTitle);  
        
        return 0;  
    } 
    #endif
    
    
    #if 1
    //播放avi视频文件(Mat)  
    #include <opencv2/opencv.hpp>  
    using namespace std;  
    using namespace cv;
    
    #pragma comment(linker, "/subsystem:"windows" /entry:"mainCRTStartup"")  
    
    int main()  
    {  
        const char *pstrAviFileName = ".\Res\AviDemo.avi";  
        const char *pstrWindowsTitle = "OpenCV.avi";   
        
        VideoCapture cap(pstrAviFileName);
        //检查是否成功打开 
        if(!cap.isOpened()) 
        { 
            cerr << "Can not open a camera or file." << endl; 
            return -1; 
        }
        Mat im;  
    
        //创建窗口 
        cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);  
        while(1)
        {
            cap >> im;
            if (im.empty())
            {
                break;
            }
            //在指定窗口中显示图像  
            imshow(pstrWindowsTitle, im);  
            if(waitKey(30) >= 0) 
            {
                break; 
            }
        }
        //退出时会自动释放cap中占用资源
    
        return 0;  
    }  
    #endif

    运行结果:

  • 相关阅读:
    PHP图像处理
    PHP文件上传
    PHP文件编程
    PHP面向对象
    【Codeforces Round #694 (Div. 1) B】Strange Definition
    【Codeforces Round #694 (Div. 2) C】Strange Birthday Party
    【Codeforces Round #693 (Div. 3) F】New Year's Puzzle
    【Codeforces Round #693 (Div. 3) G】Moving to the Capital
    【Codeforces Round #695 (Div. 2) E】Distinctive Roots in a Tree
    【Codeforces Round #695 (Div. 2) D】Sum of Paths
  • 原文地址:https://www.cnblogs.com/MakeView660/p/6508906.html
Copyright © 2011-2022 走看看