CodeTheRobot

ROS 2 Lyrical Luth · Lesson 50 of 86

Image transport

Compressed and theora transports, republish and bandwidth control.

Estimated time · 30–45 min

Learning goals

What you will understand

  • Explain image transport in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

image_transport lets image publishers offer several formats: raw (large), compressed (JPEG/PNG), and theora (video). Subscribers choose the transport, which saves network bandwidth when images are sent between a robot and a laptop.

Use image_transport::create_publisher and create_subscription in C++ instead of plain topics, and the republish node to convert between formats. Raw 640×480 RGB at 30 Hz is about 27 MB/s, whereas compressed is a small fraction of that.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the image transport example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

ros2 run image_transport list_transports
# Show which transports are installed (raw, compressed, theora, ...).
ros2 run image_transport republish raw compressed --ros-args -r in:=/camera/image -r out/compressed:=/camera/image/compressed
# Publish a compressed version of a raw image topic.
ros2 topic bw /camera/image
# Measure bandwidth used by the raw topic.
ros2 topic bw /camera/image/compressed
# Measure the compressed topic; it should be much smaller.
ros2 run rqt_image_view rqt_image_view
# View either transport.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Sensor view

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

SENSOR VIEW

Demo for Image transport. Expected result: The compressed topic uses far less bandwidth than the raw one

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# TODO: republish the raw image as a compressed image (in:=/camera/image, out/compressed:=/camera/image/compressed)
ros2 topic bw /camera/image/compressed

Expected output

The compressed topic uses far less bandwidth than the raw one
Stuck? Show the full solution (try it yourself first)
ros2 run image_transport republish raw compressed --ros-args -r in:=/camera/image -r out/compressed:=/camera/image/compressed
ros2 topic bw /camera/image/compressed

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Publish a simulated camera image, republish it as compressed, measure the bandwidth of both with ros2 topic bw, and compute the compression ratio.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.