plane.cpp
Go to the documentation of this file.
1 /*
2  velo2cam_calibration - Automatic calibration algorithm for extrinsic parameters of a stereo camera and a velodyne
3  Copyright (C) 2017-2018 Jorge Beltran, Carlos Guindel
4 
5  This file is part of velo2cam_calibration.
6 
7  velo2cam_calibration is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 2 of the License, or
10  (at your option) any later version.
11 
12  velo2cam_calibration is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with velo2cam_calibration. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 /*
22  plane: Find a plane model in the cloud using RANSAC
23 */
24 
25 #include <ros/ros.h>
26 #include <sensor_msgs/PointCloud2.h>
27 #include <pcl/ModelCoefficients.h>
28 #include <pcl/io/pcd_io.h>
29 #include <pcl/point_types.h>
30 #include <pcl/filters/filter.h>
31 #include <pcl/sample_consensus/method_types.h>
32 #include <pcl/sample_consensus/model_types.h>
33 #include <pcl/segmentation/sac_segmentation.h>
35 #include <pcl_msgs/PointIndices.h>
36 #include <pcl_msgs/ModelCoefficients.h>
37 #include <dynamic_reconfigure/server.h>
38 #include <velo2cam_calibration/PlaneConfig.h>
39 
41 {
42 public:
43 
48  double threshold_;
50  double eps_angle_;
51 
52  Eigen::Vector3f Axis;
53 
54  dynamic_reconfigure::Server<velo2cam_calibration::PlaneConfig> server;
55  dynamic_reconfigure::Server<velo2cam_calibration::PlaneConfig>::CallbackType f;
56 
58  nh_("~"), threshold_(0.1)
59  {
60  cloud_sub = nh_.subscribe<sensor_msgs::PointCloud2>("input", 1, &SACSegmentator::cloud_callback, this);
61  inliers_pub = nh_.advertise<pcl_msgs::PointIndices> ("inliers", 1);
62  coeff_pub = nh_.advertise<pcl_msgs::ModelCoefficients> ("model", 1);
63 
64  std::vector<double> axis_param;
65  Axis = Eigen::Vector3f::Zero();
66 
67  nh_.getParam("axis", axis_param);
68 
69  if (axis_param.size()==3){
70  for (int i=0; i<3; ++i){
71  Axis[i] = axis_param[i];
72  }
73  }else{
74  Axis[0]=0.0; Axis[1]=1.0; Axis[2]=0.0;
75  }
76 
77  nh_.param("eps_angle", eps_angle_, 0.35);
78 
79  // 0: SACMODEL_NORMAL_PLANE
80  // 1: SACMODEL_PARALLEL_PLANE
81  nh_.param("segmentation_type", sac_segmentation_type_, 0);
82 
83  if (sac_segmentation_type_ == 0){
84  ROS_INFO("Searching for planes perpendicular to %f %f %f", Axis[0], Axis[1], Axis[2]);
85  }else{
86  ROS_INFO("Searching for planes parallel to %f %f %f", Axis[0], Axis[1], Axis[2]);
87  }
88 
89  f = boost::bind(&SACSegmentator::param_callback, this, _1, _2);
90  server.setCallback(f);
91 
92  ROS_INFO("Segmentator ready");
93 
94  }
95 
96  void cloud_callback(const sensor_msgs::PointCloud2::ConstPtr &input)
97  {
98  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ> ());
99  pcl::fromROSMsg(*input,*cloud);
100 
101  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
102  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
103  // Create the segmentation object
104  pcl::SACSegmentation<pcl::PointXYZ> seg;
105 
106  seg.setModelType (sac_segmentation_type_ ? pcl::SACMODEL_PARALLEL_PLANE : pcl::SACMODEL_PERPENDICULAR_PLANE);
107  seg.setDistanceThreshold (threshold_);
108  seg.setMethodType (pcl::SAC_RANSAC);
109  seg.setAxis(Axis);
110  seg.setOptimizeCoefficients (true);
111 
112  seg.setEpsAngle(eps_angle_);
113  seg.setMaxIterations(250);
114 
115  std::vector<int> indices;
116  pcl::removeNaNFromPointCloud (*cloud, *cloud, indices);
117 
118  seg.setInputCloud (cloud->makeShared());
119  seg.segment (*inliers, *coefficients);
120 
121  if (inliers->indices.size () == 0)
122  {
123  ROS_WARN ("Could not estimate a planar model for the given dataset.");
124  return;
125  }
126 
127  pcl_msgs::PointIndices p_ind;
128 
129  pcl_conversions::moveFromPCL(*inliers, p_ind);
130  p_ind.header = input->header;
131 
132  pcl_msgs::ModelCoefficients m_coeff;
133 
134  pcl_conversions::moveFromPCL(*coefficients, m_coeff);
135  m_coeff.header = input->header;
136 
137  inliers_pub.publish(p_ind);
138  coeff_pub.publish(m_coeff);
139  }
140 
141  void param_callback(velo2cam_calibration::PlaneConfig &config, uint32_t level){
142  threshold_ = config.threshold;
143  ROS_INFO("New distance threshold: %f", threshold_);
144  }
145 };
146 
147 int main(int argc, char **argv)
148 {
149  ros::init(argc, argv, "plane_segmentation");
150 
151  SACSegmentator seg;
152 
153  ros::spin();
154 
155 }
double eps_angle_
Definition: plane.cpp:50
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
double threshold_
Definition: plane.cpp:48
void cloud_callback(const sensor_msgs::PointCloud2::ConstPtr &input)
Definition: plane.cpp:96
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void param_callback(velo2cam_calibration::PlaneConfig &config, uint32_t level)
Definition: plane.cpp:141
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
Eigen::Vector3f Axis
Definition: plane.cpp:52
ros::Publisher inliers_pub
Definition: plane.cpp:46
dynamic_reconfigure::Server< velo2cam_calibration::PlaneConfig > server
Definition: plane.cpp:54
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
int sac_segmentation_type_
Definition: plane.cpp:49
int main(int argc, char **argv)
Definition: plane.cpp:147
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher coeff_pub
Definition: plane.cpp:47
bool getParam(const std::string &key, std::string &s) const
SACSegmentator()
Definition: plane.cpp:57
dynamic_reconfigure::Server< velo2cam_calibration::PlaneConfig >::CallbackType f
Definition: plane.cpp:55
void moveFromPCL(pcl::PCLImage &pcl_image, sensor_msgs::Image &image)
ros::Subscriber cloud_sub
Definition: plane.cpp:45
ros::NodeHandle nh_
Definition: plane.cpp:44


velo2cam_calibration
Author(s): Jorge Beltran , Carlos Guindel
autogenerated on Thu Feb 28 2019 03:24:25