opencv2读取摄像头并保存为视频
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { VideoCapture cap(0);//读取usb摄像头视频 if(!cap.isOpened()) { cout<<"Capture could not be opened successfully"<<endl; return -1; } Size S=Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),(int)cap.get(CV_CAP_PROP_FRAME_HEIGHT)); VideoWriter put("output.mpg",CV_FOURCC('M','P','E','G'),50,S);//输出保存为视频名为output.mpg;MPEG为对应的编码器。 //VideoWriter put("output.avi",CV_FOURCC('M','P','E','G'),50,S);//可以将视频保存为avi格式的 if(!put.isOpened()) { cout<<"File could not be created for writing. Check permissions"<<endl; return -1; } namedWindow("Video"); while(char(waitKey(1))!='q'&&cap.isOpened()) { Mat frame; cap>>frame; if(frame.empty()) { cout<<"Video over"<<endl; break; } imshow("Video",frame); put<<frame; } return 0; }