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
目录下,见下图所示:
图片包括:
然后把
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:
至此,大功告成!!