eirikaso
eirikaso

Reputation: 123

Parsing YAML file using yaml-cpp error

I am trying to parse info from a .yaml file with camera calibration into a "sensor_msgs::CameraInfo" message in ROS.

I manage to parse both INTs and strings, but are having issues when I get to the double vector/matrix.

Here is my code:

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;

}

The error I get is this:

error: no match for 'operator=' (operand types are 'sensor_msgs::CameraInfo_ >::_D_type {aka std::vector >}' and 'double')
camera_info_msg.D = camera_info_yaml["distortion_coefficients"].as();

The documentation for the cameraInfo message is located here: http://docs.ros.org/api/sensor_msgs/html/msg/CameraInfo.html

Tutorial for the yaml-cpp package: https://github.com/jbeder/yaml-cpp/wiki/Tutorial

The "Distortion coefficients" part of my yaml file lokk like this:

distortion_coefficients:
rows: 1
cols: 5
data: [-0.167477, 0.023595, 0.004069, -0.002996, 0.000000]

Does anyone know what I'm doing wrong?

Upvotes: 1

Views: 2011

Answers (1)

Jesse Beder
Jesse Beder

Reputation: 34044

The error on this line:

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

suggests that the left-hand-side is an std::vector<double> whereas the right-hand-side is a double. Instead:

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

Upvotes: 1

Related Questions