双目测距之步骤一:单/双目照相

标定前的第一步,写了一个小程序来捕获摄像头的图片。每个摄像头都有畸变参数,通过标定摄像头来去取畸变。

(1)单目照相

#include "opencv2/imgproc/imgproc.hpp"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
char filename[100];            //声明一个字符型数组,用来存放图片命名
int main(int argc, char** argv[])
{
    IplImage *frame;                //声明一个图像指针,用来存放下一帧图像
    CvCapture *capture;                //声明一个视频抽象接口
    //capture = cvCreateFileCapture("1.avi");//加载本地视频
    capture = cvCreateCameraCapture(0);//打开摄像头
    cvNamedWindow("capture");        //声明一个播放视频窗口
    int frames = (int)cvGetCaptureProperty(
        capture,
        CV_CAP_PROP_FRAME_COUNT
        );                            //获取视频总帧数
    int i = 0;
    while (1)
    {
        frame = cvQueryFrame(capture);//获取视频下一帧
        if (!frame)                    //如果读取识别则停止
            break;
        cvShowImage("capture", frame);
        char c = cvWaitKey(33);
        if (c == 's')
        {
            sprintf(filename, "%s%d%s", "frame", i++, ".jpg");//保存的图片名
            cvSaveImage(filename, frame);
            cout << "Capture the pictures successfully!" << endl;
        }
        if (c == 27)
            break;
    }
    return 0;
}

(2)双目照相

#include "opencv2/imgproc/imgproc.hpp"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
char filename1[100];            //声明一个字符型数组,用来存放图片命名
char filename2[100];
int main(int argc, char** argv[])
{
    IplImage *frame1;                //声明一个图像指针,用来存放下一帧图像
    IplImage *frame2;
    CvCapture *capture1;                //声明一个视频抽象接口
    CvCapture *capture2;
    capture1 = cvCreateCameraCapture(0);//打开摄像头
    capture2 = cvCreateCameraCapture(1);
    cvNamedWindow("capture1");        //声明一个播放视频窗口
    cvNamedWindow("capture2");
    int frames1 = (int)cvGetCaptureProperty(
        capture1,
        CV_CAP_PROP_FRAME_COUNT
        );                            //获取视频总帧数
    int frames2 = (int)cvGetCaptureProperty(
        capture2,
        CV_CAP_PROP_FRAME_COUNT
        );
    int i = 0,j=0;
    cout << "Please press 's' to save the left and right cameras' pictures." << endl;
    while (1)
    {
        frame1 = cvQueryFrame(capture1);//获取视频下一帧
        frame2 = cvQueryFrame(capture2);
        if (!(frame1||frame2))                    //如果读取识别则停止
            break;
        cvShowImage("capture1", frame1);
        cvShowImage("capture2", frame2);
        char c = cvWaitKey(33);
        if (c == 's')
        {
            sprintf(filename1, "%s%d%s", "left", i++, ".jpg");//保存的图片名
            sprintf(filename2, "%s%d%s", "right", j++, ".jpg");
            cvSaveImage(filename1, frame1);
            cvSaveImage(filename2, frame2);
               cout << "Capture the pictures successfully!" << endl;
        }
        if (c == 27)
            break;
    }   
     cvReleaseCapture(&capture1);
    cvReleaseCapture(&capture2);
    cvDestroyWindow("capture1");
    cvDestroyWindow("capture2");
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值