ROS系统下配置PCL库,使用RealSense实现ROS的sensor_msg::PointCloud2与PCL的pcl::PointCloud转换

水毕设水的过分了,感觉得认真做点儿东西了,毕设主要使用激光雷达和RGBD,摄像头

均需要PointCloud数据类型,毕设中使用遍历点云呢,觉得自己太蠢了,学学PCL库

提升提升算法效率

自己买了本书。。。感觉很垃圾。。。就不推荐了,不过配套源码还可以,不过有点儿小错(需要的留邮箱吧)

更推荐官方教程http://wiki.ros.org/pcl_ros

这里我使用读realsense的点云数据

realsense的安装教程

https://github.com/IntelRealSense/librealsense/blob/master/doc/distribution_linux.md

ros_realsense的安装教程

https://github.com/IntelRealSense/realsense-ros

#include <ros/ros.h>
// PCL specific includes
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
ros::Publisher pub;
typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
#include <pcl_ros/point_cloud.h>

void 
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
   // 声明存储原始数据与滤波后的数据的点云的 格式
  pcl::PCLPointCloud2* cloud = new pcl::PCLPointCloud2;    //原始的点云的数据格式
  pcl::PCLPointCloud2ConstPtr cloudPtr(cloud); 
  // 转化为PCL中的点云的数据格式
  pcl_conversions::toPCL(*input, *cloud);
  pub.publish (*cloud);
}

int
main (int argc, char** argv)
{
  // Initialize ROS
  ros::init (argc, argv, "ros_pcl2pcl");
  ros::NodeHandle nh;
  // Create a ROS subscriber for the input point cloud
  //"/camera/depth/color/points"realsense发布的点云数据
  ros::Subscriber sub = nh.subscribe ("/camera/depth/color/points", 1, cloud_cb);
  // Create a ROS publisher for the output point cloud
  pub = nh.advertise<pcl::PCLPointCloud2> ("output", 1);

  // Spin
  ros::spin ();
}

主要函数

  pcl_conversions::toPCL(*input, *cloud);

包含在pcl_conversions.h中

可以将输入的sensor::PointCLoud2数据类型转换成pcl::PCLPointCloud2

并且这种数据类型是直接可以使用ROS进行publish的

CMake中链接的库

find_package(catkin REQUIRED COMPONENTS
  OpenCV REQUIRED
  pcl_conversions
  pcl_ros
  roscpp
  sensor_msgs
  cv_bridge
  std_msgs 
  image_transport
)

效果如图所示,经过测试,KinectV2的点云同样ok,基本点云格式都可以进行转换

从而方便使用PCL库

pcl::PCLPointCloud2与sensor_msgs::PointCloud2转换主要就是这四个函数。

  inline
  void fromPCL(const pcl::PCLPointCloud2 &pcl_pc2, sensor_msgs::PointCloud2 &pc2)
  {
    copyPCLPointCloud2MetaData(pcl_pc2, pc2);
    pc2.data = pcl_pc2.data;
  }

  inline
  void moveFromPCL(pcl::PCLPointCloud2 &pcl_pc2, sensor_msgs::PointCloud2 &pc2)
  {
    copyPCLPointCloud2MetaData(pcl_pc2, pc2);
    pc2.data.swap(pcl_pc2.data);
  }
  inline
  void toPCL(const sensor_msgs::PointCloud2 &pc2, pcl::PCLPointCloud2 &pcl_pc2)
  {
    copyPointCloud2MetaData(pc2, pcl_pc2);
    pcl_pc2.data = pc2.data;
  }

  inline
  void moveToPCL(sensor_msgs::PointCloud2 &pc2, pcl::PCLPointCloud2 &pcl_pc2)
  {
    copyPointCloud2MetaData(pc2, pcl_pc2);
    pcl_pc2.data.swap(pc2.data);
  }

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值