Ros_rosbag提取点云数据和图像数据


Linux环境

Ubuntu 16 xenial   Ubuntu 18 bionic  Ubuntu 20 focal
Debian 10 Buster   Debian 11 bullseye	

  ROS中表示点云的数据结构有: 
    sensor_msgs::PointCloud 
    sensor_msgs::PointCloud2 
    pcl::PointCloud

ROS1镜像运行

 ####1.拉取docker 环境
 docker pull osrf/ros:noetic-desktop-full	或者 docker pull osrf/ros:melodic-desktop-full
 
 ##运行docker环境
   docker run --name ros-pipe --restart always --add-host test.com:10.10.10.10 \
   -v /opt/code/workflow:/opt/code/workflow  \
   -it osrf/ros:noetic-desktop-full /bin/bash
   
 ###docker内运行
    ###docker环境内部执行
	确定是否有 roscore,可以搜索 whereis roscore
    如果没有的话,执行  source /ros_entrypoint.sh  
    roscore&
	
   1.查看bag包信息 
       rosbag info output.bag
   2.提取点云数据命令行--  bag文件转PCD文件
     http://wiki.ros.org/pcl_ro
       rosrun pcl_ros bag_to_pcd output.bag /com  /opt/pcd 
	   
	其他等价
       # Syntax is: /opt/ros/noetic/lib/pcl_ros/bag_to_pcd    []
        #Example: /opt/ros/noetic/lib/pcl_ros/bag_to_pcd  data.bag /laser_tilt_cloud ./pointclouds /base_link		
	 方法二:pointcloud_to_pcd
       一个终端通过ros发送messages,如:$ rosbag play XXX.bag
       另一个终端接收,             如:$ rosrun pcl_ros pointcloud_to_pcd input:=/velodyne_points	
   3.rostopic list -v	

   4.删除roscore进程
     killall -9 roscore
     killall -9 rosmaster
	 
	  roscore&
	  
###设置环境变量
	source /ros_entrypoint.sh  
     /ros_entrypoint.sh
     #!/bin/bash
     set -e
     # setup ros environment
     source "/opt/ros/$ROS_DISTRO/setup.bash"
     exec "$@"
  其中   
    /opt/ros/noetic/setup.bash
    #!/usr/bin/env bash
    # generated from catkin/cmake/templates/setup.bash.in
    CATKIN_SHELL=bash
    # source setup.sh from same directory as this file
    _CATKIN_SETUP_DIR=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)
    . "$_CATKIN_SETUP_DIR/setup.sh"

转换逻辑

 01.使用cv_bridge将ROS图像转换为OpenCV图像,反之亦然,从而实现ROS和OpenCV的交互
    CvBridge是一个ROS库,提供ROS和OpenCV之间的接口	
       要将ROS图像消息转换为cv :: Mat,模块cv_bridge.CvBridge提供以下功能
         cv_image = bridge.imgmsg_to_cv2(image_message, desired_encoding="bgr8")	
       将OpenCV图像转换为ROS图像消息
         image_message = cv2_to_imgmsg(cv_image, encoding="passthrough")		   
 02.  rosrun pcl_ros bag_to_pcd output.bag /com  /opt/pcd 
     "/opt/ros/noetic/lib/pcl_ros/bag_to_pcd {} {} {}".format(bag_path_nm, pc_topic, pcd_out_path) )


    介绍几个ROS节点运行的几种工具。他们的作用是ROS格式点云或包与点云数据(PCD)文件格式之间的相互转换。
    
    (1)bag_to_pcd
    用法:rosrun pcl_ros bag_to_pcd   
    读取一个包文件,保存所有ROS点云消息在指定的PCD文件中。
    
    (2)convert_pcd_to_image
    用法:rosrun pcl_ros convert_pcd_to_image 
    加载一个PCD文件,将其作为ROS图像消息每秒中发布五次。
    
    (3) convert_pointcloud_to_image
    用法:rosrun pcl_ros convert_pointcloud_to_image input:=/my_cloud output:=/my_imag
     查看图像:rosrun image_view image_view image:=/my_image
    订阅一个ROS的点云的话题并以图像的信息发布出去。
	
    (4)pcd_to_pointcloud
    用法:rosrun pcl_ros pcd_to_pointcloud  [  ]
     is the (required) file name to read.
     is the (optional) number of seconds to sleep between messages. If  is zero or not specified the message is published once.
    加载一个PCD文件,发布一次或多次作为ROS点云消息
	
    (5)pointcloud_to_pcd
    例如: rosrun pcl_ros pointcloud_to_pcd input:=/velodyne/pointcloud2
    订阅一个ROS的话题和保存为点云PCD文件。每个消息被保存到一个单独的文件,名称是由一个可自定义的前缀参数,ROS时间的消息,和以PCD扩展的文件

代码示例

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# ---------------------------
"""" 以def 开头 和以return 结尾的中间所有行"""
def get_between_def(file_nm_path):
    with open(file_nm_path,mode='r',encoding='utf8') as fobj:
        start_flag = False
        for num, line in enumerate(fobj):
            line = line.rstrip()
            if 'def' in line.rstrip() or start_flag:
                start_flag = True
                print(line)
                if  "return" in line.strip():
                    start_flag = False
                    print("hello world ##############",num)
    return start_flag


if __name__ == "__main__":
    file_nm= r"D:\Imag\flow\testFile.txt"
    get_between_def(file_nm) 
	
###说明
 引入第三方标识--通过改变标识来改变逻辑
 通过【subprocess.getstatusoutput】获得shell返回结
ROS