采用大恒MER系列工业摄像机,不能用opencv自带函数打开摄像头,需要用厂商提供的API
定义控制台应用程序的入口点。
#include "stdafx.h"
#include "core/core.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <set>
#include<stdio.h>
#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", "camera1.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(nullprt);获取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", "/home/caros/Documents/../", n++, ".jpg");
sprintf(cstr, "%s%d%s", "/home/baidu/Doc/shipin_capture", n++, ".jpg");
imwrite(cstr, frame);
imshow("Video_Capture", frame);
if (frame.empty())
{
break;
}
writer << frame;
waitKey(3);
}
//return 0;
}