天天看點

opencv3計算攝像頭内參

ORB-SLAM2跑單目SLAM需要設定相機參數到yaml檔案,這裡來說下如何利用opencv進行相機參數設定。

環境

Win10,VS2015+opencv3.3.1

對錄影機進行标定可以使用直接使用攝像頭也可以事先拍好照片。利用自己攝像頭批量拍攝圖檔見:[opencv3調用筆記本攝像頭批量拍攝圖檔]。(http://blog.csdn.net/xiangxianghehe/article/details/78807480)本文采用的是事先拍好照片,這裡的照片采用的是openCV3.3.1源碼自帶的圖檔。圖檔位置在

E:\opencv331\opencv\sources\samples\data

目錄下,見下圖所示:

opencv3計算攝像頭内參

圖檔包括:

然後把

E:\opencv331\opencv\sources\samples\cpp

目錄下的

imagelist_creator.cpp

複制到vs2015,生成相應的

imagelist_creator.exe

程式。

/*this creates a yaml or xml list of files from the command line args
*/
//imagelist_creator.cpp

#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

using namespace cv;

static void help(char** av)
{
    cout << "\nThis creates a yaml or xml list of files from the command line args\n"
        "usage:\n./" << av[] << " imagelist.yaml *.png\n"
        << "Try using different extensions.(e.g. yaml yml xml xml.gz etc...)\n"
        << "This will serialize this list of images or whatever with opencv's FileStorage framework" << endl;
}

int main(int ac, char** av)
{
    cv::CommandLineParser parser(ac, av, "{help h||}{@output||}");
    if (parser.has("help"))
    {
        help(av);
        return ;
    }
    string outputname = parser.get<string>("@output");

    if (outputname.empty())
    {
        help(av);
        return ;
    }

    Mat m = imread(outputname); //check if the output is an image - prevent overwrites!
    if (!m.empty()) {
        std::cerr << "fail! Please specify an output file, don't want to overwrite you images!" << endl;
        help(av);
        return ;
    }

    FileStorage fs(outputname, FileStorage::WRITE);
    fs << "images" << "[";
    for (int i = ; i < ac; i++) {
        fs << string(av[i]);
    }
    fs << "]";
    return ;
}
           

打開cmd,輸入

imagelist_creator.exe

的目錄,把上面的所有圖檔也複制到該目錄下,執行:

最後會在該目錄下生成一個

imglist.yaml

文本。

接下來移除

imagelist_creator.cpp

添加

calibration.cpp

(位置也在

E:\opencv331\opencv\sources\samples\cpp

),在VS2015中生成相應的

calibration.exe

程式。

然後把

calibration.exe

和上面那些圖檔放到一塊,執行:

calibration imglist.yaml left01.jpg left02.jpg left03.jpg left04.jpg left05.jpg left06.jpgleft07.jpg left08.jpg left09.jpg  left11.jpg left12.jpg left13.jpgleft14.jpg right01.jpg right02.jpg right03.jpg right04.jpg right05.jpgright06.jpg right07.jpg right08.jpg right09.jpg right11.jpg right12.jpg right13.jpg right14.jpg -w= -h=
           

就會列印出:

RMS error reported by calibrateCamera: 
Calibration succeeded. avg reprojection error = 
           

還會在同一目錄生成一個

out_camera_data.yml

檔案,内容如下:

%YAML:
---
calibration_time: "Fri Dec 15 09:19:01 2017"
image_width: 
image_height: 
board_width: 
board_height: 
square_size: 
aspectRatio: 
flags: 
camera_matrix: !!opencv-matrix
   rows: 
   cols: 
   dt: d
   data: [ , , , ,
       , , , ,  ]
distortion_coefficients: !!opencv-matrix
   rows: 
   cols: 
   dt: d
   data: [ -, -,
       , -,
        ]
avg_reprojection_error: 
           

至此,大功告成!!

繼續閱讀