A ROS package for extracting RGB and depth image frames from .bag files. Frames are saved as both JPEG (for viewing) and NumPy .npy arrays (for processing). Available as both Python scripts and a C++ ROS node.
| Dependency | Version |
|---|---|
| ROS | Kinetic / Melodic / Noetic |
| Python | 3.6+ |
| OpenCV | 3.x or 4.x |
| NumPy | any recent |
| cv_bridge | (included with ROS desktop-full) |
No build step required. The scripts in src/bag2rgbdepth/scripts/ can be run directly.
cd Bag_to_Depth/
catkin_make
source devel/setup.bashThis is the recommended approach. Both streams are captured simultaneously.
1. Start roscore in a separate terminal:
roscore2. Run the batch extraction script from the directory containing your bag folders:
cd /path/to/bag/folders
sh /path/to/scripts/truedepthandrgb_sync.shExpected folder layout before running:
session1/
recording.bag
session2/
recording.bag
truedepthandrgb_sync.sh <- run from here
Plays each bag twice: once for RGB, once for depth. Use this if the synchronized script has timing issues.
cd /path/to/bag/folders
sh /path/to/scripts/truedepthandrgb.shRun any grabber manually against a specific output folder:
# RGB only
python3 grabrgb.py /output/rgb_folder
# Depth only
python3 grabdepth.py /output/depth_folder
# Both simultaneously
python3 grabrgbdepth.py /output/rgb_folder /output/depth_folderThen in a separate terminal, play your bag:
rosbag play recording.bagEdit the output path in the launch file:
vim src/bag2rgbdepth/launch/extractbag_to_rgbd.launchChange value to your desired output directory:
<param name="folder_extract_location" value="/path/to/output"/>Launch the node, then play your bag:
# Terminal 1
roslaunch bag2rgbdepth extractbag_to_rgbd.launch
# Terminal 2
rosbag play recording.bagAfter extraction each bag folder will contain:
session1/
recording.bag
rgb_images1/
frame0.jpg <- BGR color image
frame0.npy <- raw uint8 BGR array
frame1.jpg
frame1.npy
...
depth_images1/
dframe0.jpg <- 8-bit normalized depth (for viewing)
dframe0.npy <- raw uint16 depth values in mm
dframe1.jpg
dframe1.npy
...
Note: The C++ node saves depth as 16-bit PNG (
depth_N.png) instead of JPEG, preserving full depth precision.
The default topics match a standard OpenNI/RealSense camera:
| Topic | Type | Description |
|---|---|---|
/camera/rgb/image_raw |
sensor_msgs/Image |
Color stream |
/camera/depth_registered/image_raw |
sensor_msgs/Image |
Registered depth stream |
All grabbers skip saving frames that are identical to the previous saved frame. This avoids redundant images when the bag is paused or the camera feed is static.