cluster_conversion.h
Go to the documentation of this file.
00001 
00063 #ifndef CLUSTER_CONVERSION_H__
00064 #define CLUSTER_CONVERSION_H__
00065 
00066 #include <pcl/point_types.h>
00067 
00068 #include <cob_3d_mapping_common/point_types.h>
00069 #include <cob_3d_mapping_common/polygon.h>
00070 #include <cob_3d_segmentation/polygon_extraction/polygon_extraction.h>
00071 #include <cob_3d_segmentation/cluster_handler.h>
00072 
00073 namespace cob_3d_segmentation
00074 {
00075   template<
00076     typename PointT = pcl::PointXYZRGB,
00077     typename ClusterHdlT = DepthClusterHandler<PointLabel, PointT, pcl::Normal>
00078   >
00079   class ClusterConversion
00080   {
00081     public:
00082     typedef pcl::PointCloud<PointT> PointCloud;
00083     typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00084     typedef typename ClusterHdlT::Ptr ClusterHdlPtr;
00085     typedef typename ClusterHdlT::ClusterPtr ClusterPtr;
00086 
00087     public:
00088     ClusterConversion()
00089     : min_size_(100)
00090     , max_dist_(5.0f)
00091     , colorize_(false)
00092     {}
00093 
00094     virtual ~ClusterConversion() {}
00095 
00096     inline void setClusterHandler(const ClusterHdlPtr& ch) { ch_ = ch; }
00097     inline void setInputCloud(const PointCloudConstPtr& p) { p_ = p; }
00098     inline void setMinClusterSize(int size) { min_size_ = size; }
00099     inline void setMaxCentroidDistance(float z_distance) { max_dist_ = z_distance; }
00100     inline void setColor(bool enable) { colorize_ = enable; }
00101 
00102     bool clusterToPolygon(const ClusterPtr& c, cob_3d_mapping::Polygon::Ptr& pg)
00103     {
00104       cob_3d_segmentation::PolygonContours<cob_3d_segmentation::PolygonPoint> poly;
00105       pe_.outline(p_->width,p_->height,c->border_points,poly);
00106       if(!poly.polys_.size()) return false;
00107       /*for (int i = 0; i < (int)poly.polys_.size(); ++i)
00108       {
00109         std::cout << "c " << i << ": " << std::endl;
00110         for (int idx = 0; idx < poly.polys_[i].size(); ++idx)
00111         {
00112           std::cout << poly.polys_[i][idx].x << "," << poly.polys_[i][idx].y << "->";
00113         }
00114         std::cout << std::endl;
00115       }*/
00116       int max_idx=0, max_size=0;
00117       /*for (int i=0; i<(int)poly.polys_.size(); ++i)
00118       {
00119         if( (int)poly.polys_[i].size() > max_size)
00120         {
00121           max_idx = i;
00122           max_size = poly.polys_[i].size();
00123         }
00124       }
00125       std::cout << "max_idx " << max_idx << std::endl;*/
00126       //std::cout << c->border_points.size() << "->" << poly.polys_[0].size() << std::endl;
00127 
00128       std::vector<pcl::PointCloud<pcl::PointXYZ> > contours_3d;
00129       std::vector<bool> holes;
00130       for (int i = 0; i < (int)poly.polys_.size(); ++i)
00131       {
00132         int n_points = poly.polys_[i].size();
00133         int reverse;
00134         pcl::PointCloud<pcl::PointXYZ> contour;
00135         contour.resize(n_points);
00136         if (i == max_idx) { holes.push_back(false); reverse = 0; }
00137         else              { holes.push_back(true);  reverse = n_points - 1; }
00138 
00139         for (int idx = 0; idx < n_points; ++idx)
00140         {
00141           PolygonPoint& pp = (poly.polys_[i])[abs(reverse - idx)];
00142           contour[idx].getVector3fMap() = p_->points[ pp.x + pp.y * p_->width ].getVector3fMap();
00143         }
00144         contour.height = 1;
00145         contour.width = contour.size();
00146         contours_3d.push_back(contour);
00147       }
00148 
00149       std::vector<float> color(4, 1);
00150       if(colorize_)
00151       {
00152         Eigen::Vector3i col_tmp( c->computeDominantColorVector() );
00153         float temp_inv = 1.0f/255.0f;
00154         color[0] = float(col_tmp(0)) * temp_inv;
00155         color[1] = float(col_tmp(1)) * temp_inv;
00156         color[2] = float(col_tmp(2)) * temp_inv;
00157       }
00158       else
00159       {
00160         color[0] = 0.0f;
00161         color[1] = 0.0f;
00162         color[2] = 1.0f;
00163       }
00164       pg.reset(new cob_3d_mapping::Polygon(c->id(),
00165                                            c->pca_point_comp3,
00166                                            c->getCentroid(),
00167                                            contours_3d,
00168                                            holes,
00169                                            color));
00170 
00171       return true;
00172     }
00173 
00174     void convertToPolygons(std::vector<cob_3d_mapping::Polygon::Ptr> polygons)
00175     {
00176       for(ClusterPtr c = ch_->begin(); c != ch_->end(); ++c)
00177       {
00178         if(c->size() < min_size_) continue;
00179         if(c->getCentroid()(2) > max_dist_) continue;
00180         if(c->size() <= ceil(1.1f * (float)c->border_points.size())) continue;
00181 
00182         ch_->computeClusterComponents(c);
00183         if(!c->is_planar_) continue;
00184 
00185         cob_3d_mapping::Polygon::Ptr p;
00186         if(clusterToPolygon(c, p)) {
00187           polygons.push_back(p);
00188         }
00189       }
00190     }
00191 
00192     protected:
00193     int min_size_;
00194     float max_dist_;
00195     bool colorize_;
00196 
00197     PolygonExtraction pe_;
00198     ClusterHdlPtr ch_;
00199     PointCloudConstPtr p_;
00200   };
00201 }
00202 
00203 #endif


cob_3d_segmentation
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:03