CUDA Accelerated GStreamer Camera Undistort/User Guide/Camera Calibration: Difference between revisions

m
no edit summary
No edit summary
 
mNo edit summary
 
Line 1: Line 1:
<noinclude>
<noinclude>
{{CUDA Accelerated GStreamer Camera Undistort/Head|next=Examples|previous=User Guide|metakeywords=OpenCV calibration,camera calibration,distortion parameters,fisheye model,Brown-Conrady model}}
{{CUDA Accelerated GStreamer Camera Undistort/Head|next=Examples|previous=User Guide|description=The following wiki page will introduce a way to calibrate your camera and get the parameters used in the camera undistortion.}}
</noinclude>
</noinclude>
<seo title="GStreamer Camera Undistort - Camera Calibration" titlemode="replace" metakeywords="GStreamer, NVIDIA, Jetson, Tegra, TX1, TX2, AI, GStreamer, Jetson TX1, Jetson TX2, Xavier, NVIDIA Jetson Xavier, NVIDIA Jetson Xavier NX, Jetson Xavier, Jetson Xavier NX, CUDA, Camera Undistort, GStreamer Camera Undistort, AI, Deep Learning, Undistort,CUDA Acceleration" description="The following wiki page will introduce a way to calibrate your camera and get the parameters used in the camera undistortion."></seo>


The following page will introduce a way to calibrate your camera and get the parameters used in the camera undistortion. This method consists of a Python script that will guide you through the calibration process without going into technical details.
The following page will introduce a way to calibrate your camera and get the parameters used in the camera undistortion. This method consists of a Python script that will guide you through the calibration process without going into technical details.
Line 14: Line 12:
== Setting up the Parameters ==
== Setting up the Parameters ==
Go inside the calibration directory of the '''cuda-undistort''' repository
Go inside the calibration directory of the '''cuda-undistort''' repository
<source lang=bash>
<syntaxhighlight lang="bash">
cd calibration
cd calibration
</source>
</syntaxhighlight>


Open the '''settings.json''' file, where you will find the following lines
Open the '''settings.json''' file, where you will find the following lines
<source lang=json>
<syntaxhighlight lang="json">
{
{
     "cameraWidth": 640,
     "cameraWidth": 640,
Line 32: Line 30:
     "fisheyeDistortionParameters": [0.0, 0.0, 0.0, 0.0]
     "fisheyeDistortionParameters": [0.0, 0.0, 0.0, 0.0]
}
}
</source>
</syntaxhighlight>


You need to adjust those parameters so that they match your camera specification and your printed calibration pattern.  
You need to adjust those parameters so that they match your camera specification and your printed calibration pattern.  
Line 47: Line 45:
*'''cameraIndex'''
*'''cameraIndex'''
:: When a camera is connected to a device, an index is used to refer to the camera. This index is the number that follows the ''video'' when listing the cameras with the following command
:: When a camera is connected to a device, an index is used to refer to the camera. This index is the number that follows the ''video'' when listing the cameras with the following command
<source lang=bash>
<syntaxhighlight lang="bash">
ls /dev/video*
ls /dev/video*
</source>
</syntaxhighlight>
:: This parameter is the index of the camera.
:: This parameter is the index of the camera.
*'''rtspAddress'''
*'''rtspAddress'''
Line 100: Line 98:
=== Running the Calibrator ===
=== Running the Calibrator ===
Run the calibration tool as follows
Run the calibration tool as follows
<source lang=bash>
<syntaxhighlight lang="bash">
python3 calibrationTool.py [OPTIONS]
python3 calibrationTool.py [OPTIONS]
</source>
</syntaxhighlight>
where [OPTIONS] are the chosen options of the table above. Usually, you will want to run the calibration process, overwrite possible existing images, and run a test at the end to check the results. For this use case, run the calibrator as follows:
where [OPTIONS] are the chosen options of the table above. Usually, you will want to run the calibration process, overwrite possible existing images, and run a test at the end to check the results. For this use case, run the calibrator as follows:
<source lang=bash>
<syntaxhighlight lang="bash">
python3 calibrationTool.py -c -r -t
python3 calibrationTool.py -c -r -t
# Add the -v flag if you are using a camera that is accessed through v4l2.  
# Add the -v flag if you are using a camera that is accessed through v4l2.  
</source>
</syntaxhighlight>
A window will appear, showing the captured images from the camera. In the beginning, the video will be slow, as the calibration pattern is not found.  
A window will appear, showing the captured images from the camera. In the beginning, the video will be slow, as the calibration pattern is not found.  
<br>
<br>
Line 141: Line 139:
Also, the '''cuda-undistort''' element will need the computed camera parameters, but as environment variables. For this reason, those parameters will be printed in your terminal, so that you can copy them for your pipelines. You will see an output like the one below, so choose the ''CAMERA_MATRIX'' and ''DISTORTION_PARAMETERS'' for your desired model, whether it is ''fisheye'' or ''Brown-Conrady''
Also, the '''cuda-undistort''' element will need the computed camera parameters, but as environment variables. For this reason, those parameters will be printed in your terminal, so that you can copy them for your pipelines. You will see an output like the one below, so choose the ''CAMERA_MATRIX'' and ''DISTORTION_PARAMETERS'' for your desired model, whether it is ''fisheye'' or ''Brown-Conrady''


