Repository navigation
Expand file tree
/
Copy pathsegmentation.cpp
More file actions
218 lines (151 loc) · 6.78 KB
/
Copy pathsegmentation.cpp
File metadata and controls
218 lines (151 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
ROS node for point cloud cluster based segmentaion of cluttered objects on table
Author: Sean Cassero
7/15/15
*/
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/passthrough.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/segmentation/extract_clusters.h>
#include <obj_recognition/SegmentedClustersArray.h>
#include <obj_recognition/ClusterData.h>
class segmentation {
public:
explicit segmentation(ros::NodeHandle nh) : m_nh(nh) {
// define the subscriber and publisher
m_sub = m_nh.subscribe ("/obj_recognition/point_cloud", 1, &segmentation::cloud_cb, this);
m_clusterPub = m_nh.advertise<obj_recognition::SegmentedClustersArray> ("obj_recognition/pcl_clusters",1);
}
private:
ros::NodeHandle m_nh;
ros::Publisher m_pub;
ros::Subscriber m_sub;
ros::Publisher m_clusterPub;
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr& cloud_msg);
}; // end class definition
// define callback function
void segmentation::cloud_cb (const sensor_msgs::PointCloud2ConstPtr& cloud_msg)
{
// Container for original & filtered data
pcl::PCLPointCloud2* cloud = new pcl::PCLPointCloud2;
pcl::PCLPointCloud2ConstPtr cloudPtr(cloud);
pcl::PCLPointCloud2* cloud_filtered = new pcl::PCLPointCloud2;
pcl::PCLPointCloud2Ptr cloudFilteredPtr (cloud_filtered);
// Convert to PCL data type
pcl_conversions::toPCL(*cloud_msg, *cloud);
// Perform voxel grid downsampling filtering
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud (cloudPtr);
sor.setLeafSize (0.01, 0.01, 0.01);
sor.filter (*cloudFilteredPtr);
pcl::PointCloud<pcl::PointXYZRGB> *xyz_cloud = new pcl::PointCloud<pcl::PointXYZRGB>;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr xyzCloudPtr (xyz_cloud); // need a boost shared pointer for pcl function inputs
// convert the pcl::PointCloud2 tpye to pcl::PointCloud<pcl::PointXYZRGB>
pcl::fromPCLPointCloud2(*cloudFilteredPtr, *xyzCloudPtr);
//perform passthrough filtering to remove table leg
// create a pcl object to hold the passthrough filtered results
pcl::PointCloud<pcl::PointXYZRGB> *xyz_cloud_filtered = new pcl::PointCloud<pcl::PointXYZRGB>;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr xyzCloudPtrFiltered (xyz_cloud_filtered);
// Create the filtering object
pcl::PassThrough<pcl::PointXYZRGB> pass;
pass.setInputCloud (xyzCloudPtr);
pass.setFilterFieldName ("z");
pass.setFilterLimits (.5, 1.1);
//pass.setFilterLimitsNegative (true);
pass.filter (*xyzCloudPtrFiltered);
// create a pcl object to hold the ransac filtered results
pcl::PointCloud<pcl::PointXYZRGB> *xyz_cloud_ransac_filtered = new pcl::PointCloud<pcl::PointXYZRGB>;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr xyzCloudPtrRansacFiltered (xyz_cloud_ransac_filtered);
// perform ransac planar filtration to remove table top
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
// Create the segmentation object
pcl::SACSegmentation<pcl::PointXYZRGB> seg1;
// Optional
seg1.setOptimizeCoefficients (true);
// Mandatory
seg1.setModelType (pcl::SACMODEL_PLANE);
seg1.setMethodType (pcl::SAC_RANSAC);
seg1.setDistanceThreshold (0.04);
seg1.setInputCloud (xyzCloudPtrFiltered);
seg1.segment (*inliers, *coefficients);
// Create the filtering object
pcl::ExtractIndices<pcl::PointXYZRGB> extract;
//extract.setInputCloud (xyzCloudPtrFiltered);
extract.setInputCloud (xyzCloudPtrFiltered);
extract.setIndices (inliers);
extract.setNegative (true);
extract.filter (*xyzCloudPtrRansacFiltered);
// perform euclidean cluster segmentation to seporate individual objects
// Create the KdTree object for the search method of the extraction
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>);
tree->setInputCloud (xyzCloudPtrRansacFiltered);
// create the extraction object for the clusters
std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec;
// specify euclidean cluster parameters
ec.setClusterTolerance (0.02); // 2cm
ec.setMinClusterSize (100);
ec.setMaxClusterSize (25000);
ec.setSearchMethod (tree);
ec.setInputCloud (xyzCloudPtrRansacFiltered);
// exctract the indices pertaining to each cluster and store in a vector of pcl::PointIndices
ec.extract (cluster_indices);
// declare an instance of the SegmentedClustersArray message
obj_recognition::SegmentedClustersArray CloudClusters;
// declare the output variable instances
sensor_msgs::PointCloud2 output;
pcl::PCLPointCloud2 outputPCL;
// here, cluster_indices is a vector of indices for each cluster. iterate through each indices object to work with them seporately
for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
{
// create a new clusterData message object
//obj_recognition::ClusterData clusterData;
// create a pcl object to hold the extracted cluster
pcl::PointCloud<pcl::PointXYZRGB> *cluster = new pcl::PointCloud<pcl::PointXYZRGB>;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr clusterPtr (cluster);
// now we are in a vector of indices pertaining to a single cluster.
// Assign each point corresponding to this cluster in xyzCloudPtrPassthroughFiltered a specific color for identification purposes
for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); ++pit)
{
clusterPtr->points.push_back(xyzCloudPtrRansacFiltered->points[*pit]);
}
// log the position of the cluster
//clusterData.position[0] = (*cloudPtr).data[0];
//clusterData.position[1] = (*cloudPtr).points.back().y;
//clusterData.position[2] = (*cloudPtr).points.back().z;
//std::string info_string = string(cloudPtr->points.back().x);
//printf(clusterData.position[0]);
// convert to pcl::PCLPointCloud2
pcl::toPCLPointCloud2( *clusterPtr ,outputPCL);
// Convert to ROS data type
pcl_conversions::fromPCL(outputPCL, output);
// add the cluster to the array message
//clusterData.cluster = output;
CloudClusters.clusters.push_back(output);
}
// publish the clusters
m_clusterPub.publish(CloudClusters);
}
int main (int argc, char** argv)
{
// Initialize ROS
ros::init (argc, argv, "segmentation");
ros::NodeHandle nh;
segmentation segs(nh);
while(ros::ok())
ros::spin ();
}