point_cloud_filtering.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <std_msgs/String.h>
00004 
00005 #include <pcl/point_types.h>
00006 #include <pcl_ros/point_cloud.h>
00007 #include <pcl/filters/voxel_grid.h>
00008 #include <pcl/filters/passthrough.h>
00009 #include <pcl/filters/extract_indices.h>
00010 
00011 typedef pcl::PointXYZRGB      Point;
00012 typedef pcl::PointCloud<Point>PointCloud;
00013 
00014 class PointCloudFiltering {
00015 
00016   // ROS properties
00017   ros::NodeHandle nh_;
00018   ros::NodeHandle nh_private_;
00019   ros::Subscriber point_cloud_sub_;
00020 
00021   // Publisher to send out the filtered point cloud
00022   ros::Publisher point_cloud_filtered_;
00023 
00024   // Filter parameters
00025   double x_filter_min_;
00026   double x_filter_max_;
00027   double y_filter_min_;
00028   double y_filter_max_;
00029   double z_filter_min_;
00030   double z_filter_max_;
00031   double voxel_size_;
00032 
00033 public:
00034 
00038   PointCloudFiltering() : nh_private_("~")
00039   {
00040     // Read the parameters from the parameter server (set defaults)
00041     nh_private_.param("x_filter_min", x_filter_min_, -3.0);
00042     nh_private_.param("x_filter_max", x_filter_max_, 3.0);
00043     nh_private_.param("y_filter_min", y_filter_min_, -3.0);
00044     nh_private_.param("y_filter_max", y_filter_max_, 3.0);
00045     nh_private_.param("z_filter_min", z_filter_min_, 0.2);
00046     nh_private_.param("z_filter_max", z_filter_max_, 3.0);
00047     nh_private_.param("voxel_size", voxel_size_, 0.01);
00048 
00049     // Subscription to the point cloud result from stereo_image_proc
00050     point_cloud_sub_ = nh_.subscribe<PointCloud>(
00051       "points2",
00052       1,
00053       &PointCloudFiltering::
00054       pointCloudCb,
00055       this);
00056 
00057     // Declare the point cloud filtered topic
00058     point_cloud_filtered_ = nh_private_.advertise<PointCloud>("points2_filtered", 1);
00059   }
00060 
00064   void pointCloudCb(const PointCloud::ConstPtr& point_cloud)
00065   {
00066     PointCloud cloud = *point_cloud;
00067 
00068     // Filter
00069     PointCloud::Ptr cloud_downsampled = filter(cloud.makeShared());
00070 
00071     // Publish the filtered point cloud
00072     point_cloud_filtered_.publish(cloud_downsampled);
00073   }
00074 
00079   PointCloud::Ptr filter(PointCloud::Ptr cloud)
00080   {
00081     // Copy the point cloud
00082     PointCloud::Ptr cloud_ptr(new PointCloud);
00083 
00084     // NAN and limit filtering
00085     PointCloud::Ptr cloud_filtered_ptr(new PointCloud);
00086     pcl::PassThrough<Point> pass_;
00087 
00088     // X-filtering
00089     pass_.setFilterFieldName("x");
00090     pass_.setFilterLimits(x_filter_min_, x_filter_max_);
00091     pass_.setInputCloud(cloud);
00092     pass_.filter(*cloud_filtered_ptr);
00093 
00094     // Y-filtering
00095     pass_.setFilterFieldName("y");
00096     pass_.setFilterLimits(y_filter_min_, y_filter_max_);
00097     pass_.setInputCloud(cloud_filtered_ptr);
00098     pass_.filter(*cloud_filtered_ptr);
00099 
00100     // Z-filtering
00101     pass_.setFilterFieldName("z");
00102     pass_.setFilterLimits(z_filter_min_, z_filter_max_);
00103     pass_.setInputCloud(cloud);
00104     pass_.filter(*cloud_filtered_ptr);
00105 
00106     // Downsampling using voxel grid and limit filtering
00107     pcl::VoxelGrid<Point> grid_;
00108     PointCloud::Ptr cloud_downsampled_ptr(new PointCloud);
00109     double plane_detection_voxel_size_ = voxel_size_;
00110 
00111     grid_.setLeafSize(plane_detection_voxel_size_,
00112                       plane_detection_voxel_size_,
00113                       plane_detection_voxel_size_);
00114     grid_.setDownsampleAllData(true);
00115     grid_.setInputCloud(cloud_filtered_ptr);
00116     grid_.filter(*cloud_downsampled_ptr);
00117 
00118     return cloud_downsampled_ptr;
00119   }
00120 };
00121 
00125 int main(int argc, char **argv)
00126 {
00127   ros::init(argc, argv, "point_cloud_filtering");
00128   PointCloudFiltering node;
00129   ros::spin();
00130   return 0;
00131 }
00132 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


point_cloud_filtering
Author(s): plnegre
autogenerated on Tue May 28 2013 12:09:59