38 #include <pcl/filters/filter.h> 41 #include <sensor_msgs/PointCloud2.h> 44 #include <segmentation/segmentation.hpp> 45 #include <cpf_segmentation_ros/DoSegmentation.h> 46 #include <cpf_segmentation_ros/DoSegmentationAction.h> 47 #include <cpf_segmentation_ros/EnablePublisher.h> 51 #include <boost/thread.hpp> 66 publisherEnabled_(true),
67 actionServer_(nh_,
"segmentation", false) {
69 nh_.getParam(
"pointcloud_topic", pc_topic);
71 if (pc_topic.empty()) {
72 pc_topic =
"/realsense/points_aligned";
78 nh_.advertise<sensor_msgs::PointCloud2>(
"segmented_pointcloud", 1);
81 doSegmentationSrv_ = nh_.advertiseService(
83 enablePublisherSrv_ = nh_.advertiseService(
87 actionServer_.registerGoalCallback(
90 actionServer_.start();
92 Config config = segmentation.getConfig();
95 nh_.param<
float>(
"voxel_resolution", config.voxel_resolution, 0.003f);
96 nh_.param<
float>(
"seed_resolution", config.seed_resolution, 0.05f);
98 nh_.param<
float>(
"color_importance", config.color_importance, 1.0f);
99 nh_.param<
float>(
"spatial_importance", config.spatial_importance, 0.4f);
100 nh_.param<
float>(
"normal_importance", config.normal_importance, 1.0f);
102 nh_.param<
bool>(
"use_single_cam_transform",
103 config.use_single_cam_transform,
false);
104 nh_.param<
bool>(
"use_supervoxel_refinement",
105 config.use_supervoxel_refinement,
false);
107 nh_.param<
bool>(
"use_random_sampling", config.use_random_sampling,
109 nh_.param<
float>(
"outlier_cost", config.outlier_cost, 0.02f);
110 nh_.param<
float>(
"smooth_cost", config.smooth_cost,
111 config.outlier_cost * 0.01);
113 nh_.param<
int>(
"min_inliers_per_plane", config.min_inliers_per_plane,
116 "label_cost", config.label_cost,
117 config.min_inliers_per_plane * 0.5 * config.outlier_cost);
119 nh_.param<
int>(
"max_num_iterations", config.max_num_iterations, 25);
120 nh_.param<
float>(
"max_curvature", config.max_curvature, 0.001f);
121 nh_.param<
int>(
"gc_scale", config.gc_scale, 1e4);
123 segmentation.setConfig(config);
130 actionServer_.acceptNewGoal();
131 publisherEnabled_ =
false;
132 segmentation_thread =
new boost::thread(
142 cpf_segmentation_ros::DoSegmentationResult result;
143 pcl::PointCloud<pcl::PointXYZL>::Ptr segmented_pc_ptr;
145 std::vector<int> removedIndices;
146 pcl::removeNaNFromPointCloud(cloud_, cloud_, removedIndices);
148 if (cloud_.size() > 0) {
149 segmentation.setPointCloud(cloud_.makeShared());
150 segmentation.doSegmentation();
151 segmented_pc_ptr = segmentation.getSegmentedPointCloud();
153 pcl::PointCloud<PointXYZL> cloud2_;
155 pcl::copyPointCloud(*segmented_pc_ptr, cloud2_);
158 BOOST_FOREACH (pcl::PointXYZL point, *segmented_pc_ptr) {
159 if (point.label == 0)
continue;
160 cloud2_.push_back(point);
165 actionServer_.setSucceeded(result);
167 ROS_ERROR(
"No points in cloud. Aborting..\n");
168 actionServer_.setAborted(result);
179 cpf_segmentation_ros::EnablePublisher::Response &resp) {
180 ROS_INFO(
"Publisher set to %d\n", req.enable);
181 publisherEnabled_ = req.enable;
192 cpf_segmentation_ros::DoSegmentation::Response &res) {
193 sensor_msgs::PointCloud2 seg_msg;
194 pcl::PointCloud<pcl::PointXYZL>::Ptr segmented_pc_ptr;
198 std::vector<int> removedIndices;
199 pcl::removeNaNFromPointCloud(cloud_, cloud_, removedIndices);
201 ROS_INFO(
"Got cloud %lu points\n", cloud_.size());
203 segmentation.setPointCloud(cloud_.makeShared());
205 segmentation.doSegmentation();
207 segmented_pc_ptr = segmentation.getSegmentedPointCloud();
209 pcl::PointCloud<PointXYZL> cloud2_;
210 if (cloud_.size() > 0) {
211 pcl::copyPointCloud(*segmented_pc_ptr, cloud2_);
214 BOOST_FOREACH (pcl::PointXYZL point, *segmented_pc_ptr) {
215 if (point.label == 0)
continue;
216 cloud2_.push_back(point);
219 res.segmented_cloud.header = req.input_cloud.header;
220 segmented_pub_.publish(res.segmented_cloud);
222 res.segmented_cloud.header = req.input_cloud.header;
233 if (publisherEnabled_ && cloud_.size() > 0) {
234 sensor_msgs::PointCloud2 seg_msg;
235 pcl::PointCloud<pcl::PointXYZL>::Ptr segmented_pc_ptr;
237 std::vector<int> removedIndices;
238 pcl::removeNaNFromPointCloud(cloud_, cloud_, removedIndices);
240 segmentation.setPointCloud(cloud_.makeShared());
242 segmentation.doSegmentation();
244 segmented_pc_ptr = segmentation.getSegmentedPointCloud();
246 if (cloud_.size() > 0) {
247 pcl::PointCloud<PointXYZL> cloud2_;
249 pcl::copyPointCloud(*segmented_pc_ptr, cloud2_);
252 BOOST_FOREACH (pcl::PointXYZL point, *segmented_pc_ptr) {
253 if (point.label == 0)
continue;
254 cloud2_.push_back(point);
258 segmented_pub_.publish(seg_msg);
286 int main(
int argc,
char **argv) {
287 ros::init(argc, argv,
"segmentation_node");
boost::thread * segmentation_thread
ros::Publisher segmented_pub_
void scan_callback(const sensor_msgs::PointCloud2ConstPtr &cloud_ptr)
scan_callback
ros::ServiceServer enablePublisherSrv_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
int main(int argc, char **argv)
SegmentationNode(ros::NodeHandle nh)
void goalCB()
goalCB callback funtion for the new goal
actionlib::SimpleActionServer< cpf_segmentation_ros::DoSegmentationAction > actionServer_
void doSegmentation()
doSegmentation thread for doing the segmentation
bool doSegmentation(cpf_segmentation_ros::DoSegmentation::Request &req, cpf_segmentation_ros::DoSegmentation::Response &res)
doSegmentation service callback for synchronized processing
bool enablePublisher(cpf_segmentation_ros::EnablePublisher::Request &req, cpf_segmentation_ros::EnablePublisher::Response &resp)
enablePublisher
ros::ServiceServer doSegmentationSrv_
Segmentation segmentation
void toROSMsg(const sensor_msgs::PointCloud2 &cloud, sensor_msgs::Image &image)
pcl::PointCloud< PointT > cloud_