0
$\begingroup$

Rosanswers logo

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?


Originally posted by varimax on ROS Answers with karma: 1 on 2017-10-26

Post score: 0


Original comments

Comment by gvdhoorn on 2017-10-27:
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.

Comment by psammut on 2017-10-27:
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.

Comment by varimax on 2017-10-28:
@psammut thank you for your kind reply. https://drive.google.com/open?id=0B2X39ezZBWHRYi1WU2FPamZGWTA 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]

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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_calibration/Tutorials/MonocularCalibration

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]

Originally posted by psammut with karma: 258 on 2017-10-28

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by varimax on 2017-10-29:
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?

Comment by varimax on 2017-10-29:
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?

Comment by varimax on 2017-10-29:
Lastly how do you calculate the projection_matrix from rectification matrix?

Comment by psammut on 2017-10-30:
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

Comment by psammut on 2017-10-30:
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

Comment by tanasis on 2018-06-06:
@psammut Did you figure out how to get the projection_matrix when using matlab-calibration? Whould getOptimalNewCameraMatrix() from openCV be a way?

Comment by Asan A. on 2021-12-29:
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)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.