1 # Copyright (c) 2019-2020 NVIDIA CORPORATION. All rights reserved.
3 @page dwx_camera_calibration Static Camera Calibration
6 @note SW Release Applicability: This tutorial is applicable to modules in both **NVIDIA DriveWorks** and **NVIDIA DRIVE Software** releases.
8 The NVIDIA<sup>®</sup> Static Camera Calibration tutorial describes how to perform intrinsic and extrinsic calibration for a vehicle's cameras using the DriveWorks Static Calibration Tools.
10 The step-by-step tutorial covers the following:
11 - @ref dwx_camera_calibration_prereq : The materials required to perform calibration.
12 - @ref dwx_camera_calibration_setup : The proper camera and target placements.
13 - @ref dwx_camera_calibration_data : Capturing the intrinsic and extrinsic data required for the tools.
14 - @ref dwx_camera_calibration_tooling : Generating camera calibration output.
15 - @ref dwx_camera_calibration_additional_info : Additional Information including intrinsic-only calibration.
17 @section dwx_camera_calibration_prereq 1.0 Prerequisites
19 The following cameras and targets are required before proceeding in this tutorial.
21 @subsection dwx_camera_calibration_prereq_cams 1.1 Cameras
23 The following cameras are required for calibration:
27 - N number of fixed focal length cameras. In this tutorial, 12 AR0231 cameras are used as an example.
28 - Cameras are mounted on vehicle or otherwise not expected to be purposely moved after completion of this tutorial.
29 - Referred to as "camera" or "cameras" throughout the remainder of this tutorial.
31 #### One Camera to support AV camera calibration
33 - Fixed focal length of roughly 18mm-30mm.
34 - At least 12MP resolution.
35 - Sensor size to be roughly Canon's ASP-C or Nikon's DX size (roughly 23mmx15mm).
36 - Referred to as "external camera" throughout the remainder of this tutorial.
38 @subsection dwx_camera_calibration_prereq_targets 1.2 Targets
40 This tutorial requires the use of targets to calibrate the vehicle's cameras.<br>
41 The targets used in this tutorial can be found in the following folder:
43 data/tools/calibration/aprilTargets
45 An example of the target used in this tutorial:
47 
49 @subsubsection dwx_camera_calibration_prereq_targets_prints 1.2.1 Target Prints
51 The targets must be printed as follows:
53 #### 8 large targets (or more)
55 - Target **IDs 89-101** in `data/tools/calibration/aprilTargets`.
59 - Target **IDs 180-183** in `data/tools/calibration/aprilTargets`.
61 @note A target with the same ID cannot be reused multiple times.
62 @note Although not recommended, it is possible to use targets other than the ones provided with the Static Calibration Tools.
64 @subsubsection dwx_camera_calibration_prereq_targets_print_val 1.2.2 Target Print Validation
66 For each target, measure the length of the horizontal bar in millimeters (mm) and note it in the corner, as demonstrated in the [above image](@ref dwx_camera_calibration_prereq_targets).
68 @note Ensure the printouts have correct aspect ratios by comparing the length of the vertical and horizontal bars.
69 @note __If their lengths are not equal, the targets MUST be reprinted, as they do not have the correct aspect ratio.__
70 @note Additionally, ensure the targets are perfectly flat, rigid, and do not bend.
73 @section dwx_camera_calibration_setup 2.0 Scene Setup
75 @subsection dwx_camera_calibration_setup_cams 2.1 Camera Placement
77 Depending on the number and location of the cameras on the vehicle, different scene setups may be used.<br>
78 In this tutorial, a setup with 12 cameras mounted on a car roof is used as an example.
80 @subsection dwx_camera_calibration_setup_targets 2.2 Target Placement
82 Ensure the following constraints when placing targets during scene setup:
84 - The 4 [small targets](@ref dwx_camera_calibration_prereq_cams) are placed on the wheels, and that the target centers and wheel centers are aligned.
85 - Each camera requiring calibration must observe at least 1 [large target](@ref dwx_camera_calibration_prereq_cams).
86 - Each target covers the camera's field of view as much as possible.
87 - Each target is present only once in the scene.
89 In this tutorial, a set of 8 large and 4 small targets are used as an example.
91 
93 
95 @section dwx_camera_calibration_data 3.0 Capturing Data
97 The following types of data must be captured for static camera calibration:
98 - **Intrinsic Data**: This only needs to be captured once if camera parameters i.e., lens, have not changed.
99 - **Extrinsic Data**: Includes images captured from the vehicle's cameras to be calibrated and images captured with the external camera.
101 @subsection dwx_camera_calibration_data_int 3.1 Capturing Data for Intrinsic Camera Calibration
103 To capture data for intrinsic camera calibration:
105 1. Move a checkerboard pattern or AprilTag target in front of each camera requiring calibration.<br><br>
106 
109 2. Setup and start the Recorder for all cameras requiring calibration. Refer to @ref dwx_recording_devguide_basic_recording for more information how to setup recorder.
110 @note To save disc space setup recorder to record h264/mp4 stream directly, e.g. by passing `format=h264,output-format=yuv` as camera sensor properties. <br><br>
112 3. While the recorder is running, perform the following for each camera:<br><br>
113 - Move the checkerboard pattern from left to right in front of the camera.
114 - While moving, rotate the pattern in all dimensions to calibrate pitch, roll, and yaw.
115 - While moving, get closer and further away from the camera to calibrate scale.
116 - Stop periodically while moving to allow motion-blur free constraint detection.
118 The calibration tool will automatically extract all valid frames from the recorded video. \n Refer to @ref dwx_camera_calibration_tools_int.
120 @subsection dwx_camera_calibration_data_ext 3.2 Capturing Data for Extrinsic Camera Calibration
122 To capture data for extrinsic camera calibration, use one of the following methods to capture one image from each car camera:
124 @subsubsection dwx_camera_calibration_dwksrecorder 3.2.1 Using the DriveWorks Recorder
126 Use the DriveWorks Recorder Tool to simultaneously record video from all cameras and ffmpeg to extract one frame from the video.
128 1. Setup the Driveworks Recorder Tool as described in @ref dwx_recording_devguide_basic_recording (use h264/mp4 recording)<br><br>
130 2. Extract individual frames with ffmpeg:
132 ffmpeg -i <folder>/camera_X.h264 -t 1 -f image2 camera_X.png
134 @subsubsection dwx_camera_calibration_dwkssample 3.2.2 Using the DriveWorks Sample
136 Use the DriveWorks Sample `sample_camera_multiple_gmsl` to capture a frame.
138 1. For 12 cameras of the same type execute:
140 ./sample_camera_multiple_gmsl --selector-mask=111111111111
142 2. Press **F** while the sample is running to capture a frame from each camera as a .png file.
144 Once you have followed one of the two methods described above, rename and copy the files to an appropriate location as indicated by the [DriveWorks Calibration Tools](@ref dwx_camera_calibration_tooling).
146 In addition to capturing frames from the vehicle's cameras, a set of images also needs to be captured with the external camera.<br>
147 These images help construct a calibration graph as they introduce a link between targets not observable by the car cameras.<br>
148 These targets can include ground targets, wheel targets, and between targets of individual cameras.
150 It is recommended you capture at least 20 images with the external camera, so that all possible target pairs are recorded.<br>
151 Ensure these images simultaneously observe both the ground targets, and the wheel targets.
153 The following demonstrates the types of images to capture with the external camera:
155 
157 @note It is not required to perform intrinsic calibration for the external camera. Targets detected in the frame will be automatically used for intrinsic constraints.
158 @note Therefore, it is important that there are at least 20 images provided for the external camera.
160 @section dwx_camera_calibration_tooling 4.0 Using the Calibration Tools
162 The Static Calibration Tool suite consists of multiple command line tools.
163 This tutorial covers the tools required for static calibration of the vehicle's cameras.
164 There are additional calibration tools for auxiliary support and additional sensor. These are not covered in this tutorial.
165 All tools used in this section are documented in @ref dwx_camera_calibration_tools.
167 @note It is recommended to run the calibration tool suite on an x86 Host System due to the resource requirements of the internal optimization.
169 @subsection dwx_camera_calibration_tools_dir 4.1 Directory Structure
171 The calibration tools expect a specific directory structure as well as a predefined collection of files:
175 |-- targets.json The target database with the measured bar lengths
176 |-- special-targets.json List all special targets in this file
177 |-- intrinsics Constraints generated using intrinsic tool
178 | |-- <camera-0>.json
180 | |-- <camera-N>.json
182 |-- extrinsics Images captured by car cameras
183 | |-- <camera-0>.[png/jpg] NOTE: names of the files must match the .json
184 | |-- ... file names from the intrinsic/ folder
185 | |-- <camera-N>.[png/jpg]
187 |-- external Images captured by external camera
188 | |-- <image-0>.[png/jpg/JPG] NOTE: names of the images are irrelevant
190 | |-- <image-M>.[png/jpg/JPG]
193 @subsubsection dwx_camera_calibration_targetsjson 4.1.1 targets.json
195 **targets.json** is a target database shipped with DriveWorks. It contains all the large targets that can be used in the scene.
197 Modify this file and change the `barLength` fields for all targets in the scene to the length (in meters) which you measured in @ref dwx_camera_calibration_prereq_targets_prints.
199 @subsubsection dwx_coordinate_systems_specialtargets 4.1.2 special-targets.json
201 **special-targets.json**: The targets attached to the wheels and the targets that lie flat on the floor must also be declared in this file.
203 The structure of this file is as follows:
207 "ground_targets": [99,100],
208 "ignore_targets": [142],
219 Ensure that the targets which are on the ground and which are attached to the wheels are correctly identified in this file.
221 @subsection dwx_camera_calibration_tools_int 4.2 Intrinsic Camera Calibration
223 This section requires the usage of the DriveWorks @ref dwx_intrinsics_constraints, to extract intrinsics constraints used during calibration for each individual camera.<br>
224 The input for this tool is the video recorded in @ref dwx_camera_calibration_data_int.<br>
225 This tool will then export all required constraints and output them in a `.json` file to be placed in the `/intrinsics/` subfolder in the directory structure.
227 @subsubsection dwx_camera_calibration_tools_11x8_board 4.2.1 For an 11x8 Checkerboard
229 Run the tool by executing:
231 ./calibration-intrinsics-constraints
232 --use-checkerboard=11x8
233 --input-video=<video_path>/camera-0.h264
234 --output=<calib_data_path>/intrinsics/camera-0.json
236 @subsubsection dwx_camera_calibration_tools_apriltag_vid 4.2.2 For an AprilTag Target Video
238 Run the tool by executing:
240 ./calibration-intrinsics-constraints
241 --targetDB=<calib_data_path>/targets.json
242 --input-video=<video_path>/camera-0.h264
243 --output=<calib_data_path>/intrinsics/camera-0.json
245 The tool will open a window playing back the input video and indicate with a red or green border if a new intrinsic constraint has been collected (i.e. target or checkerboard has been found).
247 
249 @note It is important that the tool is able to find at least 30 constraints (checkerboards or AprilTags) per camera.
251 @subsection dwx_camera_calibration_tools_ext 4.3 Extrinsic Calibration
253 This section requires the usage of the DriveWorks @ref dwx_calibration_graph_cli, to construct a graph-based representation of the data obtained, which is then further optimized.
255 Running this tool without parameters assumes you have the file structure specified in @ref dwx_camera_calibration_tools_dir.<br>
256 The tool determines the names of the cameras from the `extrinsics` folder. It then searches the `intrinsic` folder for `.json` files corresponding with the intrinsics constraints.<br>
257 The images captured with the external camera will also be used for intrinsic calibration for the external camera, and for extrinsics constraints.<br>
259 The Calibration Tool will then use these images to construct a graph representation which is later optimized.
261 @subsubsection dwx_camera_calibration_tools_ext_setup 4.3.1 Extrinsic Calibration Setup
263 Ensure the following before proceeding:
265 - The images captured in @ref dwx_camera_calibration_data_ext are placed in the `<calib_data_path>/extrinsics` folder. The image file \n names also match the intrinsics `.json` file names generated in @ref dwx_camera_calibration_tools_int.
267 - The images captured by the external camera are placed in the `<calib_data_path>/external` folder.
269 @subsubsection dwx_camera_calibration_tools_ext_exec 4.3.2 Extrinsic Calibration Execution
271 Run the tool by executing the following:
273 ./calibration-graph-cli --dir=<calib_data_path>
275 The tool outputs progress to the console. Take note of warnings in yellow and errors in red as they indicate possible problems with the calibration.
277 The following outputs are placed in the target folder:
279 - `calibrated-graph.json` is the main output for this tool. This contains all constraints, the calibrated camera models, the camera poses, the target poses, etc.<br> This is an intermediate format which is meant for machine consumption only.
281 - Intrinsic validation images show the set of points used for intrinsic calibration:
282 - Blue dots = detected.
283 - Red dots = reprojected.
284 - Small yellow lines = line joining detection and re-projection.
285 - Large yellow lines = outliers.
287 The output is generated for all car cameras, (`validation-intrinsics-<camera>.jpg`) and the external cameras.
289 
291 - Extrinsic validation images show the image used for extrinsic calibration with overlaid results, where:
292 - Detected targets have a green overlay.
293 - Detected tags have their corner detections with the same colors as their intrinsic re-projections.
294 - The ground plane is drawn as a series of green lines 1 meter apart. The lines at `x = 0` and `y = 0` are orange.
296 The output is generated for all car cameras, and each external image (`validation-<camera>.jpg`).
298 
300 @subsubsection dwx_camera_calibration_tools_ext_val 4.3.3 Extrinsic Calibration Validation
302 Validation images can be used to double check if the computed calibration is valid, i.e., if the green re-projections match the targets.
304 In the following image, a green mask clearly overlays the targets, and the corners are well aligned. This indicates a valid computed calibration.
306 
308 In the following image, a green mask is misaligned with the target. This indicates the calibration results might not be correct, or that there is a problem with the calibration target.
310 
312 @note In most cases an invalid re-projection of the calibration mask occurs when:
313 - Intrinsic calibration is not well constrained. This occurs if the checkerboard or target was not adequately covering the camera field of view during intrinsic calibration. As an example, intrinsic validation images contain points that are well-detected and spread all over.
314 - Target corner detection failed or is not precise. This may occur if the image is blurry or low quality.
315 - Targets are not flat. This is the most likely reason for the misalignment. Ensure that all targets are flat, rigid, and do not bend, to prevent these issues.
317 @subsection dwx_camera_calibration_tools_rig 4.4 Generating DriveWorks `rig.json` File
319 The @ref dwx_calibration_graph_cli also provides a `calibrated-graph.json` file as output. This file contains intermediate \n calibration results which cannot yet be consumed by the NVIDIA<sup>®</sup> DriveWorks SDK. These results can be converted using the Calibrated Graph Tool into a valid DriveWorks SDK `rig.json` file representing the full calibrated camera rig configuration, and can be directly consumed by the DriveWorks modules, e.g., @ref rig_mainsection.
321 To perform the conversion, run the tool by executing the following:
323 ./calibration-graph-to-rig --graph=<calib_data_path>/calibrated-graph.json
325 The tool generates a `rig.json` file in the current folder. This file lists all cameras used during calibration with their final intrinsic and extrinsic calibration data.
333 "protocol": "camera",
339 "distortion": "-3.38998615741730e-1 2.48174026608467e-1 0.000000000000000 ",
345 "correction_rig_T": [
350 "correction_sensor_R_FLU": {
352 -2.9311993804404e-11,
353 1.66752689434446e-09,
354 -6.67010668919943e-09
357 "nominalSensor2Rig_FLU": {
365 -0.00227832840755582,
375 "axlebaseFront": 0.0,
379 "centerOfMassToRearAxle": 0.0,
380 "driveByWireTimeConstant": 0.0,
381 "driveByWireTimeDelay": 0.0,
382 "frontCorneringStiffness": 0.0,
387 "rearCorneringStiffness": 0.0,
388 "steeringCoefficient": 0.0,
389 "wheelDiameter": 0.0,
392 "widthWithMirrors": 0.0
400 If an existing `rig.json` is passed as input, all camera entries are modified with the new calibration results.
402 @note The coordinate system conventions used by the calibration tools can be found in @ref dwx_coordinate_systems.
404 @section dwx_camera_calibration_additional_info 5.0 Additional Information
406 @subsection dwx_camera_calibration_int_only 5.1 Intrinsic-only Calibration
408 The Static Calibration Tool suite supports calibration for intrinsic parameters only, if extrinsic calibration is not desired or required.
410 The procedure is similar to the full calibration workflow, with several modifications:
412 1. Record videos with the checkerboard or AprilTag patterns described in @ref dwx_camera_calibration_data_int.
413 2. Create the directory structure as described in @ref dwx_camera_calibration_tools_dir. Only include the `intrinsics` folder and `targets.json` file.
414 3. For each camera, execute the [intrinsic calibration application](@ref dwx_camera_calibration_tools_int). This generates a set of `.json` files for each camera.
415 4. Execute the Graph Calibration Tool as demonstrated in @ref dwx_camera_calibration_tools_ext_exec.
416 - This generates a `calibrated-graph.json file` in the input directory, and intrinsic validation images as demonstrated in the section. Verify that these validation images meet the requirements described above.
417 5. Execute the Calibrated Graph to Rig File Tool to populate a `rig.json` file with the result. Refer to @ref dwx_camera_calibration_tools_rig for more information.