Camera Calibration

Introduction

This tutorial explains how to calibrate the camera. A good calibration is necessary for image processing. The output of calibration process is a calibration matrix. A calibration contains rectification coefficients for lens distortion as well as intrinsic and extrinsic coefficients, which are needed for reconstruction of 3D objects from 2D images.

  • Lines beginning with $ are terminal commands.

    • To open a new terminal use the shortcut Ctrl + Alt + T.

    • To open a new tab inside an existing terminal use the shortcut Ctrl + Shift + T.

    • To kill a process in a terminal use the shortcut Ctrl + C.

  • Lines beginning with # indicate the syntax of the commands.

  • Code is separated in boxes.

  • Code is case sensitive.

Using a USB Camera

Attention

If you don’t have an usb camera or webcam, you have to use the gazebo camera in the turtlebot3 simulation. Gazebo already publishes the camera_info topic with calibration data, but you should still learn the procedure.

  1. Download the the calibration target gazebo model. Calibration Target Gazebo Model

  2. Extract it in ~/.gazebo/models.

  3. $ ros2 launch turtlebot3_gazebo turtlebot3_house.launch.py

  4. Place it in your world.

  5. Skip this chapter.

To run the webcam, use the ROS2 package usb_camera_driver. It provides an interface to all standard USB cameras.

$ cd ~/robot_ws/src
$ git clone https://github.com/klintan/ros2_usb_camera
$ cd ~/robot_ws
$ colcon build
$ source ~/.bashrc
$ ros2 launch usb_camera_driver usb_camera_node.launch.py

Task

Start the camera with the new launch file and visualize the image stream in RViz.

  1. $ rviz2

  2. Add > Image

As you maybe already noticed, you got a ROS warning during the starting process of the camera node: Using default calibration URL. This happens because you are not providing any calibration information to the camera node. For many image processing applications in ROS you have to calibrate the camera.

Calibrating a Camera

For calibration, the ROS package camera_calibration can be used.

$ cd ~/robot_ws/src``
$ git clone -b ros2 https://github.com/ros-perception/image_pipeline``
   # - Delete all folders in "image_pipeline" except "camera_calibration"
   #   or install all package dependencies
   # - On cv2 import error: ``sudo apt install python3-opencv``
$ cd ~/robot_ws``
$ colcon build``
$ source ~/.bashrc``

Calibration Process

  1. Read the whole process once before starting calibration.

  2. Print Checkerboard (not for simulation).

  3. Start the camera with $ ros2 launch usb_camera_driver usb_camera_node.launch.py or the simulation with $ ros2 launch turtlebot3_gazebo turtlebot3_house.launch.py and place the calibration target in front of the turtlebot3

  4. Check your camera info topic. All intrinsic camera matrices should be empty or zero (not in simulation).

  5. Launch the calibration with correct parameters. $ ros2 run camera_calibration cameracalibrator -c camera --size=8x6 --square=0.05 image:=/camera/image_raw It will pop up a new window, like shown in Figure 10.

    ../../../_images/parameters.png
    ../../../_images/checkerboard_pattern.png

    Figure 10 Calibration Tool

  6. In order to get a good calibration result, you will need to move the checkerboard around in the camera view such that the checkerboard:

    • is on the camera’s left, right, top and bottom of field of view

    • is filling the whole field of view

    • is tilted to the left, right, top and bottom (skew)

    At each step (s. Figure 11), hold the checkerboard still until the pattern is highlighted in the calibration window.

    ../../../_images/steps.png

    Figure 11 Calibration Steps

  7. When all 4 bars (X, Y, Size and Skew) are in green click on calibrate button.

    Note

    It may take some time due to large amount of data to be processed.

  8. After the calibration is done, click on the Save button.

  9. Now click on Commit.

  10. Copy the generated calibration data (calibrationdata.tar.gz) from /tmp directory into the config folder of usb_camera_driver

  11. Extract the calibrationdata.tar.gz file:

    $ cd ~/robot_ws/src/ros2_usb_camera/config
    $ mkdir calib
    $ tar –xvzf calibrationdata.tar.gz -C calib
    
  12. Rename the ost.txt file to calibration.ini.

  13. Convert the calibration.ini file to a yml format.

    $ cd calib
    $ ros2 run camera_calibration_parsers convert calibration.ini webcam.yml
    
  14. Move the webcam.yml file to the config folder in your usb_camera_driver package.

  15. Open the usb_camera_node.launch.py file and edit the camera_info_url parameter with the new calibration file.

    usb_camera = launch_ros.actions.Node(
       package='usb_camera_driver', node_executable='usb_camera_driver_node', output='screen', node_namespace=ns,
       parameters=[{"camera_calibration_file": "file:///config/webcam.yml"}])
    
  16. Launch usb_camera_node.launch.py, take a look at the camera_info topic and check, if the correct calibration data is published.

For more information visit http://wiki.ros.org/camera_calibration and http://wiki.ros.org/camera_calibration/Tutorials/MonocularCalibration.