ROS直接pub compressed image.

本文讲解了如何在ROS中高效地发布压缩后的图像数据,避免了解码和编码带来的性能损耗,通过直接使用sensor_msgs::CompressedImage类型发布jpeg格式的图片,提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果用ros的 image_transport::Publisher 发布图片的话则,ros会自动添加一个 image/compressed的topic,将原始图像编码,编码方式可以设置为png\jpeg.
有时候我们获取到的图片就是编码之后的数据,这个时候如果将解码转成 cv::Mat 再pub的话 性能开销比较大,并且数据损失更大. 所以我们可以直接pub compressed.
注意 我们使用的并不是 compressed_publisher

	#include <>
	ros::init(argc, argv, "nodename");
	ros::NodeHandle nh;
	
	ros::Publisher pub = nh.advertise<sensor_msgs::CompressedImage>("node/image/compressed", 100);
	sensor_msgs::CompressedImage jpeg_img;
	jpeg_img.format = "jpeg";
	jpeg_img.header.stamp.sec = times;
	jpeg_img.header.stamp.nsec = timeu*1000;
	jpeg_img.header.seq = kSeq++;
	// pimage :pointer to jpeg image buffer
	// image_pos : buffer size
	std::vector<unsigned char> jpeg_buffer(pimage, pimage+image_pos);
	jpeg_img.data.swap(jpeg_buffer);
	pub.publish(jpeg_img);
	

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import cv2 import torch import rospy import numpy as np from ultralytics import YOLO from time import time from std_msgs.msg import Header from sensor_msgs.msg import Image from yolov8_ros_msgs.msg import BoundingBox, BoundingBoxes class Yolo_Dect: def __init__(self): # load parameters weight_path = rospy.get_param('~weight_path', '') image_topic = rospy.get_param( '~image_topic', '/camClassera/color/image_raw') pub_topic = rospy.get_param('~pub_topic', '/yolov8/BoundingBoxes') self.camera_frame = rospy.get_param('~camera_frame', '') conf = rospy.get_param('~conf', '0.5') self.visualize = rospy.get_param('~visualize', 'True') # which device will be used if (rospy.get_param('/use_cpu', 'false')): self.device = 'cpu' else: self.device = 'cuda' self.model = YOLO(weight_path) self.model.fuse() self.model.conf = conf self.color_image = Image() self.getImageStatus = False # Load class color self.classes_colors = {} # image subscribe self.color_sub = rospy.Subscriber(image_topic, Image, self.image_callback, queue_size=1, buff_size=52428800) # output publishers self.position_pub = rospy.Publisher( pub_topic, BoundingBoxes, queue_size=1) self.image_pub = rospy.Publisher( '/yolov8/detection_image', Image, queue_size=1) # if no image messages while (not self.getImageStatus): rospy.loginfo("waiting for image.") rospy.sleep(2) def image_callback(self, image): self.boundingBoxes = BoundingBoxes() self.boundingBoxes.header = image.header self.boundingBoxes.image_header = image.header self.getImageStatus = True self.color_image = np.frombuffer(image.data, dtype=np.uint8).reshape( image.height, image.width, -1) self.color_image = cv2.cvtColor(self.color_image, cv2.COLOR_BGR2RGB) results = self.model(self.color_image, show=False, conf=0.3) self.dectshow(results, image.height, image.width) cv2.waitKey(3) def dectshow(self, results, height, width): self.frame = results[0].plot() print(str(results[0].speed['inference'])) fps = 1000.0/ results[0].speed['inference'] cv2.putText(self.frame, f'FPS: {int(fps)}', (20,50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA) for result in results[0].boxes: boundingBox = BoundingBox() boundingBox.xmin = np.int64(result.xyxy[0][0].item()) boundingBox.ymin = np.int64(result.xyxy[0][1].item()) boundingBox.xmax = np.int64(result.xyxy[0][2].item()) boundingBox.ymax = np.int64(result.xyxy[0][3].item()) boundingBox.Class = results[0].names[result.cls.item()] boundingBox.probability = result.conf.item() self.boundingBoxes.bounding_boxes.append(boundingBox) self.position_pub.publish(self.boundingBoxes) self.publish_image(self.frame, height, width) if self.visualize : cv2.imshow('YOLOv8', self.frame) def publish_image(self, imgdata, height, width): image_temp = Image() header = Header(stamp=rospy.Time.now()) header.frame_id = self.camera_frame image_temp.height = height image_temp.width = width image_temp.encoding = 'bgr8' image_temp.data = np.array(imgdata).tobytes() image_temp.header = header image_temp.step = width * 3 self.image_pub.publish(image_temp) def main(): rospy.init_node('yolov8_ros', anonymous=True) yolo_dect = Yolo_Dect() rospy.spin() if __name__ == "__main__": main()详细解释上述代码并解释其每个函数作用
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值