OpenCV双目相机拍照及图片分割

最近在做双目相机测距及三维重建,我从tb买了一个双目相机,第一步需要通过棋盘图来对双目相机进行标定,由于双目相机拍出来的左右相机的图片是一张图,需要进行分割。

#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;

//双目摄像头支持2560x720, 1280x480,640x240
#define FRAME_WIDTH    1280
#define FRAME_HEIGHT   480

const char* keys =
{
	"{help h usage ? | | print this message}"
	"{@video | | Video file, if not defined try to use webcamera}"
};

int main(int argc, 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(1);  //0-笔记本自带摄像头,1-外接usb双目摄像头
		cap.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);  //设置捕获视频的宽度
		cap.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);  //设置捕获视频的高度
	}

	if (!cap.isOpened())
	{
		cout << "摄像头打开失败!" << endl;
		return -1;
	}

	Mat frame, frame_L, frame_R;
	
	cap >> frame;         //从相机捕获一帧

	Mat grayImage;
	
	double fScale = 1.;
	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;
	int count = 0;
	namedWindow("图片1", 1);
	namedWindow("图片2", 1);

	while(1)
	{
		key = waitKey(50);
		cap >> frame;
		count++;

		resize(frame, imagedst, dsize);
		
		frame_L = imagedst(Rect(0, 0, FRAME_WIDTH/2, FRAME_HEIGHT));
		namedWindow("Video_L", 1);
		imshow("Video_L", frame_L);

		frame_R = imagedst(Rect(FRAME_WIDTH/2, 0, FRAME_WIDTH/2, FRAME_HEIGHT));
		namedWindow("Video_R", 1);
		imshow("Video_R", frame_R);
		
		if (key == 27)
			break;
		
		if (key == 32)            //使用空格键拍照
		//if (0 == (count % 100))   //每5秒定时拍照
		{
			snprintf(image_left, sizeof(image_left), "left%02d.jpg", ++count1);
			imwrite(image_left, frame_L);
			imshow("图片1", frame_L);

			snprintf(image_right, sizeof(image_right), "right%02d.jpg", ++count2);
			imwrite(image_right, frame_R);
			imshow("图片2", frame_R);
		}
		
	}

	cap.release();

	return 0;
}

CMakeLists文件如下:

cmake_minimum_required(VERSION 2.8)
project( demo )
set(OpenCV_DIR /usr/local/opencv3/share/OpenCV)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
aux_source_directory(. DIR_SRCS)
add_executable(demo ${DIR_SRCS})
target_link_libraries( demo ${OpenCV_LIBS} )

编译:

cmake .
make

这样就能得到左右相机分开的照片,可根据需要用空格键拍摄或者定时拍摄,做为标定用的棋盘图一般拍摄20-30张左右。

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值