-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosbag2image.py
28 lines (24 loc) · 1.03 KB
/
rosbag2image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import subprocess
import yaml
import rosbag # source /opt/ros/noetic/setup.bash
import cv2
from cv_bridge import CvBridge
import numpy as np
FILENAME = 'test' # rosbag file name without .bag
ROOT_DIR = '/home/sadik/noetic_ws' # rosbag path
BAGFILE = ROOT_DIR + '/' + FILENAME + '.bag'
f = open(ROOT_DIR + FILENAME + '-' + 'timestamps.txt', 'w') # for saving timestamps
if __name__ == '__main__':
bag = rosbag.Bag(BAGFILE)
TOPIC = '/usb_cam/image_raw' # image topic
image_topic = bag.read_messages(TOPIC)
for k, b in enumerate(image_topic):
bridge = CvBridge()
cv_image = bridge.imgmsg_to_cv2(b.message, b.message.encoding)
cv_image.astype(np.uint8)
cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
cv2.imwrite(ROOT_DIR + str(b.timestamp) + '.png', cv_image)
print('saved: ' + str(b.timestamp) + '.png')
f.write(str(b.timestamp) + '\n')
bag.close()
print('PROCESS COMPLETE')