Full Body Pose Estimation for Sports Analysis - Camera Calibration

From RidgeRun Developer Connection
Jump to: navigation, search



Previous: Camera Abstraction Index Next: AprilTag Camera Calibration




This wiki summarizes the documentation of the camera calibration library. It contains information such as system design, scalability, and usage.

Introduction

Camera calibration is a key task in computer vision applications. It provides a way to relate a camera frame with what it's being actually captured in the real world. A camera is considered calibrated when its calibration parameters are known. Therefore, the camera calibration process is a procedure that allows the determination of the camera's parameters.

Camera calibration parameters are separated into three groups. First, the extrinsic calibration describes the position and orientation of the camera in relation to the world's coordinate system. They are defined by a rotation and a translation, which are commonly expressed as a rotation matrix R and a translation vector t. Then, the distortion coefficients account for the camera lens distortion. And finally, the intrinsic parameters model the the behavior of the internal geometry and optical characteristics of the camera, such as focal length (fx, fy), principal point offset (x0, y0) and axis skew (s). They are unique to the camera, and they are represented by the intrinsic calibration matrix K.

[math]\displaystyle{ K = \begin{pmatrix} f_{x} & s & x_{0}\\ 0 & f_{y} & y_{0}\\ 0 & 0 & 1 \end{pmatrix} }[/math]

The camera calibration library consists of a python package that provides an interface for the use of different camera calibration methods. It was developed along with the Full Body Pose Estimation System, however, it is a standalone module as shown in the next image.

Camera calibration module location in general workflow

Library Features

The library provides two approaches for computing camera calibration parameters. Both are based on classic computer vision techniques and require the use of a calibration object.

If you don't know what a calibration object is, or why is it required by classic calibration methods to calibrate your camera, don't worry, it is a relatively simple concept. By using an object with known geometry and dimensions, it is possible to establish a relation between known points in the object (object points) and their corresponding points in the camera frame (image points). Such an object would be referred to as the calibration object since it would be used to perform the calibration process. Consequently, it is not only required to be able to identify the object on the image but also to match the object points with their corresponding image points.

Chessboard calibration

The OpenCV library provides a series of functions that facilitate the process required to compute the camera intrinsic and extrinsic calibration parameters.

As already mentioned, this approach is based on a classic method that requires the use of a calibration object. In this case, the said object corresponds to a chessboard with known dimensions. Ideally, the chessboard calibration object must be built on sturdy material and with the highest tolerances at hand to improve the calibration quality. Also, it is required for the chessboard to be asymmetrical, otherwise, the resulting calibration parameters will be wrong.

You can read more about this calibration method in the Chessboard Calibrator Wiki.

AprilTag calibration

When calibrating a multi-camera system, it is required for every camera to capture the same calibration object in order to have enough data to compute the transformation between the cameras coordinate systems. Sometimes, depending on the camera angles, it could be difficult to capture a chessboard like a calibration object.

AprilTag, is a visual fiducial system popular in robotics research. The library provides the capability of identifying predefined tags on a given image. This makes easier the construction of a three-dimensional calibration object since each of the object faces can be associated with a different tag.

Same as with other calibration methods, its accuracy depends on the quality of the calibration object. However, if the application is not critical, a simple cardboard box with the tags printed on the sides is enough.

You can read more about this calibration method in the AprilTag Calibrator Wiki.

How to use the calibration library

The camera calibration library offers an interface to its camera calibrators by implementing the facade design pattern. This structural pattern hides the system complexities from the library user. To obtain further information on this design pattern, you can check the information on this tutorial.

To perform intrinsic and extrinsic camera calibration, it is necessary to get the required calibrator objects instantiated. As mentioned in the #Library Features section, there are currently two different calibration methods available. Hence, you can choose between the chessboard and the apriltag calibrators. However, if you want to check which calibrator options you have available in your system, you can do it as follows.

1 import camera_calibration.calibrationfacade
2 
3 available_calibrators = calibratorfacade.get_available_calibrators()
4 print('Available calibrators: %s' % available_calibrators)

Now, it is not required for you to use the same calibrator for both calibrations. For instance, in the following code snippet, we create a chessboard intrinsic calibrator and an apriltag extrinsic calibrator.

1 import camera_calibration.calibrationfacade
2 
3 available_calibrators = calibratorfacade.get_available_calibrators()
4 print('Available calibrators: %s' % available_calibrators)
5 
6 intrinsic_calibrator = calibrationfacade.get_intrinsic_calibrator('chessboard')
7 extrinsic_calibrator = calibrationfacade.get_extrinsic_calibrator('apriltag')

