Skip to content
Merged
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
30 changes: 30 additions & 0 deletions stereo_image_proc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# stereo_image_proc

`stereo_image_proc` processes the images of a stereo camera.
It computes a disparity image and a point cloud from the left and right images, after debayering and rectifying them with `image_proc`.

For a stereo camera that publishes `/my_stereo/left/image_raw`, `/my_stereo/right/image_raw` and their `camera_info`:

```console
ros2 launch stereo_image_proc stereo_image_proc.launch.py namespace:=my_stereo
ros2 run image_view stereo_view --ros-args -r stereo:=/my_stereo -r image:=image_rect_color
```

The launch file publishes `/my_stereo/disparity` and `/my_stereo/points2`, and the rectified images of each camera.

| Component | Executable | Topics |
| --- | --- | --- |
| `stereo_image_proc::DisparityNode` | `disparity_node` | `left/image_rect` + `right/image_rect` + camera infos → `disparity` |
| `stereo_image_proc::PointCloudNode` | `point_cloud_node` | `disparity` + `left/image_rect_color` + camera infos → `points2` |

## Documentation

The full documentation, including all the parameters, a tutorial on a recorded stereo camera and a tutorial on choosing good stereo parameters, is in the [doc](https://github.com/ros-perception/image_pipeline/blob/rolling/stereo_image_proc/doc/index.rst) directory and it is published at [docs.ros.org](https://docs.ros.org/en/rolling/p/stereo_image_proc/).

To build it locally, [install rosdoc2](https://github.com/ros-infrastructure/rosdoc2#installation) and run:

```console
rosdoc2 build --package-path .
```

The result is written to `docs_output/stereo_image_proc/index.html`.
201 changes: 201 additions & 0 deletions stereo_image_proc/doc/choosing_good_stereo_params.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
.. _choosing_good_stereo_parameters:

Choosing Good Stereo Parameters
===============================

This tutorial tunes the parameters of ``DisparityNode`` to get a good disparity image: dense, with few holes on the objects of interest, and clean, without noise.
It continues :ref:`first-tutorial`: the pipeline is running, the bag is playing in a loop, and ``stereo_view`` shows the disparity image.

The images below were all computed on the same frame of the bag, so they can be compared.
While the bag plays, the bottle and the table rotate, and your images will change a little.

.. contents::
:local:
:depth: 1

How the matching works
----------------------

``DisparityNode`` uses block matching.
For each pixel of the left image, it takes a small window around it, the correlation window, and searches the same row of the right image for the most similar window.
The distance, in pixels, between the two windows is the disparity.
Close points have a large disparity, far points a small one.

The search covers the disparities from ``min_disparity`` to ``min_disparity + disparity_range - 1``.
This range defines the *horopter*, the range of depths that the matcher can see.
Points outside of it are not found.

Then filters remove the doubtful matches: matches with too little texture (``texture_threshold``), matches not much better than the second best (``uniqueness_ratio``), and small isolated regions (``speckle_size`` and ``speckle_range``).

Change the parameters while the node runs
-----------------------------------------

All the matching parameters can be changed while the node runs.
Run ``rqt_reconfigure``, and select ``disparity_node`` under ``narrow_stereo_textured``:

.. code-block:: console

$ ros2 run rqt_reconfigure rqt_reconfigure

.. image:: images/rqt_reconfigure.png
:alt: rqt_reconfigure showing the parameters of disparity_node

You can also use ``ros2 param set``, as in the rest of this tutorial.
Change one parameter at a time, and look at the effect on the disparity image.

1. Remove the speckles
----------------------

With the default parameters, there are small blobs of random disparity where the table and the bottle should be:

.. image:: images/speckles.png
:alt: disparity image with speckles on the table
:width: 60%

These *speckles* are false matches.
Block matching often makes them near the edges of objects, where the correlation window sees the object on one side and the background on the other.
Here, the matcher also finds small false matches in the texture projected on the table.

The speckle filter removes regions of similar disparity smaller than ``speckle_size`` pixels.
Two neighbor pixels are in the same region if their disparities differ by at most ``speckle_range``.
The objects of this scene are large, so raise ``speckle_size`` from 100 to 1000 pixels, the maximum:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node speckle_size 1000

.. image:: images/table_object.png
:alt: disparity image without speckles
:width: 60%

The speckles are gone.
If thin objects disappear too, lower ``speckle_size``.

2. Find the table and the bottle
--------------------------------

The wall behind the table has a nice gradient of disparity, but there is a large hole where the table and the bottle should be.
They are too close to the camera: their disparity is larger than 63, the end of the default range ``[0, 63]``.

Double ``disparity_range`` to 128, so that the range becomes ``[0, 127]``:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node disparity_range 128

.. image:: images/disparity_range_128.png
:alt: disparity image with disparity_range set to 128
:width: 60%

The table and the bottle are found.
The colors are scaled to the new range, so the wall is now blue.

Two other things changed:

* The gray band on the left of the image is twice as large.
The leftmost pixels of the left image cannot be searched over the whole range, because the matching window would leave the right image.
This band is a little wider than ``min_disparity + disparity_range`` pixels.
* There is a gray area on the left of the bottle.
This part of the table is seen by the left camera, but hidden by the bottle for the right camera, so it cannot be matched.

``disparity_range`` must be a multiple of 16.
A larger range sees closer objects, but takes more time: use the smallest range that covers your scene.

3. Move the range with min_disparity
------------------------------------

``min_disparity`` moves the range.
It is normally 0, to see as far as possible: a disparity of 0 is a point at infinity.

* To see objects even closer to the camera, raise it. This moves the horopter toward the camera, at the cost of far objects.
* If the cameras are *verged*, inclined toward each other, far points have a negative disparity. Use a negative value to see them.

For example, set ``disparity_range`` to 96, and raise ``min_disparity`` step by step:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node disparity_range 96
$ ros2 param set /narrow_stereo_textured/disparity_node min_disparity 50

The disparity of the wall is about 51.
With ``min_disparity`` 50, it is at the far end of the range, in dark red.
Raise ``min_disparity`` to 60, for the range ``[60, 155]``:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node min_disparity 60

.. image:: images/disparity_min_60.png
:alt: disparity image with min_disparity set to 60
:width: 60%

The wall is now out of range and disappears, while the table and the bottle are still found.
The gray band on the left grew again, to the new ``min_disparity + disparity_range``.

Set the range back before going on:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node min_disparity 0
$ ros2 param set /narrow_stereo_textured/disparity_node disparity_range 128

4. Other parameters
-------------------

The other parameters are changed less often.

``correlation_window_size`` is the size of the matching window, 15 by 15 pixels by default.
A larger window fills small holes in the disparity image, but smears the edges of the objects.
A smaller window keeps the details, but gives more holes and more noise.
For example, with ``correlation_window_size`` set to 9:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node correlation_window_size 9

.. image:: images/correlation_windows_9.png
:alt: disparity image with correlation_window_size set to 9
:width: 60%

``texture_threshold`` rejects windows with too little texture, like blank walls.
Raise it to remove noisy matches on surfaces without texture, lower it to keep more matches on surfaces with little texture.

``uniqueness_ratio`` rejects a match if it is not better than the second best match by this margin, in percent.
Raise it if ``texture_threshold`` and the speckle filter still let false matches through.

``prefilter_size`` and ``prefilter_cap`` normalize the brightness of the images before the matching.
This makes the matching robust to brightness differences between the two cameras.
The defaults are usually fine.

5. Semi-global block matching
-----------------------------

``stereo_algorithm`` 1 selects semi-global block matching (SGBM).
It is slower than block matching, but it also takes into account the disparity of the neighbor pixels, and gives denser disparity images, in particular on surfaces with little texture.
Here, with ``correlation_window_size`` back to 15 and ``disparity_range`` 128:

.. code-block:: console

$ ros2 param set /narrow_stereo_textured/disparity_node correlation_window_size 15
$ ros2 param set /narrow_stereo_textured/disparity_node stereo_algorithm 1

.. image:: images/disparity_sgbm.png
:alt: disparity image computed with semi-global block matching
:width: 60%

SGBM has its own parameters: ``P1`` and ``P2`` control how smooth the disparity is, ``disp12_max_diff`` enables a left-right consistency check, and ``sgbm_mode`` selects a variant.
``texture_threshold`` and ``prefilter_size`` are not used by SGBM.
See :ref:`DisparityNode` for all the parameters.

Keep the parameters
-------------------

Parameters changed at runtime are lost when the node stops.
To keep them, pass them to the launch file:

.. code-block:: console

$ ros2 launch stereo_image_proc stereo_image_proc.launch.py namespace:=narrow_stereo_textured \
speckle_size:=1000 disparity_range:=128

or save them from ``rqt_reconfigure`` (the save button at the top of the parameters), or with ``ros2 param dump /narrow_stereo_textured/disparity_node``.
Loading
Loading