使用yaml-cpp错误解析YAML文件

时间:2018-05-03 08:29:19

标签: c++ yaml ros yaml-cpp

我正在尝试将带有相机校准的.yaml文件中的信息解析为ROS中的“sensor_msgs :: CameraInfo”消息。

我设法解析INT和字符串,但是当我到达双向量/矩阵时遇到问题。

这是我的代码:

sensor_msgs::CameraInfo yamlToCameraInfo(std::string leftOrRightCam)
{
  YAML::Node camera_info_yaml = YAML::LoadFile(leftOrRightCam + ".yaml");
  sensor_msgs::CameraInfo camera_info_msg;
  camera_info_msg.width = camera_info_yaml["image_width"].as<uint32_t>();
  camera_info_msg.height = camera_info_yaml["image_height"].as<uint32_t>();
  camera_info_msg.distortion_model = camera_info_yaml["distortion_model"].as<std::string>();
  camera_info_msg.D = camera_info_yaml["distortion_coefficients"].as<double>();
  camera_info_msg.K = camera_info_yaml["camera_matrix"].as<double>();

  return camera_info_msg;

}

我得到的错误是:

  

错误:'operator ='不匹配(操作数类型是   'sensor_msgs :: CameraInfo_&gt; :: _ D_type {aka   std :: vector&gt;}'和'double')
  camera_info_msg.D =   camera_info_yaml [ “distortion_coefficients”]作为();

cameraInfo消息的文档位于:http://docs.ros.org/api/sensor_msgs/html/msg/CameraInfo.html

yaml-cpp包教程:https://github.com/jbeder/yaml-cpp/wiki/Tutorial

我的yaml文件的“失真系数”部分像这样:

  

distortion_coefficients:
   行:1
   cols:5
   数据:[ - 0.167477,0.023595,0.004069,-0.002996,0.000000]

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

这一行的错误:

camera_info_msg.D = camera_info_yaml["distortion_coefficients"].as<double>();

表示左侧是std::vector<double>,而右侧是double。代替:

camera_info_msg.D = camera_info_yaml["distortion_coefficients"].as<std::vector<double>>();