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


velo2cam_calibration
Author(s): Jorge Beltran , Carlos Guindel
autogenerated on Fri Feb 26 2021 03:40:57