def ros2cv(msg, scale=1, offset=0): if hasattr(msg, 'format'): return compressed_imgmsg_to_cv2(msg) if 'FC' in msg.encoding: passimg = numpy.nan_to_num(imgmsg_to_cv2(msg)) valscaled = cv2.convertScaleAbs(passimg, None, scale, offset) return valscaled mono = msg.encoding.startswith('mono') or msg.encoding[-1] in [ '1', 'U', 'S', 'F' ] return imgmsg_to_cv2(msg, 'mono8' if mono else 'bgr8')
def images(cam): """Extract images from input stream to jpg files. Args: cam: Input stream of raw rosbag messages. Returns: File instances for images of input stream. """ # Set output stream title and pull first message yield marv.set_header(title=cam.topic) # Fetch and process first 20 image messages name_template = '%s-{}.jpg' % cam.topic.replace('/', ':')[1:] while True: idx, msg = yield marv.pull(cam, enumerate=True) if msg is None or idx >= 20: break # Deserialize raw ros message pytype = get_message_type(cam) rosmsg = pytype() rosmsg.deserialize(msg.data) # Write image to jpeg and push it to output stream img = imgmsg_to_cv2(rosmsg, 'rgb8') name = name_template.format(idx) imgfile = yield marv.make_file(name) cv2.imwrite(imgfile.path, img) yield marv.push(imgfile)
def image(cam): """Extract first image of input stream to jpg file. Args: cam: Input stream of raw rosbag messages. Returns: File instance for first image of input stream. """ # Set output stream title and pull first message yield marv.set_header(title=cam.topic) msg = yield marv.pull(cam) if msg is None: return # Deserialize raw ros message pytype = get_message_type(cam) rosmsg = pytype() rosmsg.deserialize(msg.data) # Write image to jpeg and push it to output stream name = f"{cam.topic.replace('/', ':')[1:]}.jpg" imgfile = yield marv.make_file(name) img = imgmsg_to_cv2(rosmsg, 'rgb8') cv2.imwrite(imgfile.path, img, (cv2.IMWRITE_JPEG_QUALITY, 60)) yield marv.push(imgfile)
def imgsrc(stream): """Convert ROS sensor_msgs/Image stream into cv2 image stream.""" while True: rosmsg = yield marv.pull(stream) if rosmsg is None: break try: img = imgmsg_to_cv2(rosmsg, 'bgr8') except (ImageFormatError, ImageConversionError) as err: log = yield marv.get_logger() log.error('could not convert image from topic %s: %s ', stream.topic, err) raise marv.Abort() yield marv.push(img)