您的位置:首页 > 运维架构

opencv 读取、修改、保存视频文件

2016-11-14 10:04 656 查看
用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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: