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> 54 dynamic_reconfigure::Server<velo2cam_calibration::PlaneConfig>
server;
55 dynamic_reconfigure::Server<velo2cam_calibration::PlaneConfig>::CallbackType
f;
58 nh_(
"~"), threshold_(0.1)
61 inliers_pub = nh_.
advertise<pcl_msgs::PointIndices> (
"inliers", 1);
62 coeff_pub = nh_.
advertise<pcl_msgs::ModelCoefficients> (
"model", 1);
64 std::vector<double> axis_param;
65 Axis = Eigen::Vector3f::Zero();
69 if (axis_param.size()==3){
70 for (
int i=0; i<3; ++i){
71 Axis[i] = axis_param[i];
74 Axis[0]=0.0; Axis[1]=1.0; Axis[2]=0.0;
77 nh_.
param(
"eps_angle", eps_angle_, 0.35);
81 nh_.
param(
"segmentation_type", sac_segmentation_type_, 0);
83 if (sac_segmentation_type_ == 0){
84 ROS_INFO(
"Searching for planes perpendicular to %f %f %f", Axis[0], Axis[1], Axis[2]);
86 ROS_INFO(
"Searching for planes parallel to %f %f %f", Axis[0], Axis[1], Axis[2]);
90 server.setCallback(f);
98 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (
new pcl::PointCloud<pcl::PointXYZ> ());
101 pcl::ModelCoefficients::Ptr coefficients (
new pcl::ModelCoefficients);
102 pcl::PointIndices::Ptr inliers (
new pcl::PointIndices);
104 pcl::SACSegmentation<pcl::PointXYZ> seg;
106 seg.setModelType (sac_segmentation_type_ ? pcl::SACMODEL_PARALLEL_PLANE : pcl::SACMODEL_PERPENDICULAR_PLANE);
107 seg.setDistanceThreshold (threshold_);
108 seg.setMethodType (pcl::SAC_RANSAC);
110 seg.setOptimizeCoefficients (
true);
112 seg.setEpsAngle(eps_angle_);
113 seg.setMaxIterations(250);
115 std::vector<int> indices;
116 pcl::removeNaNFromPointCloud (*cloud, *cloud, indices);
118 seg.setInputCloud (cloud->makeShared());
119 seg.segment (*inliers, *coefficients);
121 if (inliers->indices.size () == 0)
123 ROS_WARN (
"Could not estimate a planar model for the given dataset.");
127 pcl_msgs::PointIndices p_ind;
130 p_ind.header = input->header;
132 pcl_msgs::ModelCoefficients m_coeff;
135 m_coeff.header = input->header;
142 threshold_ = config.threshold;
143 ROS_INFO(
"New distance threshold: %f", threshold_);
147 int main(
int argc,
char **argv)
149 ros::init(argc, argv,
"plane_segmentation");
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())
void cloud_callback(const sensor_msgs::PointCloud2::ConstPtr &input)
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)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
ROSCPP_DECL void spin(Spinner &spinner)
ros::Publisher inliers_pub
dynamic_reconfigure::Server< velo2cam_calibration::PlaneConfig > server
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
int sac_segmentation_type_
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool getParam(const std::string &key, std::string &s) const
dynamic_reconfigure::Server< velo2cam_calibration::PlaneConfig >::CallbackType f
void moveFromPCL(pcl::PCLImage &pcl_image, sensor_msgs::Image &image)
ros::Subscriber cloud_sub