00001 #include "zyonz_obtain_two_clustered_leaves_alg.h" 00002 #include "pcl/kdtree/kdtree_flann.h" 00003 #include "pcl/segmentation/extract_clusters.h" 00004 00005 ZyonzObtainTwoClusteredLeavesAlgorithm::ZyonzObtainTwoClusteredLeavesAlgorithm(void) 00006 { 00007 } 00008 00009 ZyonzObtainTwoClusteredLeavesAlgorithm::~ZyonzObtainTwoClusteredLeavesAlgorithm(void) 00010 { 00011 } 00012 00013 void ZyonzObtainTwoClusteredLeavesAlgorithm::config_update(Config& new_cfg, uint32_t level) 00014 { 00015 this->lock(); 00016 00017 // save the current configuration 00018 this->config_=new_cfg; 00019 00020 this->unlock(); 00021 } 00022 00023 // ZyonzObtainTwoClusteredLeavesAlgorithm Public API 00024 bool ZyonzObtainTwoClusteredLeavesAlgorithm::extract_clusters(sensor_msgs::PointCloud2::ConstPtr& msg) 00025 { 00026 00027 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>); 00028 pcl::fromROSMsg(*msg, *cloud); 00029 00030 cluster_a_img_.data.clear(); 00031 cluster_b_img_.data.clear(); 00032 cluster_a_img_.header = cluster_b_img_.header = msg->header; 00033 cluster_a_img_.height = cluster_b_img_.height = msg->height; 00034 cluster_a_img_.width = cluster_b_img_.width = msg->width; 00035 cluster_a_img_.encoding = cluster_b_img_.encoding = "mono8"; 00036 cluster_a_img_.step = cluster_b_img_.step = msg->width; 00037 cluster_a_img_.data.resize(msg->width * msg->height); 00038 cluster_b_img_.data.resize(msg->width * msg->height); 00039 for (unsigned int rr=0; rr<msg->height; ++rr) 00040 for (unsigned int cc=0; cc<msg->width; ++cc ){ 00041 int idx0 = rr*msg->width + cc; 00042 cluster_a_img_.data[idx0] = 0.0; // Initialize image to white 00043 } 00044 00045 ROS_DEBUG("height: %d, width: %d", msg->height, msg->width); 00046 00047 //Creating the KdTree object for the search method of the extraction 00048 pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>); 00049 00050 tree->setInputCloud (cloud); 00051 00052 std::vector<pcl::PointIndices> cluster_indices; 00053 pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec; 00054 ec.setClusterTolerance (0.02); 00055 ec.setMinClusterSize (100); 00056 ec.setMaxClusterSize (25000); 00057 ec.setSearchMethod (tree); 00058 ec.setInputCloud (cloud); 00059 ec.extract (cluster_indices); 00060 00061 if(cluster_indices.size() < 2) 00062 { 00063 ROS_INFO("ZyonzObtainTwoClusteredLeavesAlgorithm::extract_clusters: ERROR: Not enough clusters"); 00064 return false; 00065 } 00066 00067 cluster_a_.header = cluster_b_.header = cloud->header; 00068 cluster_a_.width = cluster_b_.width = cloud->width; 00069 cluster_a_.height = cluster_b_.height = cloud->height; 00070 cluster_a_.is_dense = cluster_b_.is_dense = false; 00071 cluster_a_.resize(cloud->width*cloud->height); 00072 cluster_b_.resize(cloud->width*cloud->height); 00073 00074 // Initialize to NaN the pointclouds 00075 pcl::PointCloud<pcl::PointXYZRGB>::iterator b_it = cluster_b_.begin (); 00076 for(pcl::PointCloud<pcl::PointXYZRGB>::iterator a_it = cluster_a_.begin(); a_it != cluster_a_.end (); ++a_it, ++b_it) 00077 a_it->z = b_it->z = std::numeric_limits<float>::quiet_NaN (); 00078 00079 // fill in cluster_a 00080 std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); 00081 for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); ++pit){ 00082 cluster_a_img_.data[*pit] = 255; 00083 cluster_a_[*pit] = cloud->points[*pit]; 00084 // std::cout << "pixel_position: " << *pit << std::endl; 00085 //std::cout << "point: " << cloud->points[*pit] << std::endl; 00086 } 00087 00088 // fill in cluster_b 00089 ++it; 00090 for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); ++pit){ 00091 cluster_b_img_.data[*pit] = 255; 00092 cluster_b_[*pit] = cloud->points[*pit]; 00093 } 00094 00095 return true; 00096 } 00097 00098 pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr ZyonzObtainTwoClusteredLeavesAlgorithm::get_cluster_a(void) 00099 { 00100 return cluster_a_.makeShared(); 00101 } 00102 00103 pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr ZyonzObtainTwoClusteredLeavesAlgorithm::get_cluster_b(void) 00104 { 00105 return cluster_b_.makeShared(); 00106 } 00107 00108 sensor_msgs::Image* ZyonzObtainTwoClusteredLeavesAlgorithm::get_cluster_a_img(void) 00109 { 00110 return &cluster_a_img_; 00111 } 00112 00113 sensor_msgs::Image* ZyonzObtainTwoClusteredLeavesAlgorithm::get_cluster_b_img(void) 00114 { 00115 return &cluster_b_img_; 00116 } 00117