multi_plane.hpp
Go to the documentation of this file.
00001 /*
00002  * ransac.hpp
00003  *
00004  *  Created on: 14.01.2013
00005  *      Author: josh
00006  */
00007 
00008 #include <cob_3d_segmentation/eval.h>
00009 
00010 
00011 template <typename Point, typename PointTypeNormal, typename PointLabel>
00012 bool Segmentation_MultiPlane<Point, PointTypeNormal,PointLabel>::compute() {
00013   coef.clear();
00014   regions.clear();
00015   inlier_indices.clear();
00016   label_indices.clear();
00017   boundary_indices.clear();
00018   typename pcl::IntegralImageNormalEstimation<Point, PointTypeNormal> ne;
00019   typename pcl::PointCloud<PointTypeNormal>::Ptr n(new pcl::PointCloud<PointTypeNormal>());
00020 
00021 l.reset(new pcl::PointCloud<pcl::Label>());
00022 
00023     ne.setNormalEstimationMethod(ne.COVARIANCE_MATRIX);
00024     ne.setMaxDepthChangeFactor(0.02f);
00025     ne.setNormalSmoothingSize(40.0f);
00026 
00027   ne.setInputCloud(input_);
00028   ne.compute(*n);
00029   float* distance_map = ne.getDistanceMap();
00030 
00031   typename pcl::EdgeAwarePlaneComparator<Point, PointTypeNormal>::Ptr comparator;
00032   typename pcl::OrganizedMultiPlaneSegmentation<Point, PointTypeNormal, pcl::Label> omps;
00033 
00034     omps.setAngularThreshold(2.5f/180.0f*M_PI);
00035     omps.setMaximumCurvature(0.01); // default 0.001
00036     omps.setDistanceThreshold(0.01f);
00037     omps.setMinInliers(100);
00038 
00039   comparator.reset(new pcl::EdgeAwarePlaneComparator<Point, PointTypeNormal>(distance_map));
00040   omps.setComparator(comparator);
00041   omps.setInputCloud(input_);
00042   omps.setInputNormals(n);
00043   omps.segmentAndRefine(regions, coef, inlier_indices, l, label_indices, boundary_indices);
00044 
00045   ROS_INFO("finished");
00046 
00047   return true;
00048 }
00049 
00050 template <typename Point, typename PointTypeNormal, typename PointLabel>
00051 boost::shared_ptr<const pcl::PointCloud<PointLabel> > Segmentation_MultiPlane<Point, PointTypeNormal,PointLabel>::getReconstructedOutputCloud() {
00052   boost::shared_ptr<pcl::PointCloud<PointLabel> > cloud (new pcl::PointCloud<PointLabel>);
00053   cloud->header = input_->header;
00054 
00055 for(size_t i=0; i<inlier_indices.size(); i++) {
00056     boost::shared_ptr<pcl::SampleConsensusModel<Point> >
00057     model;
00058     pcl::PointCloud<Point> temp;
00059     pcl::PointCloud<PointLabel> temp2;
00060 
00061         for(size_t j=0; j<inlier_indices[i].indices.size(); j++) {
00062                 temp.push_back( (*input_)[inlier_indices[i].indices[j]] );
00063         }
00064 
00065     temp2.width = temp.width;
00066     temp2.height = temp.height;
00067     temp2.header = temp.header;
00068     temp2.resize(temp.size());
00069 
00070     for(size_t j=0; j<temp.size(); j++) {
00071       temp2[j].x=temp[j].x;
00072       temp2[j].y=temp[j].y;
00073       temp2[j].z=temp[j].z;
00074       SetLabeledPoint<PointLabel>( temp2[j], i);
00075     }
00076 
00077     *cloud += temp2;
00078   }
00079 
00080   return cloud;
00081 }
00082 
00083 template <typename Point, typename PointTypeNormal, typename PointLabel>
00084 void Segmentation_MultiPlane<Point,PointTypeNormal,PointLabel>::compute_accuracy(float &mean, float &var, float &mean_weighted, float &var_weighted, size_t &used, size_t &mem, size_t &points, float &avg_dist, const boost::shared_ptr<const pcl::PointCloud<PointLabel> > &labeled_pc, double &true_positive, double &false_positive)
00085 {
00086   BinaryClassification bc;
00087   RunningStat rstat;
00088   points = 0;
00089   avg_dist = 0;
00090   mem = 0;
00091 
00092   bc.addPC(labeled_pc);
00093 
00094 std::cout<<l->size()<<"\n";
00095   for(size_t i=0; i<l->size(); i++) {
00096       if(labeled_pc)
00097         bc.addMark((*l)[i].label, (*labeled_pc)[i].label);
00098 
00099         if(pcl_isfinite((*input_)[i].z)) {
00100         ++points;
00101         avg_dist += (*input_)[i].z;}
00102   }
00103 
00104   for(size_t j=0; j<label_indices.size(); j++) {
00105 mem += 4*4;
00106       for(size_t i=0; i<label_indices[j].indices.size(); i++) {
00107 
00108 float mind = std::numeric_limits<float>::max();
00109 for(size_t ind=0; ind<regions.size(); ind++) {
00110 
00111       Eigen::Vector4f m = regions[ind].getCoefficients();
00112       const float l = m.head<3>().norm();
00113       m /= l;
00114 
00115         float d = std::abs( m.head<3>().dot( (*input_)[label_indices[j].indices[i]].getVector3fMap() ) + m(3) );
00116 mind = std::min(d,mind);
00117 }
00118         rstat.Push(mind);}
00119     }
00120 
00121 for(size_t i=0; i<boundary_indices.size(); i++)
00122   mem += 4*boundary_indices[i].indices.size();
00123 
00124   /*for(size_t ind=0; ind<regions.size(); ind++) {
00125 mem += 4*4;
00126     for(size_t i=0; i<regions[ind].getContour ().size(); i++) {
00127       float d = std::abs( regions[ind].getCoefficients().head(3).dot( regions[ind].getContour ()[i].getVector3fMap() ) + regions[ind].getCoefficients()(3) );
00128       rstat.Push(d);}
00129   }*/
00130 #if 0
00131   for(size_t i=0; i<shapes_.size(); i++) {
00132     boost::shared_ptr<pcl::SampleConsensusModel<Point> >
00133     model;
00134     pcl::PointCloud<Point> temp;
00135     pcl::PointCloud<PointLabel> temp2;
00136 
00137     if(shapes_[i].type_==SHAPE_S::PLANE)
00138       model.reset(new pcl::SampleConsensusModelPlane<Point> (input_));
00139     //else if(shapes_[i].type_==SHAPE_S::CYLINDER)
00140     //  model.reset(new pcl::SampleConsensusModelCylinder<Point,Point> (input_));
00141     else if(shapes_[i].type_==SHAPE_S::SPHERE)
00142       model.reset(new pcl::SampleConsensusModelSphere<Point> (input_));
00143     else
00144       ROS_ASSERT(0);
00145 
00146     mem+=shapes_[i].inliers_.size()*4;
00147     mem+=shapes_[i].coeff_.rows()*shapes_[i].coeff_.cols()*4+8;
00148 
00149     model->projectPoints(shapes_[i].inliers_, shapes_[i].coeff_, temp, false);
00150 
00151     temp2.width = temp.width;
00152     temp2.height = temp.height;
00153     temp2.header = temp.header;
00154     temp2.resize(temp.size());
00155 
00156     for(size_t j=0; j<temp.size(); j++) {
00157       temp2[j].x=temp[j].x;
00158       temp2[j].y=temp[j].y;
00159       temp2[j].z=temp[j].z;
00160       SetLabeledPoint<PointLabel>( temp2[j], i);
00161     }
00162 
00163     for(size_t j=0; j<shapes_[i].inliers_.size(); j++) {
00164       Point pr = (*input_)[shapes_[i].inliers_[j]];
00165       Point pm = temp[j];
00166       const float z = pr.z;
00167       const float d = (pr.getVector3fMap()-pm.getVector3fMap()).norm();
00168 
00169       if(labeled_pc && shapes_[i].inliers_[j]<(int)labeled_pc->size())
00170         bc.addMark(i, (*labeled_pc)[shapes_[i].inliers_[j]].label);
00171 
00172       if(!pcl_isfinite(d)) continue;
00173 
00174       rstat.Push(d);
00175       avg_dist += z;
00176       ++points;
00177     }
00178   }
00179 #endif
00180 
00181   bc.finish().get_rate(true_positive, false_positive);
00182 
00183   //points = levels_[0].w*levels_[0].h;
00184   used = rstat.NumDataValues();
00185 
00186   mean = rstat.Mean();
00187   var = rstat.Variance();
00188   avg_dist /= points;
00189 }


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