16 #include <sensor_msgs/PointCloud2.h>
19 #include <geometry_msgs/TransformStamped.h>
22 #include <visualization_msgs/Marker.h>
23 #include <visualization_msgs/MarkerArray.h>
25 #include <pcl/point_cloud.h>
26 #include <pcl/point_types.h>
27 #include <pcl/common/common.h>
28 #include <pcl/filters/passthrough.h>
29 #include <pcl/filters/voxel_grid.h>
30 #include <pcl/kdtree/kdtree.h>
31 #include <pcl/segmentation/extract_clusters.h>
38 const std::string &frame_id,
39 const pcl::PointXYZRGB &min_pt,
const pcl::PointXYZRGB &max_pt)
46 marker->ns =
"/sciurus17/example";
47 marker->id = marker_id;
48 marker->type = visualization_msgs::Marker::CUBE;
49 marker->action = visualization_msgs::Marker::ADD;
52 marker->pose.position.x = (max_pt.x + min_pt.x) * 0.5;
53 marker->pose.position.y = (max_pt.y + min_pt.y) * 0.5;
54 marker->pose.position.z = (max_pt.z + min_pt.z) * 0.5;
55 marker->pose.orientation.x = 0.0;
56 marker->pose.orientation.y = 0.0;
57 marker->pose.orientation.z = 0.0;
58 marker->pose.orientation.w = 1.0;
59 marker->scale.x = (max_pt.x - min_pt.x);
60 marker->scale.y = (max_pt.y - min_pt.y);
61 marker->scale.z = (max_pt.z - min_pt.z);
62 marker->color.a = 1.0;
63 marker->color.r = 1.0;
64 marker->color.g = 1.0;
65 marker->color.b = 1.0;
69 void cloud_cb (
const sensor_msgs::PointCloud2ConstPtr& cloud_msg)
71 const static std::string FRAME_ID =
"base_link";
80 const int CLUSTER_MAX = 10;
81 const int CLUSTER_COLOR[CLUSTER_MAX][COLOR_MAX] = {
82 {230, 0, 18},{243, 152, 18}, {255, 251, 0},
83 {143, 195, 31},{0, 153, 68}, {0, 158, 150},
84 {0, 160, 233},{0, 104, 183}, {29, 32, 136},
88 sensor_msgs::PointCloud2 cloud_transformed;
108 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (
new pcl::PointCloud<pcl::PointXYZRGB>());
113 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_passthrough (
new pcl::PointCloud<pcl::PointXYZRGB>());
114 pcl::PassThrough<pcl::PointXYZRGB> pass;
115 pass.setInputCloud (cloud);
116 pass.setFilterFieldName (
"z");
117 pass.setFilterLimits (0.02, 1.0);
118 pass.filter (*cloud_passthrough);
122 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_voxelgrid (
new pcl::PointCloud<pcl::PointXYZRGB>());
123 pcl::VoxelGrid<pcl::PointXYZRGB> voxelgrid;
124 voxelgrid.setInputCloud (cloud_passthrough);
125 voxelgrid.setLeafSize (0.01, 0.01, 0.01);
126 voxelgrid.filter (*cloud_voxelgrid);
130 if(cloud_voxelgrid->size() <= 0){
136 pcl::search::KdTree<pcl::PointXYZRGB>::Ptr
tree (
new pcl::search::KdTree<pcl::PointXYZRGB>());
137 tree->setInputCloud (cloud_voxelgrid);
139 std::vector<pcl::PointIndices> cluster_indices;
140 pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec;
141 ec.setClusterTolerance (0.02);
142 ec.setMinClusterSize (10);
143 ec.setMaxClusterSize (250);
144 ec.setSearchMethod (
tree);
145 ec.setInputCloud (cloud_voxelgrid);
146 ec.extract (cluster_indices);
149 visualization_msgs::MarkerArray markers;
155 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_output (
new pcl::PointCloud<pcl::PointXYZRGB>());
156 for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin ();
157 it != cluster_indices.end (); ++it)
159 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (
new pcl::PointCloud<pcl::PointXYZRGB>());
162 for (std::vector<int>::const_iterator pit = it->indices.begin ();
163 pit != it->indices.end (); ++pit){
164 cloud_voxelgrid->points[*pit].r = CLUSTER_COLOR[cluster_i][
RED];
165 cloud_voxelgrid->points[*pit].g = CLUSTER_COLOR[cluster_i][
GREEN];
166 cloud_voxelgrid->points[*pit].b = CLUSTER_COLOR[cluster_i][
BLUE];
167 cloud_cluster->points.push_back (cloud_voxelgrid->points[*pit]);
171 cloud_cluster->width = cloud_cluster->points.size ();
172 cloud_cluster->height = 1;
175 cloud_cluster->is_dense =
true;
176 *cloud_output += *cloud_cluster;
180 visualization_msgs::Marker marker;
181 pcl::PointXYZRGB min_pt, max_pt;
182 pcl::getMinMax3D(*cloud_cluster, min_pt, max_pt);
184 markers.markers.push_back(marker);
187 if(cluster_i >= CLUSTER_MAX){
194 sensor_msgs::PointCloud2 output;
199 output.header.frame_id = FRAME_ID;
206 int main (
int argc,
char** argv)
208 ros::init (argc, argv,
"object_detection");