Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 73 additions & 83 deletions image_pipeline/doc/camera_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,99 @@
Camera Info
===========

Camera Coordinate System
A camera publishes the calibration of its images in a ``sensor_msgs/msg/CameraInfo`` message, next to each image: ``image_raw`` comes with ``camera_info``.
The calibration is computed by `camera_calibration <https://docs.ros.org/en/rolling/p/camera_calibration/>`_, and used by `image_proc <https://docs.ros.org/en/rolling/p/image_proc/>`_ to rectify the images, and by `stereo_image_proc <https://docs.ros.org/en/rolling/p/stereo_image_proc/>`_ to compute depth from stereo images.
This page explains what its matrices mean.

Camera coordinate system
------------------------
Below is a diagram of the camera coordinate system assumed by the
CameraInfo message. It is a right-handed system, with the world X
and Y aligned with the image x and y. This is the same coordinate
system used in OpenCV. It differs from the coordinate system of
Harley and Zisserman, which has Z forward, Y up, and X to the
left (looking towards +Z).

The camera frame is an *optical frame*: z points forward, along the optical axis, x to the right and y down, as the x and y axes of the image.
It is a right-handed system, the same as in OpenCV.
The ``frame_id`` of the images and camera infos is the optical frame of the camera.

|coords|

.. |coords| image:: images/CameraCoords.png

Derivation Diagram
------------------
The fields of CameraInfo
------------------------

* ``height``, ``width``: The size of the images used for the calibration, normally the full resolution of the camera.
* ``distortion_model`` and ``d``: The model and the coefficients of the lens distortion.
``plumb_bob`` has 5 coefficients (k1, k2, p1, p2, k3), ``rational_polynomial`` 8 (k1, k2, p1, p2, k3, k4, k5, k6), and ``equidistant``, for fisheye lenses, 4 (k1, k2, k3, k4).
* ``k``: The 3x3 camera matrix of the raw (distorted) images, with the focal lengths fx and fy and the principal point (cx, cy), in pixels:

.. code-block:: text

K = [fx 0 cx]
[ 0 fy cy]
[ 0 0 1]

* ``r``: The rectification matrix, a rotation applied to the camera frame. It is the identity for a monocular camera. For a stereo camera, it aligns the two cameras so that their image rows are aligned.
* ``p``: The 3x4 projection matrix of the rectified images:

.. code-block:: text

P = [fx' 0 cx' Tx]
[ 0 fy' cy' Ty]
[ 0 0 1 0]

Its left 3x3 part, ``K'``, is the camera matrix of the rectified images. It may differ from ``K``.
``Tx`` and ``Ty`` are 0, except for the second camera of a stereo pair.
* ``binning_x``, ``binning_y`` and ``roi``: The subsampling and the region of interest of the images, when the camera does not publish its full resolution. 0 means no binning and the full image.

Below is a diagram of the various images that are conceptually
derivable from the CameraInfo message. The boxed variables are
present in this message.
From a 3D point to the raw image
--------------------------------

The diagram below shows the images that can be computed from the camera info; the boxed matrices are in the message.

|camera_info|

.. |camera_info| image:: images/CameraInfo.png

Projection Onto the Original Image
----------------------------------
Starting with an initial 3D point X, the position of the corresponding
image point is found by going right-to-left in the upper half of the
diagram. The process is summarized by the following equations.
A 3D point ``X'``, in the camera frame, projects to the raw image, going right to left along the top of the diagram:

.. code-block:: text

X' = [R,t]X transform
sx = X' projection
x* = d(x) distortion
q = Kx* pixel coordinates
x = (X'/Z', Y'/Z') projection onto the normalized image
x* = d(x) distortion, with the coefficients D
q = K [x*, 1] pixel in the raw image

This process uses the K camera matrix and D distortion vector from CameraInfo.
Ignoring the transform T, you can find the projection of a point X' into the
original camera frame using these equations. First, the 3D point X' is
projected onto the normalized, undistorted image via a projection operation
(division by Z). Then the distortion coefficients are used in the function
d() to move the point to its distorted position, still in a normalized image.
Finally, the normalized image is converted to a pixel-coordinate image by
applying the camera matrix to each image point.
This is what ``cv2.projectPoints`` does with ``K`` and ``D``.

Rectification
-------------
Rectification is the process of transforming the input image into an output
image with the distortion corrected, and optionally the normalized image plane
transformed by rotation, optinain-image-plane translation, and optionally
in-image-plane scale.

Simple Monocular Rectification
------------------------------
In this case, there is a monocular device with distortion correction only.
To transform a pixel in the input image into one in the output image, it is
sent through the K - D - R - K' series of transformations. K - D gets to the
normalized, undistorted image; the rotation R is the identity because we
don't want to rotate the normalized undistorted image; and then K' converts
back to pixel coordinates in the output image. In this case, since there is
no need of in-image-plane translation, or scaling from the original image,
so K = K' , and only the K and D elements of CameraInfo are needed.
The ``camera_calibration`` package does monocular calibration to get K'
using OpenCV's getOptimalNewCameraMatrix() function with argument
'alpha'=0.0 that cause K' ≠ K.