<source lang=bash>
<syntaxhighlight lang="bash">
   =============================================  
   =============================================  
  |  The following variables can be used as the |
  |  The following variables can be used as the |
Line 159: Line 157:
CAMERA_MATRIX="{\"fx\":9.5211633874478218e+02, \"fy\":9.4946222068253201e+02, \"cx\":6.8041416457132573e+02, \"cy\":3.1446117133659988e+02}"
CAMERA_MATRIX="{\"fx\":9.5211633874478218e+02, \"fy\":9.4946222068253201e+02, \"cx\":6.8041416457132573e+02, \"cy\":3.1446117133659988e+02}"
DISTORTION_PARAMETERS="{\"k1\":2.1107496349546324e+01, \"k2\":-2.4383787227376064e+02, \"p1\":-2.4875466379420917e-03, \"p2\":2.2798038164244493e-03, \"k3\":5.9419414118252871e+02, \"k4\":2.1085235633925034e+01, \"k5\":-2.4360553937983042e+02, \"k6\":5.9359831515760391e+02}"
DISTORTION_PARAMETERS="{\"k1\":2.1107496349546324e+01, \"k2\":-2.4383787227376064e+02, \"p1\":-2.4875466379420917e-03, \"p2\":2.2798038164244493e-03, \"k3\":5.9419414118252871e+02, \"k4\":2.1085235633925034e+01, \"k5\":-2.4360553937983042e+02, \"k6\":5.9359831515760391e+02}"
</source>
</syntaxhighlight>


If you need to generate the output above from the saved '''settings.json''' file, just run the tool without arguments:
If you need to generate the output above from the saved '''settings.json''' file, just run the tool without arguments:
<source lang=bash>
<syntaxhighlight lang="bash">
python3 calibrationTool.py
python3 calibrationTool.py
</source>
</syntaxhighlight>


== Using the Undistort Element ==
== Using the Undistort Element ==
Line 170: Line 168:


=== Fisheye ===
=== Fisheye ===
<source lang=bash>
<syntaxhighlight lang="bash">
CAMERA_MATRIX="{\"fx\":9.5211633874478218e+02, \"fy\":9.4946222068253201e+02, \"cx\":6.8041416457132573e+02, \"cy\":3.1446117133659988e+02}"
CAMERA_MATRIX="{\"fx\":9.5211633874478218e+02, \"fy\":9.4946222068253201e+02, \"cx\":6.8041416457132573e+02, \"cy\":3.1446117133659988e+02}"
DISTORTION_PARAMETERS="{\"k1\":3.8939572818197948e-01, \"k2\":-5.5685725182648649e-01, \"k3\":2.3785352925072494e+00, \"k4\":-1.2037220289124213e+00}"
DISTORTION_PARAMETERS="{\"k1\":3.8939572818197948e-01, \"k2\":-5.5685725182648649e-01, \"k3\":2.3785352925072494e+00, \"k4\":-1.2037220289124213e+00}"
</source>
</syntaxhighlight>