Finally, all that is left is to perform the actual calibration. Both, the intrinsic and the extrinsic calibrators provide a calibrate() function that takes a list of calibration pictures in RGB format. However, the extrinsic calibrator also requires the intrinsic calibration matrix and the distortion coefficients of the camera. Take as an example the following piece of code, which corresponds to the complete sample code we have been working on in this section.

 1 import camera_calibration.calibrationfacade
 2 
 3 available_calibrators = calibratorfacade.get_available_calibrators()
 4 print('Available calibrators: %s' % available_calibrators)
 5 
 6 intrinsic_calibrator = calibrationfacade.get_intrinsic_calibrator('chessboard')
 7 extrinsic_calibrator = calibrationfacade.get_extrinsic_calibrator('apriltag')
 8 
 9 # Calibration images with chessboard like calibration object
10 chessboard_images = []
11 # Calibration images with apriltag calibration object
12 apriltag_images = []
13 
14 # Perform intrinsic calibration
15 # K = intrinsic calibration matrix
16 # dist = distortion coefficients
17 # err = re-projection error
18 K, dist, err = intrinsic_calibrator.calibrate(chessboard_images)
19 
20 #Perform extrinsic calibration
21 # r = rotation vector
22 # t = translation vector
23 # err = re-projection error
24 r, t, err = extrinsic_calibrator.calibrate(apriltag_images, K, dist)

System design

Library base architecture

The camera calibration library was modeled based on the object-oriented programming paradigm. Its two main objects are the intrinsic and extrinsic calibrators. Therefore, the whole calibration system was designed around these two classes.

The design is based on the abstract factory design pattern. This creational pattern provides interfaces for both the concrete calibrators and their factories. Factories allow the creation of new calibrator instances and since this pattern is based on the concept of a super factory or factory of factories, concrete factories can be instantiated using a factory creator, which then can be used to instantiate new concrete calibrators. For more information on this pattern, check this tutorial.

In this case, the factory creator class corresponds to the calibration library facade, as shown in the next image. Please note that the diagram shows the classes colored according to their abstraction level. For instance, the library facade corresponds to the higher level of abstraction you can use to interact with the library and is colored green, interfaces and base classes are painted blue and orange respectively, and the concrete classes are color red.

Error creating thumbnail: Unable to save thumbnail to destination
Camera calibration library design

Library modules

The camera calibration library is composed of several python modules, as shown in the next image. The colors of the diagram represent the level of abstraction, the more abstraction. For instance, the calibrationfacade offers the simplest interface to interact with the library and is colored green. Whereas concrete modules like chessboardcalibrator is represented by the color red.

Error creating thumbnail: Unable to save thumbnail to destination
Camera calibration library module diagram.

The orange classes that are part of two of the modules in the Camera calibration library module diagram are base classes. Which are different from the blue colored classes that correspond to interfaces. On one hand, the base classes provide methods or properties to be extended by other classes, for instance, the BaseIntrinsicCalibrator and BaseExtrinsicCalibrator classes offer OpenCV based methods to compute calibration parameters from sets of object and image points. On the other hand, the Calibrator and CalibratorFactory interfaces enforce a determined structure on concrete calibrator and factory classes that implement them.

Red colored modules provide specific functionalities that are unique to them. For example, the chessboardcalibrator offers the ability to compute camera calibration parameters from images with a chessboard like calibration object. Similarly, the apriltagcalibrator provides the possibility to calibrate a camera using a three-dimensional calibration object build with tags. Both of these concrete modules extend the base calibrator classes since they use the same mathematical functions provided by OpenCV to compute the parameters. At the same time, base calibrators implement the calibrator interface. Therefore, concrete calibrators can be treated the same, no matter what special calibration characteristic they have, by using polymorphism.

Classes, attributes and methods

If you would like to know in-depth the library implementation, please check its full class diagram in the next image. The diagram follows a color scheme based on the level of abstraction. The CameraCalibrationFacade, which offers the simplest method of interaction with the library is shown in green color. Then all interface classes, such as CameraCalibratorFactory and Calibrator are shown in color blue. Similarly, all base classes are identified by an orange color. And finally, all concrete classes, such as ChessboardIntrinsicCalibrator are colored red.

Also, note that some classes have the same name, such as Factory or Calibration, this is possible because they belong to separate modules. In fact, the use of the same name for the Factory class at the calibrator module level is important, because it is the name that the calibrationfacade module looks for, to instantiate new calibrators.

Error creating thumbnail: Unable to save thumbnail to destination
Camera calibration library full class diagram.

Scalability

The library was designed around the idea that new calibration methods could be included. That is the reason why the creation of new camera calibrator classes must follow the library defined interfaces. The user must not only implement the custom calibrator class, but also its concrete factory.

