【PCL使用】点云直通滤波

简单分享一下用PCL库做直通滤波的代码。ROS版本是Melodic。

ptf.cpp 

#include <iostream>
#include <vector>
#include <ctime>
#include <ros/ros.h> 
#include <sensor_msgs/PointCloud2.h> 
#include <pcl/point_cloud.h> 
#include <pcl_conversions/pcl_conversions.h> 
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/passthrough.h>

using namespace std;

class Pointcloud
{
public:
    Pointcloud()
    {
        // cout << ros::Time::now() << "  构建Pointcloud类  " << endl;
        pub = nh.advertise<sensor_msgs::PointCloud2>("/cloud_out", 1);
        sub = nh.subscribe("/lidar_points",3,&Pointcloud::CallBack_PassThroughFilter,this);
        SetFilter_PassThroughFilter();
    }

    void SetFilter_PassThroughFilter()
    {
        // cout << ros::Time::now() << "  配置滤波器  " << endl;
        cloudFilter_X.setFilterFieldName("x"); 
        cloudFilter_X.setFilterLimits(-10.0, 10.0);
        
        cloudFilter_Y.setFilterFieldName("y"); 
        cloudFilter_Y.setFilterLimits(-10.0, 10.0);
        
        cloudFilter_Z.setFilterFieldName("z");
        cloudFilter_Z.setFilterLimits(-4.0, 4.0);
        
    }

    void RunFilter_PassThroughFilter(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_ptr) 
    {
        // cout << ros::Time::now() << "  执行滤波  " << endl;
        cloudFilter_X.setInputCloud(cloud_ptr);
        cloudFilter_Y.setInputCloud(cloud_ptr);
        cloudFilter_Z.setInputCloud(cloud_ptr);
        cloudFilter_X.filter(*cloud_ptr);
        cloudFilter_Y.filter(*cloud_ptr);
        cloudFilter_Z.filter(*cloud_ptr);
    }

    void CallBack_PassThroughFilter(const sensor_msgs::PointCloud2ConstPtr input)
    {
        // time_start = clock();

	    pcl::fromROSMsg(*input,*cloud_in);
        RunFilter_PassThroughFilter(cloud_in);
 	    pcl::toROSMsg(*cloud_in,cloud_out);
        cloud_out.header.stamp = ros::Time::now();
        cloud_out.header.frame_id = "laser";
        pub.publish(cloud_out);

        // time_end = clock();
        // float timePhase = (float(time_end-time_start))/CLOCKS_PER_SEC;
        // cout << "回调函数执行用时" << timePhase << "秒" << endl;
    }

    ~Pointcloud()
    {
        // cout << ros::Time::now() << "  析构Pointcloud类  " << endl;
    }

private:
    ros::NodeHandle nh;
    ros::Publisher pub;
    ros::Subscriber sub;
    //转换成pcl格式
    pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_in{new pcl::PointCloud<pcl::PointXYZI>};
    //转换成ros格式
    sensor_msgs::PointCloud2 cloud_out;

    //PassThroughFilter
    pcl::PassThrough<pcl::PointXYZI> cloudFilter_Z;
    pcl::PassThrough<pcl::PointXYZI> cloudFilter_Y;
    pcl::PassThrough<pcl::PointXYZI> cloudFilter_X;

    // clock_t time_start;
    // clock_t time_end;
    // float timePhase;
};



int main(int argc, char **argv)
{
    ros::init(argc,argv,"Pointcloud");
    Pointcloud Pointcloud1;
    ros::spin();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(ptf)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  sensor_msgs
  pcl_ros
  pcl_conversions
)

catkin_package(
CATKIN_DEPENDS 
roscpp 
pcl_ros 
sensor_msgs 
std_msgs 
pcl_conversions
)

include_directories(
 include
 ${catkin_INCLUDE_DIRS}
 ${PCL_INCLUDE_DIR}
)

add_executable(ptf /home/xwcd/Project/xw_smartcar/src/PassThroughFilter/src/ptf.cpp)
target_link_libraries(ptf ${catkin_LIBRARIES})

package.xml

<?xml version="1.0"?>
<package format="2">
  <name>ptf</name>
  <version>0.0.0</version>
  <description>The ptf package</description>
  <maintainer email="csdn@qq.com">csdn</maintainer>
  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>

  <build_depend>roscpp</build_depend>
  <exec_depend>roscpp</exec_depend>

  <build_depend>libpcl-all-dev</build_depend>
  <exec_depend>libpcl-all</exec_depend>

  <build_depend>sensor_msgs</build_depend>
  <exec_depend>sensor_msgs</exec_depend>

  <build_depend>std_msgs</build_depend>
  <exec_depend>std_msgs</exec_depend>

  <build_depend>message_runtime</build_depend>
  <exec_depend>message_runtime</exec_depend>

  <exec_depend>message_generation</exec_depend>

  <build_depend>pcl_ros</build_depend>
  <exec_depend>pcl_ros</exec_depend>

  <build_depend>pcl_conversions</build_depend>
  <exec_depend>pcl_conversions</exec_depend>

  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>message_generation</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->
  </export>
</package>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值