vs+opencv打开USB相机拍照并保存

简单实现


#include<opencv2\opencv.hpp>
#include<string>
#include<iostream>
using namespace std;
using namespace cv;

//图片文件保存位置
string writePath = "C:\\Users\\Lenovo\\Desktop\\biaoding\\";
int i = 0;

int main()
{
	VideoCapture camera(0);
	//设置相机参数(.set)   另外(.get)可以获得相机参数
	camera.set(CAP_PROP_FRAME_WIDTH, 640);
	camera.set(CAP_PROP_FRAME_HEIGHT, 480);
	namedWindow("tupian", WINDOW_AUTOSIZE);
	
	if (!camera.isOpened())
	{
		return 0;
	}

	while (1)
	{
		Mat frame;
		camera >> frame;
		if (32 == waitKey(20)) {	//空格拍照,去除即可连续保存
			string name = writePath + to_string(i) + ".jpg";
			imwrite(name, frame);
			cout << name << endl;
			i++;
		}
		if (97 == waitKey(10)) {	//'a'退出
			break;
		}
		imshow("tupian", frame);
	}
}

你可能感兴趣的:(opencv,c++,opencv,c++)