python 读取rosbag数据中的image_rects话题,并显示视频

代码: 

import rosbag,time,cv2
import numpy as np


def readBag():
    bagFile = r"D:\camera_det\camera_det1.bag"
    # print(bagreader(bagFile).topic_table)  # 查看哪些话题
    bag = rosbag.Bag(bagFile, "r")
    bag_data = bag.read_messages("/image_rects")

    for topic, msg , t in bag_data:
        cv_img = np.frombuffer(msg.data, dtype=np.uint8).reshape(msg.height, msg.width, -1)
        img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
        yield img

        # cv2.imshow("cv_img", cv_img)
        # cv2.imwrite("cv_img"+str(index)+".jpg",cv_img)
        # index+=1
        # time.sleep(1)


if __name__ == '__main__':
    frame = readBag()
    while True:
        try:
            frame1 = next(frame)  # 取一帧
            cv2.imshow('frame', frame1)
            time.sleep(0.1)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        except:
            break

结果:

你可能感兴趣的:(工具,python,opencv,计算机视觉)