How to add a new calibrator to the library

Step 1. Create a new python file inside the camera_calibration directory

As an example, let's say our new calibrator module will be called newcalibrator.py. In that case, you can proceed to open the python file with your editor of choice.

cd rrpose/camera_calibration/
editor newcalibrator.py

Step 2. Add the calibrator classes to the calibrator module

There are two possible calibrators to implement. The intrinsic camera calibrator and the extrinsic camera calibrator. These are used to compute the intrinsic and extrinsic camera parameters respectively. Each calibrator module might include either one or both calibrator classes. Now, said classes must inherit from the IntrinsicCalibrator or ExtrinsicCalibrator base classes provided in calibratorinterface.py, depending on what is required.

Of course, it is also possible for the calibrator classes to inherit from other already implemented concrete calibrators, as they are already based on the base calibrator classes and might have other reusable code. For instance, the AprilTag calibrator extends this calibrator.

If your new calibrator requires some sort of configuration parameters to be provided when being initialized, then you can just specify them as arguments in the constructor. However, we heavily encourage the use of a calibration class, which can hold all your calibration parameters in a much cleaner way. For instance, in the following code example, you will find how to create your own intrinsic and extrinsic calibrator classes with their configuration class.

 1 from .calibratorinterface import BaseConfigurationClass
 2 from .calibratorinterface import Calibrator
 3 
 4 
 5 class Configuration(BaseConfigurationClass):
 6     '''New-calibrator configuration class.
 7 
 8     Attributes:
 9         obj_points (None): Some required configuration
10             parameter. Defaults to None.
11     '''
12 
13     def __init__(self):
14         self.configuration_parameter = None
15 
16 
17 class NewIntrinsicCalibrator(Calibrator):
18     """New concrete intrinsic calibrator.
19 
20     This calibrator...
21 
22     Args:
23         configuration (Configuration): Configuration object.
24 
25     Attributes:
26         configuration (Configuration): Configuration object.
27     """
28 
29     def __init__(self, configuration):
30         self.configuration = configuration
31 
32     def calibrate(self, images):
33         """Compute intrinsic calibration parameters.
34 
35         This function computes the intrinsic calibration matrix
36         and distortion coefficients of a camera. It also
37         computes the re-projection error resulting from the use
38         of the calculated calibration parameters.
39 
40         It is important to maintain the function return values.
41         This way the calibrator can be compatible with other
42         software that already uses the library.
43 
44         Args:
45             images (list): Image list captured with the same camera.
46 
47         Returns:
48             mtx (np.array): Camera's intrinsic calibration matrix.
49             dist (np.array): Camera's distortion coefficients.
50             err (float): Re-projection error.
51         """
52         mtx = np.zeros(1)
53         dist = np.zeros(1)
54         err = float('inf')
55 
56         return [mtx, dist, err]
57 
58 
59 class NewExtrinsicCalibrator(Calibrator):
60     """New concrete extrinsic calibrator.
61 
62     This calibrator...
63 
64     Args:
65         configuration (Configuration): Configuration object.
66 
67     Attributes:
68         configuration (Configuration): Configuration object.
69     """
70 
71     def __init__(self, configuration):
72         self.configuration = configuration
73 
74     def calibrate(self, images):
75         """Compute extrinsic calibration parameters.
76 
77         This function computes the rotation and translation 
78         of the camera, relative to the world coordinate system.
79         It also computes the re-projection error resulting from
80         the use of the calculated calibration parameters.
81 
82         It is important to maintain the function return values.
83         This way the calibrator can be compatible with other
84         software that already uses the library.
85 
86         Args:
87             images (list): Image list captured with the same camera.
88 
89         Returns:
90             rotation_vector (list): Estimated rotation vector.
91             translation_vector (list): Estimated translation vector.
92         """
93         rvec = np.zeros(1)
94         tvec = np.zeros(1)
95         err = float('inf')
96 
97         return [rvec, tvec, err]

Step 3. Add the Factory class to the calibrator module

