文件如图:
使用方法:
#include <iostream>
#include <string>
#include<opencv2/core/core.hpp>
using namespace std;
int main(int argc, char **argv) {
//读取
string ConfigSLAMPath = "../ConfigSLAM.yaml";//把路径设置对
cv::FileStorage fsSettings(ConfigSLAMPath.c_str(), //将配置文件名转换成为字符串
cv::FileStorage::READ); //只读
if (!fsSettings.isOpened()){
cerr << "Failed to open settings file at: " << ConfigSLAMPath << endl;
//然后退出
exit(-1);
}
float fx = fsSettings["Camera.fx"];
float fy = fsSettings["Camera.fy"];
cout<<fx<<"-"<<fy<<endl;
return 0;
}
使用YAML文件注意点:
我在使用的时候,因为文件有缩进,会报错(把缩进收回去就可以):
OpenCV Error: Unknown error code -49 (Input file is empty) in cvOpenFileStorage, file /home/yk/桌面/opencv-3.2.0/modules/core/src/persistence.cpp, line 4422
terminate called after throwing an instance of 'cv::Exception'
what(): /home/yk/桌面/opencv-3.2.0/modules/core/src/persistence.cpp:4422: error: (-49) Input file is empty in function cvOpenFileStorage