OpenCV学习(一) - 调用相机拍照

OpenCV - 调用相机拍照

记录一下 使用OpenCV 调用相机拍照存储功能的实现

单目相机拍照程序

#include <opencv2\imgcodecs.hpp>
#include <opencv2\videoio.hpp>
#include <opencv2\highgui.hpp>

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
/*
   单相机拍照程序
*/
int process(VideoCapture& capture)
{
	int n = 0;//保存相片数
	char filename[200];//保存的图片路径名
	string windowName = "camera_0";//窗口名
	cout << "Enter the space bar to save the picture.\n";
	Mat frame;//定义抓取的帧数据存储矩阵
	for (;;)//无限循环,读取没一帧的数据
	{
		capture >> frame;//将捕获的一帧输入到Mat矩阵中
		if (frame.empty())//如果获得的一帧为空则返回,重新抓取
			break;
		imshow(windowName, frame);//显示相机抓取的窗口
		char key = (char)waitKey(30);//每显示一帧数据的时候停30ms,检测是否按下按键
		switch (key) 
		{
		case 'q':
		case 'Q':
		case 27: // escape
			return 0;
		case ' '://空格键,保存信息
			sprintf(filename, "left%.1d.jpg", n++);
			imwrite(filename, frame);//将抓拍的图片,保存
			cout << "Saved " << filename << endl;
			break;
		default:
			break;
		}
	}
	return 0;
}

int main()
{
	VideoCapture capture(0);//创建相机的视频抓取,0号设备,一般为笔记本的摄像机
	if (!capture.isOpened())//判断相机是否开启
	{
		cerr << "No camera detected.\n";
		return 1;
	}
	return process(capture);
}

双目摄像头拍照存储简单程序

#include <opencv2\imgcodecs.hpp>
#include <opencv2\videoio.hpp>
#include <opencv2\highgui.hpp>

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
/*
   双目相机拍照程序
*/
int process(VideoCapture capture[])
{
	int n = 0;//保存相片数
	char filename[200];//保存的图片路径名
	string windowName = "TWOCam";//窗口名
	cout << "Enter the space bar to save the picture.\n";
	Mat frame[2];//定义抓取的帧数据存储矩阵,两个摄像头的
	for (;;)//无限循环,读取没一帧的数据
	{
		capture[0] >> frame[0];//摄像头0将捕获的一帧输入到Mat[0]矩阵中
		waitKey(20);
		capture[1] >> frame[1];//摄像头1将捕获的一帧输入到Mat[1]矩阵中
		if (frame[0].empty()|| frame[1].empty())//如果获得的一帧为空则返回,重新抓取
			break;
		transpose(frame[0], frame[0]);
		//flip(frame[0], frame[0], 1);
		imshow("Video0", frame[0]);
		//flip(frame[1],);
		transpose(frame[1], frame[1]);
		//flip(frame[1], frame[1], -1);//反转矩阵,摄像头方向问题,借助flip调整
		imshow("Video1", frame[1]);
		char key = (char)waitKey(70);//每显示一帧数据的时候停70ms,检测是否按下按键
		switch (key)
		{
		case 'q':
		case 'Q':
		case 27: // escape
			return 0;
		case ' '://空格键,保存信息
			sprintf(filename, "imageLeft%.1d.jpg", n);
			imwrite(filename, frame[0]);
			cout << "Saved " << filename << endl;
			sprintf(filename, "imageRight%.1d.jpg", n++);
			imwrite(filename, frame[1]);
			cout << "Saved " << filename << endl;
			break;
		default:
			break;
		}
	}
	return 0;
}

int main()
{
	VideoCapture capture[2];
	capture[0].open(0);
	capture[1].open(1);
	if (!capture[0].isOpened())
	{
		cerr << "Failed to open the video device [0].\n";
		return 1;
	}
	if (!capture[1].isOpened())
	{
		cerr << "Failed to open the video device [1].\n";
		return 1;
	}
	return process(capture);
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值