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

Export matlab monocular camera calibration parameters to ROS

asked 2017-10-26 18:51:00 -0600

varimax gravatar image

I have calibrated my camera using matlab's calibration toolbox and saved it as a .mat file. Is there a way to convert this file into yaml? Also once I have the yaml file, how can this file be accepted by camera_info so that image_proc can subscribe and read the parameters?

edit retag flag offensive close merge delete

Comments

1

Seeing as Mathworks has quite good ROS support these days (Robotics System Toolbox), you might be able to ask them about this. I can imagine you're not the only one who tries to do this.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-27 03:07:35 -0600 )edit

Can you post a sample .mat cal file? A .yaml camera calibration file is nothing special, and simply contains the same information as your matlab file but in a different format. If you post a .mat file i can do a quick conv for you and show you how to do it.

psammut gravatar image psammut  ( 2017-10-27 13:17:25 -0600 )edit

@psammut thank you for your kind reply. https://drive.google.com/open?id=0B2X... This is a link to my parameters. I think the most important parameter is the camera intrinsic matrix: [7.0832e+03 0 0] [0 7.0783e+03 0] [2.4454e+03 1.6328e+03 1]

varimax gravatar image varimax  ( 2017-10-28 01:18:43 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-10-28 12:39:07 -0600

psammut gravatar image

updated 2017-10-28 15:31:40 -0600

A matlab monocular camera calibration is stored in the format of a cameraParameters object.

In ROS, the calibration parameters are usually stored in a yaml file, and then they are loaded into a sensor_msgs/CameraInfo Message (read this documentation for more info). The format

A calibration yaml file for a monocular camera in ROS requires the following:

  • height in pixels
  • width in pixels
  • camera_name which should correspond to a link name in your URDF.
  • distortion_model which is always just "plumb_bob"
  • The distortion parameters D, size depending on the distortion model. For "plumb_bob", the 5 parameters are: (k1, k2, t1, t2, k3). Also known as the distortion_coefficients
  • Intrinsic camera_matrix K for the raw (distorted) images.
  • A 3x3 rectification_matrix which for monocular cameras is just an identity matrix.
  • A 3x4 projection_matrix which specifies the intrinsic matrix of the processed image

I tried loading your .mat file into matlab to get the parameters out if it but unfortunately I couldn't get a any real numbers out of it (everything was either 0's or 1's) and also I don't think it uses the same distortion model. I'm sorry but I wasn't able to convert your file!

Also once I have the yaml file, how can this file be accepted by camera_info so that image_proc can subscribe and read the parameters?

The way this works in ROS is that you pass in the location of the yaml file to the driver node you use to load your camera. In ROS there are bunch of camera drivers depending on what type of camera it is. If it is a UVC compliant camera you would use libuvc_camera. You pass in the following parameter with the location of the yaml file:

<param name="camera_info_url" value="file:///tmp/cam.yaml"/>

That driver node then publishes BOTH the images from your camera along with the calibration information. Stereo_image_proc can then subscribe to those images and calibration and use them to give you the point cloud if you are doing stereo. Hope this helps!

PS. The easiest way to do this is just to calibrate your camera using the ros calibration utility and a checkerboard. Follow this tutorial for a monocular camera: http://wiki.ros.org/camera_calibratio...

Here is a sample camera calibration yaml for the left camera of a stereo pair:

image_width: 640
image_height: 480
camera_name: stereo0_link
camera_matrix:
  rows: 3
  cols: 3
  data: [734.889420, 0.000000, 317.706196, 0.000000, 732.082689, 239.033655, 0.000000, 0.000000, 1.000000]
distortion_model: plumb_bob
distortion_coefficients:
  rows: 1
  cols: 5
  data: [0.100896, 0.019699, 0.004179, -0.004728, 0.000000]
rectification_matrix:
  rows: 3
  cols: 3
  data: [0.997520, -0.005830, 0.070143, 0.005907, 0.999982, -0.000881, -0.070137, 0.001293, 0.997537]
projection_matrix:
  rows: 3
  cols: 4
  data: [801.287930, 0.000000, 255.724689, 0.000000, 0.000000, 801.287930, 235.533001, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000]
edit flag offensive delete link more

Comments

thank you so much! just one more question though. Could you explain a little bit more about what camera_name means and where I can find it?

varimax gravatar image varimax  ( 2017-10-29 12:59:32 -0600 )edit

Also for distortion_coefficient, I only see four radial and tangential distortions. radialDistortion: [-0.0208,-0.0051], TangentialDistortion: [0,0]. Where is the fifth radial distortion coefficient? Should it be 0?

varimax gravatar image varimax  ( 2017-10-29 13:33:57 -0600 )edit

Lastly how do you calculate the projection_matrix from rectification matrix?

varimax gravatar image varimax  ( 2017-10-29 13:35:12 -0600 )edit

camera_name is simply the frame_id of your camera if you defined it in the URDF file of your robot. This allows you to attach a location to the images. See the URDF tutorials if unclear. I think the 5th coefficient should be 0

psammut gravatar image psammut  ( 2017-10-30 08:28:37 -0600 )edit

how do you calculate the projection_matrix from rectification matrix? I don't know :( You should really look into doing the calibration with the ROS utility and not have to worry about this stuff

psammut gravatar image psammut  ( 2017-10-30 08:29:25 -0600 )edit

@psammut Did you figure out how to get the projection_matrix when using matlab-calibration? Whould getOptimalNewCameraMatrix() from openCV be a way?

tanasis gravatar image tanasis  ( 2018-06-06 05:36:31 -0600 )edit

the projection matrixes which are calculated in the stereo_calibration node, are not correct. I would suggest you to not use them, but instead, calculate by yourself.

by decoupling a fundamental matrix cv::sfm::projectionsFromFundamental(F,P1,P2) ,

or

cv::decomposeEssentialMat(E,R1,R2,t) and then reconstruct a projection matrix. P1=I and P2 = [R|t] (in case of decomposeEssentialMat function you will obtain 4 possible projection matrix, check the opencv documentation for more details)

Asan A. gravatar image Asan A.  ( 2021-12-29 05:36:50 -0600 )edit
0

answered 2021-12-29 09:59:39 -0600

Asan A. gravatar image

The best way is to write a small Matlab code that will load parameters and export them to the YAML file

There is a small bridge tool to do this yamlmatlab

Example for creating yaml file from stereo calibration

addpath(genpath('/home/adamanov/Libraries/yamlmatlab-master'));

load('stereoParams.mat');
load('estimationErrors.mat');

%% -----------Left Camera (=Camera_1) ----------------
l.image_width  = stereoParams.CameraParameters1.ImageSize(2);
l.image_height = stereoParams.CameraParameters1.ImageSize(1);

l.camera_matrix = '' ;
l.camera_matrix.rows = 3;
l.camera_matrix.cols = 3;
l.camera_matrix.dt = 'd';
fx = stereoParams.CameraParameters1.IntrinsicMatrix(1,1);
fy = stereoParams.CameraParameters1.IntrinsicMatrix(2,2);
s =  stereoParams.CameraParameters1.IntrinsicMatrix(2,1);
cx = stereoParams.CameraParameters1.IntrinsicMatrix(3,1);
cy = stereoParams.CameraParameters1.IntrinsicMatrix(3,2);

l.camera_matrix.data = [fx, s, cx , 0, fy, cy, 0, 0,  1];

k1 = stereoParams.CameraParameters1.RadialDistortion(1);
k2 = stereoParams.CameraParameters1.RadialDistortion(2);
k3 = stereoParams.CameraParameters1.RadialDistortion(3);
p1 = stereoParams.CameraParameters1.TangentialDistortion(1);
p2 = stereoParams.CameraParameters1.TangentialDistortion(2);

l.distortion_coefficients = '';
l.distortion_coefficients.rows = 1;
l.distortion_coefficients.cols = 5;
l.distortion_coefficients.dt = 'd';
l.distortion_coefficients.data = [k1,k2,p1,p2,k3];


yaml.WriteYaml('camera_info_left.yaml',l)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-26 18:43:03 -0600

Seen: 1,870 times

Last updated: Dec 29 '21