OpenCV学习:播放avi视频文件

时间:2023-03-08 19:58:39
#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 -;
}
IplImage *pImage = NULL; //创建窗口
cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE); while()
{
pImage = cvQueryFrame(pCapture);
if (!pImage)
{
cout << "Fail to query avi frame image!" << endl;
break;
}
//在指定窗口中显示图像
cvShowImage(pstrWindowsTitle, pImage);
if (cvWaitKey() >= )
{
break;
}
} cvReleaseCapture(&pCapture);
cvDestroyWindow(pstrWindowsTitle); return ;
}
#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 -;
}
Mat im; //创建窗口
cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);
while()
{
cap >> im;
if (im.empty())
{
break;
}
//在指定窗口中显示图像
imshow(pstrWindowsTitle, im);
if(waitKey() >= )
{
break;
}
}
//退出时会自动释放cap中占用资源 return ;
}
#endif

运行结果:

OpenCV学习:播放avi视频文件