Projection onto the output image is by the P matrix, which is formed from K'
and the optional rotation and translation, in this case the identity and 0,
respectively. So if K' = K, P is redundant information, and could be formed
directly from K.

Stereo and Complex Monocular Rectification
------------------------------------------
After getting normalized undistorted image using K and D, here we apply a
transformation to the normalized, undistorted image: each points on this
image is rotated by R and then is passed to K' get the correspondent pixel
coordinate on the output image which is translated and scaled by K' with
respect to the original image. This transformation is important, for example,
in aligning two images of a stereo pair to lie in the same plane and have
coincident epipolar lines.

The transform of a pixel from the input to output image is the same as in
the simple monocular case, except that R need not be the identity, and K'
need not be equal to K. As a consequence, the normalized undistorted image
can be rotated in 3D space, and the output image can be translated, scaled
relative to the original image.

The projection matrix has an additional parameter, a translational offset t.
This parameter is used in the right stereo camera to reflect its external
position relative to the left camera. Since the point X in left camera
frame is transformed to the right camera by [I|t], that means the position
of the left camera frame origin with respect to the right camera frame is
given by the vector t in the right camera frame.

Putting this all together, the projection matrix for the rectified output
image is given by:

Rectification computes an image without distortion, and, for a stereo camera, with aligned rows.
Each pixel of the raw image goes through ``K``, ``D``, ``R`` and ``K'``, along the bottom of the diagram:
``K`` and ``D`` give the normalized undistorted image, ``R`` rotates it, and ``K'`` gives the pixel of the rectified image.

As a result, a 3D point projects to the rectified image with ``P``:

.. code-block:: text

P = K' [I|t].
[u, v, w] = P [X, Y, Z, 1]
(x, y) = (u/w, v/w)

where ``[X, Y, Z]`` is the point in the rectified frame of the camera, which is the camera frame rotated by ``R``.

Monocular cameras
^^^^^^^^^^^^^^^^^

``R`` is the identity, and ``P = [K' | 0]``: the point is in the camera frame.

``K'`` sets the scale of the rectified image.
``camera_calibration`` computes it with ``cv2.getOptimalNewCameraMatrix`` and the **scale** slider of the calibration window: at 0, the default, the rectified image only contains valid pixels, so ``K'`` is a little different from ``K``.

Stereo cameras
^^^^^^^^^^^^^^

The two cameras have their own ``K``, ``D``, ``R`` and ``P``.
Their ``R`` rotate both cameras to the same orientation, so that a point of the scene is on the same row in both rectified images, and their ``P`` have the same ``K'``.

The fourth column of ``P`` gives the position of the second camera: ``Tx = -fx' * B``, where ``B`` is the baseline, the distance between the two cameras.
``Tx`` is 0 for the first camera, usually the left one.

Note that in this case the internal camera parameters of the output image,
K', can be different from the input image, so P cannot be formed just
from K and D. And the I is the 3x3 rotation matrix representing left
camera frame in the right, not the identity.
With ``[X, Y, Z]`` a point in the rectified frame of the *first* camera, ``P`` of each camera gives its pixel in each rectified image.
The two pixels are on the same row, and their horizontal distance is the disparity ``d = fx' * B / Z``, from which ``stereo_image_proc`` computes the depth.
10 changes: 5 additions & 5 deletions image_pipeline/doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ the major changes between distributions

Changes in Jazzy Jalisco
------------------------
There are several major change between ``Iron`` and ``Jazzy``:
There are several major changes between ``Iron`` and ``Jazzy``:

* All components now properly support ``image_transport`` paramter,
* All components now properly support ``image_transport`` parameter,
or ``depth_image_transport`` parameter if the topic is a depth image.
In most places, this consists of simply adding the parameter, or making
the parameter work, however two cases should be noted where the
parameter was renamed:

* image_view::ExtractImages: incorrectly named parameter ``transport``
was renamed to more consistent ``image_transport``.
* imaeg_view::StereoView: incorrectly named parameter ``transport``
* image_view::StereoView: incorrectly named parameter ``transport``
was renamed to more consistent ``image_transport``.

* Improvements to QoS support:
Expand All @@ -38,7 +38,7 @@ There are several major change between ``Iron`` and ``Jazzy``:
``rgb/image_rect_color`` to ``rgb/image_raw`` to make clear that the
unrectified camera projection matrix is used, and for consistency with
other radial nodes.
* The boolen parameter ``full_dp`` from the DisparityNode has been deleted
and a new integer parameter ``sgbm_mode`` added to enable all the
* The boolean parameter ``full_dp`` from the DisparityNode has been deleted
and a new integer parameter ``sgbm_mode`` added to enable all the
variations of the stereo matching algorithm SGBM available from the
OpenCV library.
Loading