==== For Jetpack 4.n ====
==== For Jetpack 4.n ====
Camera to display.
Camera to display.
<source lang=bash>
<syntaxhighlight lang="bash">
gst-launch-1.0 nvarguscamerasrc \
gst-launch-1.0 nvarguscamerasrc \
   ! nvvidconv \
   ! nvvidconv \
Line 183: Line 181:
   ! nvvidconv \
   ! nvvidconv \
   ! nvoverlaysink
   ! nvoverlaysink
</source>
</syntaxhighlight>


Test to mp4
Test to mp4
<source lang=bash>
<syntaxhighlight lang="bash">
OUTPUT=output.mp4
OUTPUT=output.mp4
gst-launch-1.0 videotestsrc num-buffers=60 \
gst-launch-1.0 videotestsrc num-buffers=60 \
Line 194: Line 192:
   ! nvv4l2h264enc bitrate=20000000 \
   ! nvv4l2h264enc bitrate=20000000 \
   ! h264parse ! mp4mux ! filesink location=$OUTPUT
   ! h264parse ! mp4mux ! filesink location=$OUTPUT
</source>
</syntaxhighlight>


==== For Jetpack 5.n ====
==== For Jetpack 5.n ====


<source lang=bash>
<syntaxhighlight lang="bash">


gst-launch-1.0 nvarguscamerasrc \
gst-launch-1.0 nvarguscamerasrc \
Line 205: Line 203:
   ! nvvidconv \
   ! nvvidconv \
   ! nvoverlaysink
   ! nvoverlaysink
</source>
</syntaxhighlight>


=== Brown-Conrady ===
=== Brown-Conrady ===


<source lang=bash>
<syntaxhighlight lang="bash">
CAMERA_MATRIX="{\"fx\":9.5211633874478218e+02, \"fy\":9.4946222068253201e+02, \"cx\":6.8041416457132573e+02, \"cy\":3.1446117133659988e+02}"
CAMERA_MATRIX="{\"fx\":9.5211633874478218e+02, \"fy\":9.4946222068253201e+02, \"cx\":6.8041416457132573e+02, \"cy\":3.1446117133659988e+02}"
DISTORTION_PARAMETERS="{\"k1\":2.1107496349546324e+01, \"k2\":-2.4383787227376064e+02, \"p1\":-2.4875466379420917e-03, \"p2\":2.2798038164244493e-03, \"k3\":5.9419414118252871e+02, \"k4\":2.1085235633925034e+01, \"k5\":-2.4360553937983042e+02, \"k6\":5.9359831515760391e+02}"
DISTORTION_PARAMETERS="{\"k1\":2.1107496349546324e+01, \"k2\":-2.4383787227376064e+02, \"p1\":-2.4875466379420917e-03, \"p2\":2.2798038164244493e-03, \"k3\":5.9419414118252871e+02, \"k4\":2.1085235633925034e+01, \"k5\":-2.4360553937983042e+02, \"k6\":5.9359831515760391e+02}"
</source>
</syntaxhighlight>


==== For Jetpack 4.n ====
==== For Jetpack 4.n ====


<source lang=bash>
<syntaxhighlight lang="bash">
gst-launch-1.0 nvarguscamerasrc \
gst-launch-1.0 nvarguscamerasrc \
   ! nvvidconv \
   ! nvvidconv \
Line 222: Line 220:
   ! nvvidconv \
   ! nvvidconv \
   ! nvoverlaysink
   ! nvoverlaysink
</source>
</syntaxhighlight>


==== For Jetpack 5.n ====
==== For Jetpack 5.n ====


<source lang=bash>
<syntaxhighlight lang="bash">


gst-launch-1.0 nvarguscamerasrc \
gst-launch-1.0 nvarguscamerasrc \
Line 233: Line 231:
   ! nvvidconv \
   ! nvvidconv \
   ! autovideosink
   ! autovideosink
</source>
</syntaxhighlight>


== How to get good quality calibration  ==
== How to get good quality calibration  ==