1. 程式人生 > 其它 >Ros_rosbag提取點雲資料和影象資料

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 <file_in.bag> <topic> <output_directory> [<target_frame>]
        #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 <input_file.bag> <topic> <output_directory>
    讀取一個包檔案,儲存所有ROS點雲訊息在指定的PCD檔案中。
    
    (2)convert_pcd_to_image
    用法:rosrun pcl_ros convert_pcd_to_image <cloud.pcd>
    載入一個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 <file.pcd> [ <interval> ]
    <file.pcd> is the (required) file name to read.
    <interval> is the (optional) number of seconds to sleep between messages. If <interval> 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返回結