赞
踩
- def capture_image(self, ret, image) -> CompressedImage:
-
- if ret:
-
- obj_cimage = CompressedImage()
-
- obj_head = Header()
-
-
- obj_head.frame_id = "camera"
-
- obj_head.stamp = rospy.Time.now()
-
- obj_head.seq = 0
-
- # 编码图像为PNG格式的二进制数据
-
- retval, buffer = cv2.imencode('.png', image)
-
- if retval:
-
- obj_cimage.data = np.array(buffer).tobytes()
-
- obj_cimage.format = "png"
-
- obj_cimage.header = obj_head
-
- return obj_cimage
-
- else:
-
- rospy.logerr("图像编码失败")
-
- else:
-
- rospy.logerr("没有摄像头数据")
- cvbridge=CvBridge()
-
- class Sub_image():
- def __init__(self) -> None:
- self.image_sub=rospy.Subscriber("/cam_image",CompressedImage,self.showcb,queue_size=1)
- def showcb(self,data):
- cv_image=cvbridge.compressed_imgmsg_to_cv2(data,"bgr8")
- cv2.imshow("dd",cv_image)
- cv2.waitKey(1)
-
- def main():
- rospy.init_node("test1",anonymous=True)
- # 实例化的时候自动就会执行init
- sub_obj=Sub_image()
- rospy.spin()
可以将图像通过cv2.imencode编码一下,就可以了,具体原因不知道为什么。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。