用VideoCapture 读取avi视频文件
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture capture("myvideo.avi");
if (!capture.isOpened())
{
cout << "Failed to open the avi file!\n" << endl;
return 1;
}
Mat image;
namedWindow("my Video player", 1);
for(;;)
{
capture>> frame;
if(frame.empty())
{
cout << "End of frame" << endl;
break;
}
imshow("my Video player", frame);
waitKey(0);
}
return 0;
}
用VideoWriter对视频文件进行修改
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
void main(int agrc, char** argv)
{
VideoCapture capture(0); // 打开摄像头
VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480));
Mat frame;
while (capture.isOpened())
{
capture>> frame;
writer<< frame;
imshow("video", frame);
if (cvWaitKey(20) == 27)
{
break;
}
}
}
获取总帧数
int totalFrameNum = capture.get(CV_CAP_PROP_FRAME_COUNT);
获取帧率
double rate = capture.get(CV_CAP_PROP_FPS);