双目相机拍照
设备一个双目摄像头
使用双目摄像头拍照
#include <iostream>
#include <chrono>
#include<string>
#include<sstream>
#include<stdio.h>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
const char* keys =
{
"{help h usage ? | | print this message}"
"{@video | | Video file, if not defined try to use webcamera}"
};
/*
//分别表示简称,文件来源,文件值和帮助语句
1、构造函数------接收命令行输入的指令
2、get-----获得指定的参数的内容
3、has----在get之前可以先检查是否含有此指令
4、check---在使用这些参数之前,检查是否有解析错误的现象;
4、printMessage---打印该对象包含的信息;
*/
int main(int argc, char **argv) {
/*Mat image;
image = imread("/home/desian/image/11.jpg");
if(image.empty()){
cout <<"no find image" << endl;
return -1;
}
cout << image.cols << endl;
cout <<image.rows<< endl;
namedWindow("1",WINDOW_AUTOSIZE);
imshow("1",image);
waitKey(0);
*/
CommandLineParser parser(argc, argv, keys);
parser.about("Video Capture");
if (parser.has("help")) //帮助信息
{
parser.printMessage();
return 0;
}
String videoFile = parser.get<String>(0);
if (!parser.check())
{
parser.printErrors();
return 0;
}
VideoCapture cap; //定义摄像头对象,准备对每一帧进行处理
if (videoFile != "")
{
cap.open(videoFile); //打开视频流文件
}
else
{
cap.open(1); //打开相机,电脑自带摄像头一般编号为0,外接摄像头编号为1,主要是在设备管理器中查看自己摄像头的编号。
//--------------------------------------------------------------------------------------
cap.set(CAP_PROP_FRAME_WIDTH, 2560); //设置捕获视频的宽度 2560
cap.set(CAP_PROP_FRAME_HEIGHT, 720); //设置捕获视频的高度 720
}
if (!cap.isOpened()) //判断是否成功打开相机
{
cout << "摄像头打开失败!" << endl;
return -1;
}
Mat frame, frame_L,frame_R;
cap >> frame; //从相机捕获一帧图像
Mat grayImage; //用于存放灰度数据
double fScale = 0.5; //定义缩放系数,对2560*720图像进行缩放显示(2560*720图像过大,液晶屏分辨率较小时,需要缩放才可完整显示在屏幕)
Size dsize = Size(frame.cols*fScale, frame.rows*fScale);
Mat imagedst = Mat(dsize, CV_32S);
resize(frame, imagedst, dsize);
char key;
char image_left[200];
char image_right[200];
int count1 = 0;
int count2 = 0;
namedWindow("图片1",1);
namedWindow("图片2",1);
while (1)
{
key = waitKey(50);
cap >> frame; //从相机捕获一帧图像
resize(frame, imagedst, dsize); //对捕捉的图像进行缩放操作
frame_L = imagedst(Rect(0, 0, 640, 360)); //获取缩放后左Camera的图像 640*360
namedWindow("Video_L", 1);
imshow("Video_L", frame_L); //显示左摄像头拍摄的图像
frame_R = imagedst(Rect(640, 0, 640, 360)); //获取缩放后右Camera的图像
namedWindow("Video_R", 2);
imshow("Video_R", frame_R);
if (key == 27) //按下ESC退出
break;
if (key == 32) // 按下空格开始拍照图片保存在工程文件下
{
snprintf(image_left,200, "/home/desian/image/car/image_left_%d.jpg", ++count1);
imwrite(image_left, frame_L);
imshow("图片1", frame_L);
snprintf(image_right, 200,"/home/desian/image/car/image_right_%d.jpg", ++count2);
imwrite(image_right, frame_R);
imshow("图片2", frame_R);
}
}
return 0;