如何使用opencv进行两个摄像头的同时拍摄

主体代码在visual studio中安装完opencv就可以跑了,可能会报错,根据报错内容改就行;
辅助代码中有设置图像分辨率的代码

主体代码
辅助代码

主体代码:


#include <iostream>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
 
int main()
{
 
    cv::VideoCapture capl(0);
    cv::VideoCapture capr(1);
 
    int i = 0;
 
    cv::Mat src_imgl;
    cv::Mat src_imgr;
 
    char filename_l[15];
    char filename_r[15];
    while(capl.read(src_imgl) && capr.read(src_imgr))
    {
 
 
        
        cv::imshow("src_imgl", src_imgl);
        cv::imshow("src_imgr", src_imgr);
	
        char c = cv::waitKey(1);
        if(c==‘ ’) //按空格采集图像,这里必须是单引号,不能是双引号
        {
	    sprintf(filename_l, "img%d.jpg",i);
            imwrite(filename_l, src_imgl);
	    sprintf(filename_r, "right%d.jpg",i++);
            imwrite(filename_r, src_imgr);
        }
        if(c==‘q’ || c=='Q') // 按q退出,这里必须是单引号,不能是双引号
        {
            break;
        }
	  
 
    }
 
    return 0;
}

辅助代码:

#include <opencv2/opencv.hpp>
#include<iostream>
 
using namespace cv;
using namespace std;
 
int main()
{
 
    //initialize and allocate memory to load the video stream from camera
    VideoCapture camera0(1);
    camera0.set(CAP_PROP_FRAME_WIDTH,320);//若无namespace cv,那得写成CV_CAP_PROP_FRAME_WIDTH
    camera0.set(CAP_PROP_FRAME_HEIGHT,240);
 
    VideoCapture camera1(0);
    camera1.set(CAP_PROP_FRAME_WIDTH,320);
    camera1.set(CAP_PROP_FRAME_HEIGHT,240);
 
 
    if( !camera0.isOpened() ) return 1;
    if( !camera1.isOpened() ) return 1;
 
 
 
    while(true) {
 
        //grab and retrieve each frames of the video sequentially
        Mat3b frame0;
        camera0 >> frame0;
 
        Mat3b frame1;
        camera1 >> frame1;
 
 
 
        imshow("Video0", frame0);
        imshow("Video1", frame1);
        //std::cout << frame1.rows() << std::endl;
        //wait for 40 milliseconds
 
        int c = waitKey(20);
        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27)
        if(27 == char(c)) break;
 
    }
 
 
    return 0;
 
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现效果:http://v.youku.com/v_show/id_XMTU2Mzk0NjU3Ng==.html 如何在你的电脑上运行这个程序? 1,它需要cvblobslib这一个opencv的扩展库来实现检测物体与给物体画框的功能,具体安装信息请见: http://dsynflo.blogspot.com/2010/02/cvblobskib-with-opencv-installation.html,当你配置好cvblobslib之后,你可以用这一的程序进行测试:http://dl.dropbox.com/u/110310945/Blobs%20test.rar 2,视频中两个摄像头之间的距离是6cm,你可以根据你摄像头的型号,来选择合适的距离来达到最好的效果。 3,在进行测距之前,首先需要对摄像头进行标定,那么如何标定呢? 在stdafx.h中把"#define CALIBRATION 0"改成 “#define CALIBRATION 1”表示进行标定,标定之后,你就可以在工程目录下的"CalibFile" 文件夹中得到标定信息的文件。如果标定效果还不错,你就可以吧"#define CALIBRATION " 改成0,以后就不需要再标定,直接使用上一次的标定信息。你还需要把"#define ANALYSIS_MODE 1"这行代码放到stdafx.h中。 4,视频中使用的是10*7的棋牌格,共摄录40帧来计算摄像头的各种参数,如果你像使用其他棋盘格,可以在 "StereoFunctions.cpp"文件中修改相应参数。 5,如果你无法打摄像头,可以在 "StereoGrabber.cpp"文件中修改代码“cvCaptureFromCAM(index)”中index的值。 6,About computing distance: it interpolates the relationship between depth-value and real-distance to third degree polynomial. So i used excel file "interpolation" for interpolation to find k1 to k4, you should find your own value of these parameters. 7,你可以通过调整控制窗口中各个参数的滑块,从而来得到更好的视差图。 8,在目录下的”distance“文件夹中,有计算距离信息的matlab代码。 9,如果你想了解基本的理论,可以看一下这个文档:http://scholar.lib.vt.edu/theses/available/etd-12232009-222118/unrestricted/Short_NJ_T_2009.pdf 视频中环境:vs2008,opencv2.1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值