This class must implement the CalibratorFactory interface. And, it is very important to keep the name Factory for this class in every calibrator module so that it can be managed by the calibration library facade.

  1 from .calibratorinterface import BaseConfigurationClass
  2 from .calibratorinterface import Calibrator
  3 from .calibratorinterface import CalibratorFactory
  4 
  5 
  6 class Configuration(BaseConfigurationClass):
  7     '''New-calibrator configuration class.
  8 
  9     Attributes:
 10         obj_points (None): Some required configuration
 11             parameter. Defaults to None.
 12     '''
 13 
 14     def __init__(self):
 15         self.configuration_parameter = None
 16 
 17 
 18 class NewIntrinsicCalibrator(Calibrator):
 19     """New concrete intrinsic calibrator.
 20 
 21     This calibrator...
 22 
 23     Args:
 24         configuration (Configuration): Configuration object.
 25 
 26     Attributes:
 27         configuration (Configuration): Configuration object.
 28     """
 29 
 30     def __init__(self, configuration):
 31         self.configuration = configuration
 32 
 33     def calibrate(self, images):
 34         """Compute intrinsic calibration parameters.
 35 
 36         This function computes the intrinsic calibration matrix
 37         and distortion coefficients of a camera. It also
 38         computes the re-projection error resulting from the use
 39         of the calculated calibration parameters.
 40 
 41         It is important to maintain the function return values.
 42         This way the calibrator can be compatible with other
 43         software that already uses the library.
 44 
 45         Args:
 46             images (list): Image list captured with the same camera.
 47 
 48         Returns:
 49             mtx (np.array): Camera's intrinsic calibration matrix.
 50             dist (np.array): Camera's distortion coefficients.
 51             err (float): Re-projection error.
 52         """
 53         mtx = np.zeros(1)
 54         dist = np.zeros(1)
 55         err = float('inf')
 56 
 57         return [mtx, dist, err]
 58 
 59 
 60 class NewExtrinsicCalibrator(Calibrator):
 61     """New concrete extrinsic calibrator.
 62 
 63     This calibrator...
 64 
 65     Args:
 66         configuration (Configuration): Configuration object.
 67 
 68     Attributes:
 69         configuration (Configuration): Configuration object.
 70     """
 71 
 72     def __init__(self, configuration):
 73         self.configuration = configuration
 74 
 75     def calibrate(self, images):
 76         """Compute extrinsic calibration parameters.
 77 
 78         This function computes the rotation and translation 
 79         of the camera, relative to the world coordinate system.
 80         It also computes the re-projection error resulting from
 81         the use of the calculated calibration parameters.
 82 
 83         It is important to maintain the function return values.
 84         This way the calibrator can be compatible with other
 85         software that already uses the library.
 86 
 87         Args:
 88             images (list): Image list captured with the same camera.
 89 
 90         Returns:
 91             rotation_vector (list): Estimated rotation vector.
 92             translation_vector (list): Estimated translation vector.
 93         """
 94         rvec = np.zeros(1)
 95         tvec = np.zeros(1)
 96         err = float('inf')
 97 
 98         return [rvec, tvec, err]
 99 
100 
101 class Factory(CalibratorFactory):
102     """Calibrator concrete factory for new_calibrator.
103 
104     This factory allows the creation of intrinsic and extrinsic
105     calibrator instances.
106     """
107 
108     def get_intrinsic_calibrator(self):
109         """Create instance of NewIntrinsicCalibrator.
110 
111         Args:
112             configuration_object (Configuration): Configuration class instance
113                 with all newcalibrator configuration parameters for the
114                 camera calibration process.
115 
116         Returns:
117             NewIntrinsicCalibrator instance.
118         """
119         if configuration_object is not None:
120             return NewIntrinsicCalibrator(configuration_object)
121         return NewIntrinsicCalibrator(Configuration())
122 
123     def get_extrinsic_calibrator(self, configuration_object=None):
124         """Create instance of NewExtrinsicCalibrator.
125 
126         Args:
127             configuration_object (Configuration): Configuration class instance
128                 with all newcalibrator configuration parameters for the
129                 camera calibration process.
130 
131         Returns:
132             NewExtrinsicCalibrator instance.
133         """
134         if configuration_object is not None:
135             return NewExtrinsicCalibrator(configuration_object)
136         return NewExtrinsicCalibrator(Configuration())

Step 4. Add the new calibrator module to the library facade

To add the new calibrator module to the library facade, is as simple as adding a new item to the CALIBRATOR_MODULES dictionary. Make sure to follow the <module_name>: <alias> item structure, where the module_name corresponds to the actual name of the python file, and the alias corresponds to the simpler name the user will use to request the calibrator. For example, if the new calibrator module name is newcalibrator.py but you want the user to call it ncalib, then add newcalibrator: ncalib to the CALIBRATOR_MODULES dictionary.

First, open calibrationfacade.py for editing with your editor of choice:

editor calibrationfacade.py

Then, add newcalibrator to the facade dictionary.

1 CALIBRATOR_MODULES = {'chessboardcalibrator': 'chessboard',
2                       'apriltagcalibrator': 'apriltag',
3                       'newcalibrator': 'ncalib'}
4 .
5 .
6 .



Previous: Camera Abstraction Index Next: AprilTag Camera Calibration