本文实例为大家分享了Opencv实现双目摄像头拍照程序的具体代码,供大家参考,具体内容如下
我用的双目摄像头是一根usb线接入电脑。运行环境是vc2015,opencv3.0。将左右两个摄像头拍到的图片分别保存起来。
贴出代码(C++)
#include"stdafx.h"
#include<iostream>
#include<string>
#include<sstream>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/videoio.hpp>
#include<opencv2/opencv.hpp>
#include<stdio.h>
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}"
};
int main(int argc, const char** argv) //程序主函数
{
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(0); //打开相机,电脑自带摄像头一般编号为0,外接摄像头编号为1,主要是在设备管理器中查看自己摄像头的编号。
//--------------------------------------------------------------------------------------
cap.set(CV_CAP_PROP_FRAME_WIDTH, 2560); //设置捕获视频的宽度
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 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的图像
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) // 按下空格开始拍照图片保存在工程文件下
{
sprintf_s(image_left, "image_left_%d.jpg", ++count1);
imwrite(image_left, frame_L);
imshow("图片1", frame_L);
sprintf_s(image_right, "image_right_%d.jpg", ++count2);
imwrite(image_right, frame_R);
imshow("图片2", frame_R);
}
}
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)