首页 > 学院 > 开发设计 > 正文

《OpenCV》Part10 OpenCV3.1.0 openCV读取摄像头并保存为.avi视频

2019-11-14 10:06:58
字体:
来源:转载
供稿:网友

《OpenCV》Part10 OpenCV3.1.0 openCV读取摄像头并保存为.avi视频

#include <opencv2/core.hpp>#include <opencv2/opencv.hpp>#include <opencv2/highgui.hpp>#include <opencv2/videoio.hpp>//for camera#include <opencv2/video.hpp>#include <opencv2/imgPRoc//imgproc.hpp>#include <opencv2/ml/ml.hpp>#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/ml/ml.hpp"#include <time.h>#include <ctime>#include <iostream>#include <string>using namespace std;using namespace cv;int main(int argc, char ** argv){	char filename[1024];	if (argc == 1)		sprintf(filename, "%s", "camera.avi");	if (argc == 2)		sprintf(filename, "%s", "123");	VideoCapture capture;	capture.open(0);	if (!capture.isOpened())	{		cout << "Could not initialize capturing.../n" << endl;		return -1;	}	//按时间格式命名	time_t now = time(NULL);////获取1970.1.1至当前秒数time_t	struct tm * timeinfo = localtime(&now);	//创建TimeDate,并转化为当地时间,	//struct tm * timeinfo = gmtime ( &currTime );	 //创建TimeDate,并转化为GM时间,	char path[60];	strftime(path, 60, "%Y_%m_%d_%H_%M_%S", timeinfo);	char strPath[100];	sprintf(strPath, "%s.avi", path);//将创建文件的命令存入cmdchar中	//保存为avi格式视频	Mat frame;	VideoWriter writer;	writer.open(strPath, CV_FOURCC('X', 'V', 'I', 'D'), 25, Size(640, 480), true);//Size(640, 480)//Size(frame.rows, frame.cols)//"cam.avi"	int n = 1;	while (true)	{		capture >> frame;		char* cstr = new char[120];				sprintf(cstr, "%s%d%s", "D://OpenCVWorkSpace//saveCamAsVideo//saveCamAsVideo//savedPic//Pic", n++, ".jpg");		imwrite(cstr, frame);		imshow("Video_Capture", frame);		if (frame.empty())		{			break;		}		writer << frame;		waitKey(3);	}	//return 0;}该程序功能为读取摄像头后将视频存储为avi格式。

其中的waitKey(3)如果没有的话程序容易未响应。CV_FOURCC('P','I','M','1') = MPEG-1 codecCV_FOURCC('M','J','P','G') = motion-jpeg codecCV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codecCV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codecCV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codecCV_FOURCC('U', '2', '6', '3') = H263 codecCV_FOURCC('I', '2', '6', '3') = H263I codecCV_FOURCC('F', 'L', 'V', '1') = FLV1 codec


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表