需要配置好opencv环境变量
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(){
//读取视频
cv::VideoCapture capture("/home/nx/Pictures/input.mp4");
long totalFrameNumber = capture.get(CAP_PROP_FRAME_COUNT);
cout<<"整个视频共"<<totalFrameNumber<<"帧"<<endl;
capture.set( CAP_PROP_POS_FRAMES,0);
//获取帧率
double rate = capture.get(CAP_PROP_FPS);
cout<<"帧率为:"<<rate<<endl;
int delay = 1000/rate;
cv::Mat frame;
if(!capture.read(frame))
{
cout<<"读取视频失败"<<endl;
return -1;
}
cout<<"图片宽width为:"<<frame.rows<<endl;
cout<<"图片高height为:"<<frame.cols<<endl;
int isColor = 1;
int fps = rate;
int frameWidth = frame.rows;
int frameHeight = frame.cols;
cv::VideoWriter writer("output.avi", VideoWriter::fourcc('M','J','P','G'), fps, Size(frameWidth, frameHeight), isColor);
for (;;)
{
frame = frame.clone(); //对图片进行处理
writer.write(frame);
imshow("frame",frame);
waitKey(delay);//因为图像处理需要消耗一定时间,所以图片展示速度比保存视频要慢
//读取下一帧
if(!capture.read(frame))
{
cout<<"读取视频失败"<<endl;
return -1;
}
}
capture.release();
waitKey(0);
return 0;
}