利用opencv实现按键打开摄像机并且间隔时间连续拍照保存


#include  
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include 
#include 
using namespace std;
using namespace cv;
//保存文件目录
string writePath = "D:\\vs2017project\\ConsoleApplication1\\ConsoleApplication1\\picture\\";
//-----------------------------------【main( )函数】--------------------------------------------
//     描述:控制台应用程序的入口函数,我们的程序从这里开始//--------------------------------------------
int main()
{
	// 接收键盘上的输入	
	char keyCode;
	int cout = 0;
	cv::namedWindow("test", cv::WINDOW_AUTOSIZE);
	if (keyCode = 'a')//按下a,打开摄像头
	{
		cv::VideoCapture cap;	// 读取摄像头	
		cap.open(0);
		// 判断摄像头是否打开	
		if (!cap.isOpened())
		{
			std::cerr << "Could't open capture" << std::endl;
			return -1;
		}


		int i = 0;
		cv::Mat frame;

		// 保存的图片名称	
		std::string imgName;
		//循环显示每帧
		while (1) {
			// 把读取的摄像头传入Mat对象中		
			cap >> frame;
			// 判断是否成功		
			if (frame.empty())
			{
				break;
			}
			// 把每一帧图片表示出来		
			cv::imshow("testcv", frame);
			// 每次循环waitKey()起作用一次		
			waitKey(30);
			if (cout % 300 == 0) //每30帧获取照片
			{
				//按顺序输出照片名称
				imgName = writePath + to_string(i) + ".jpg";
				// 把图片保存起来			
				cv::imwrite(imgName, frame);
				i++;
			}
			cout++;
		}
		cap.release();
	}
	return 0;
}

 

你可能感兴趣的:(opencv)