ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Publishing CameraInfo messages from YAML file

asked 2016-11-21 10:00:11 -0600

mohito gravatar image

I have a bag file that publishes camera/image_raw. I also have the calibration parameters of the corresponding camera in a separate YAML file. How can I publish cam_info messages using only the YAML file so that I can rectify the image_raw?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2016-11-21 18:25:41 -0600

lucasw gravatar image

This is the same as though the comment there points to something that isn't quite what you want. is an example of publishing a camera info defined by dynamic reconfigure- but if your yaml file is loaded into the node namespace (with rosparam load) and shares the same layout then when the dr server starts it will get the right camera parameters. That script only publishes the camera info once, it could be modified to easily to also subscribe to an image and then in the image callback it would republish the camera info with a timestamp copied from the image.

It really seems like this ought to already exist- if it were really done right there would be a node that also provided the set_camera_info service and then the yaml could be provided as a url, using camera_info_manager.

edit flag offensive delete link more

answered 2017-08-29 17:16:27 -0600

ckirksey gravatar image

Nice gist from @rossbar

# Load data from file
with open(yaml_fname, "r") as file_handle:
    calib_data = yaml.load(file_handle)
# Parse
camera_info_msg = CameraInfo()
camera_info_msg.width = calib_data["image_width"]
camera_info_msg.height = calib_data["image_height"]
camera_info_msg.K = calib_data["camera_matrix"]["data"]
camera_info_msg.D = calib_data["distortion_coefficients"]["data"]
camera_info_msg.R = calib_data["rectification_matrix"]["data"]
camera_info_msg.P = calib_data["projection_matrix"]["data"]
camera_info_msg.distortion_model = calib_data["distortion_model"]
return camera_info_msg
edit flag offensive delete link more


Is this still the best method to create the CameraInfo messages from a yaml calibrationf file? Or is there an official ros package to do this?

fjp gravatar image fjp  ( 2021-08-29 21:06:19 -0600 )edit

answered 2022-08-09 09:25:17 -0600

For C++, you may consider the camera_calibration_parsers package from the ros_perception/image_common repository. It provides low-level methods for reading and writing between calibration files and CameraInfo messages.

For example, use the readCalibration function to load a calibration from file, after which you can publish it manually.

#include "camera_calibration_parsers/parse.hpp"
#include "sensor_msgs/msg/camera_info.hpp"

sensor_msgs::msg::CameraInfo camera_info;
std::string camera_name;
    "/path/to/calibration.yaml", camera_name, camera_info);

You can also look at the camera_info_manager and image_transport packages from the same repository which provide additional utility and abstraction on this topic.

edit flag offensive delete link more

Question Tools



Asked: 2016-11-21 10:00:11 -0600

Seen: 5,037 times

Last updated: Aug 29 '17