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

运行结果:

 

posted on 2017-03-06 10:54  我来乔23  阅读(1614)  评论(0编辑  收藏  举报

导航