1、打开视频文件
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; void processiamge(Mat &frame) { circle(frame, Point(cvRound(frame.cols / 2), cvRound(frame.rows / 2)), 150, Scalar(0, 0, 255), 2, 8); } int main() { string filename = "1.avi";//打开的视频文件 VideoCapture capture; capture.open(filename); double rate = capture.get(CV_CAP_PROP_FPS);//获取视频文件的帧率 int delay = cvRound(1000.000 / rate); if (!capture.isOpened())//判断是否打开视频文件 { return -1; } else { while (true) { Mat frame; capture >> frame;//读出每一帧的图像 if (frame.empty()) break; imshow("处理前视频", frame); processiamge(frame); imshow("处理后视频", frame); waitKey(delay); } } return 0; }2、打开摄像头,采集图片,并保存到视频
主要用到两个类
VideoCapture 打开摄像头 VideoWriter 保存为视频文件
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; void main() { VideoCapture capture(0);//如果是笔记本,0打开的是自带的摄像头,1 打开外接的相机 double rate = 25.0;//视频的帧率 Size videoSize(1280,960); VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), rate, videoSize); Mat frame; while (capture.isOpened()) { capture >> frame; writer << frame; imshow("video", frame); if (waitKey(20) == 27)//27是键盘摁下esc时,计算机接收到的ascii码值 { break; } } }具体参数,可参考 http://blog.csdn.net/yang_xian521/article/details/7440190