3D视觉(一):双目摄像头的调用

3D视觉(一):双目摄像头的调用

1、计时器 chrono

chrono是C++11新加入的方便时间日期操作的标准库,它既是相应的头文件名称,也是std命名空间下的一个子命名空间,所有时间日期相关定义均在std::chrono命名空间下。

通过这个新的标准库,可以非常方便进行时间日期相关操作。

#include <iostream>
#include<unistd.h> 
#include <chrono>

using namespace std;


int main()
{
  
    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  
//   cout << "begin"<< endl;
//   sleep(2);
//   cout << "end"<< endl;
  
    long int x = 0;
    for(int i=0; i<1000000000; i++)
    {
        x = x + i;
    }
    
    cout << x << endl;
    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "耗时: " << time_used.count() << " 秒. " << endl;

    return 0;
}

2、单目摄像头的调用

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
 
int main()
{
    VideoCapture capture(0);
 
    while (true)
    {
        Mat frame;
        capture >> frame;
        
        imshow("read stream !", frame);
        int c = waitKey(30);
        
        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27)
        if(27 == char(c)) break;
    }
    return 0;
}

3、双目摄像头的调用

调用双目摄像头时遇到一个小问题,编译器报错带宽不够。查询原因后发现,原图分辨率为 480*640 大小,在调用单目时不会出现问题,但对于双目同时调用,USB接口带宽不够,摄像头获取失败。

此时需要手动调小摄像头分辨率,此处我调整成了 240*320 大小,即可成功调用。

#include <opencv2/opencv.hpp>
#include<iostream>

#define CV_CAP_PROP_FRAME_WIDTH  3
#define CV_CAP_PROP_FRAME_HEIGHT  4

using namespace cv;
using namespace std;

int main()

{

    //initialize and allocate memory to load the video stream from camera
    VideoCapture camera0(2);
    camera0.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    camera0.set(CV_CAP_PROP_FRAME_HEIGHT,240);
    
    VideoCapture camera1(0);
    camera1.set(CV_CAP_PROP_FRAME_WIDTH,320);
    camera1.set(CV_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;
        
        Mat r0;
        Mat r1;
        resize(frame0, r0, Size(640, 480), 0, 0, INTER_LINEAR);
        resize(frame1, r1, Size(640, 480), 0, 0, INTER_LINEAR);
        
        imshow("Video00", frame0);
        imshow("Video11", frame1);
        imshow("Video0", r0);
        imshow("Video1", r1);

        // std::cout << frame1.rows() << std::endl;
        // wait for 40 milliseconds
        int c = waitKey(40);

        // exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27)
        if(27 == char(c)) break;

    }

 

    return 0;

}

参考

https://jingyan.baidu.com/article/9158e000392cada25412281a.html
https://blog.csdn.net/qq_32900237/article/details/102392445

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值