ROS BAG FILE EXTRACTION INTO PNG IMAGE USING ROBOT OPERATING SYSTEM (ROS)

Maarahma
4 min readApr 21, 2024
ROS .bag to PNG

In this article, I wanna share how to extract a ROS .bag file into PNG images using a Robot Operating System (ROS). In this case, I collected data using an Intel Realsense D455 depth camera and the output video format is .bag file. So I need to extract this into PNG images. Before we start, we need to install/download these 4 necessary things.

  1. WSL (Windows Subsystem for Linux)
  2. Linux Ubuntu
  3. Robot Operating System (ROS)
  4. Foxglove Studio

Because I use Windows 10 OS, so I need to install WSL. But if you use Linux OS you can go to step 4.

The ROS .bag file can only be opened using applications in ROS or special applications that support such as Foxglove Studio. Before starting image extraction, some stages need to be done as follows.

  1. Enable WSL support features under “Windows Features”. Check the “Virtual Machine Platform” and “Windows Subsystem for Linux” options. Click “OK” then restart the computer.
WSL Download
Ubuntu Download

3. Open the Ubuntu that has been installed then create a UNIX account by entering the username and password as requested.

4. After successfully creating a UNIX account the next step is to install ROS Noetic referring to this link, ROS installation.

5. At the Ubuntu terminal copy the following command.

wget -c https://raw.githubusercontent.com/qboticslabs/ros_install_noetic/master/ros_install_noetic.sh && chmod +x ./ros_install_noetic.sh && ./ros_install_noetic.sh

6. The terminal will ask for your UNIX password again before starting the Noetic ROS installation. Re-enter the password that was created earlier.

7. When the installation process is running the user will be offered the type of installation to be performed. If the computer has sufficient storage select the “Desktop-Full” option.

8. After the successful installation of ROS Noetic, the next step is to activate ROS Noetic that has been installed. Enter this command every time the machine is started.

source /opt/ros/noetic/setup.bash

To be more effective, save the command in the “~/.bashrc” by typing the following command.

source /opt/ros/noetic/setup.bash >> ~/.bashrc

9. After that, close the Ubuntu terminal and reopen it.

10. Check if ROS Noetic is installed by typing the command “roscore” in the Ubuntu terminal.

11. After ROS Noetic is installed, the next step is the image extraction process.

12. On Windows, create a Python script (.py) in an existing IDE, such as Visual Studio Code, then save it as rosbag2png.py in the same directory as the ROS .bag file.

13. Open the rosbag2png.py repository and copy the entire line of code into your Python script.

14. At the bottom of the code change it to the following.

    except CvBridgeError as e:
print(e)
rgb_file.close()
dep_file.close()
bag.close()

15. Save the data folder containing the ROS .bag files and the rosbag2png.py file in the C:/ directory to facilitate the image extraction process.

16. Open the installed Ubuntu with ROS Noetic and open the terminal.

17. Change the directory path in the terminal to the directory where the data folder is located.

18. Before running the script, pay attention to the parameter in the rosbag2png.py file.

parser = argparse.ArgumentParser()
parser.add_argument("--out", required=True, help = "name of output directory (will be created if not exist)")
parser.add_argument("--rgb", required=True, help = "name of ROS topic for RGB images")
parser.add_argument("--dep", required=True, help = "name of ROS topic for Depth images")
parser.add_argument("--bag", required=True, help = "path to bag file")
args = vars(parser.parse_args())

The parameters “out” and “bag” are already known but we need to know the arguments for the parameters “rgb” and “dep”. To find out the “rgb” and “dep” parameters, you can use Foxglove Studio.

Foxglove Studio Download
Foxglove Studio Uploads File
File Opened
RGB and Depth Arguments

21. After that, at the Ubuntu terminal enter the following command and run it.

python3 rosbag2png.py --out nama_output_direktori --rgb device_0/sensor_1/Color_0/image/data --dep device_0/sensor_0/Depth_0/image/data -- bag nama_direktori_rosbag/nama_rosbag_file.bag
Depth PNG Images
RGB PNG Images

--

--