连接工业相机,拍了几张照片!!
主要用到的函数接口:
1、CV_WRAP VideoCapture();
CV_WRAP VideoCapture(const string& filename);
CV_WRAP VideoCapture(int device);**
2、Mat imread( const string& filename, int flags=1 );
3、bool imwrite( const string& filename, InputArray img, const vector& params=vector());
代码如下:
#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace cv;
string inttostring(int number){
stringstream ss;
ss << number;
return ss.str();
}
int _tmain(int argc, _TCHAR* argv[])
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "无法打开摄像头" << endl;
system("pause");
return -1;
}
char* windowName = "Picture demo";
char filename[50];
//文件名序号启示
int n = 0;
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;//图像
//从摄像头读取帧(frame)
bool bSuccess = cap.read(frame);
if (!bSuccess)//检查是否读取成功
{
cout << "无法从摄像头读取帧" << endl;
break;
}
imshow(windowName, frame);
//等待10ms是否按键
switch (waitKey(10))
{
case 27://按下Esc 退出
return 0;
break;
case 13://按下enter保存图片
sprintf(filename, "D:\\pic\\process\\VideoFrame%d.jpg", n++);
imwrite(filename, frame);
break;
}
}
system("pause");
return 0;
}