ransac.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 PointLabel>
00012 bool Segmentation_RANSAC<Point, PointLabel>::compute() {
00013   shapes_.clear();
00014 
00015   bool found = true;
00016   boost::shared_ptr<pcl::PointCloud<Point> > cloud (new pcl::PointCloud<Point>(*input_));
00017 
00018   while(found) {
00019     found = false;
00020     SHAPE_S shape;
00021 
00022     if(!found && planes_) {
00023       boost::shared_ptr<pcl::SampleConsensusModelPlane<Point> >
00024         model (new pcl::SampleConsensusModelPlane<Point> (cloud));
00025 
00026       pcl::RandomSampleConsensus<Point> ransac (model);
00027       ransac.setDistanceThreshold (.03);
00028       ransac.computeModel();
00029       ransac.getInliers(shape.inliers_);
00030 
00031       if(shape.inliers_.size()>0) {
00032         ransac.getModelCoefficients(shape.coeff_);
00033         shape.type_ = SHAPE_S::PLANE;
00034         found = true;
00035       }
00036     }
00037 
00038     if(!found && spheres_) {
00039       boost::shared_ptr<pcl::SampleConsensusModelSphere<Point> >
00040         model (new pcl::SampleConsensusModelSphere<Point> (cloud));
00041 
00042       pcl::RandomSampleConsensus<Point> ransac (model);
00043       ransac.setDistanceThreshold (.03);
00044       ransac.computeModel();
00045       ransac.getInliers(shape.inliers_);
00046 
00047       if(shape.inliers_.size()>0) {
00048         ransac.getModelCoefficients(shape.coeff_);
00049         shape.type_ = SHAPE_S::SPHERE;
00050         found = true;
00051       }
00052     }
00053 
00054     if(found) {
00055       ROS_INFO("add %d %d",shape.type_, (int)shape.inliers_.size());
00056       shapes_.push_back( shape );
00057       for(size_t i=0; i<shape.inliers_.size(); i++)
00058         (*cloud)[shape.inliers_[i]].x = (*cloud)[shape.inliers_[i]].y = (*cloud)[shape.inliers_[i]].z =
00059             std::numeric_limits<float>::quiet_NaN();
00060     }
00061 
00062   }
00063   ROS_INFO("finished");
00064 
00065   return true;
00066 }
00067 
00068 template <typename Point, typename PointLabel>
00069  boost::shared_ptr<const pcl::PointCloud<PointLabel> > Segmentation_RANSAC<Point, PointLabel>::getReconstructedOutputCloud() {
00070   boost::shared_ptr<pcl::PointCloud<PointLabel> > cloud (new pcl::PointCloud<PointLabel>);
00071   cloud->header = input_->header;
00072 
00073   for(size_t i=0; i<shapes_.size(); i++) {
00074     boost::shared_ptr<pcl::SampleConsensusModel<Point> >
00075       model;
00076     pcl::PointCloud<Point> temp;
00077     pcl::PointCloud<PointLabel> temp2;
00078 
00079     if(shapes_[i].type_==SHAPE_S::PLANE)
00080       model.reset(new pcl::SampleConsensusModelPlane<Point> (input_));
00081     //else if(shapes_[i].type_==SHAPE_S::CYLINDER)
00082     //  model.reset(new pcl::SampleConsensusModelCylinder<Point,Point> (input_));
00083     else if(shapes_[i].type_==SHAPE_S::SPHERE)
00084       model.reset(new pcl::SampleConsensusModelSphere<Point> (input_));
00085     else
00086       ROS_ASSERT(0);
00087 
00088     model->projectPoints(shapes_[i].inliers_, shapes_[i].coeff_, temp, false);
00089 
00090     temp2.width = temp.width;
00091     temp2.height = temp.height;
00092     temp2.header = temp.header;
00093     temp2.resize(temp.size());
00094 
00095     for(size_t j=0; j<temp.size(); j++) {
00096       temp2[j].x=temp[j].x;
00097       temp2[j].y=temp[j].y;
00098       temp2[j].z=temp[j].z;
00099       SetLabeledPoint<PointLabel>( temp2[j], i);
00100     }
00101 
00102     *cloud += temp2;
00103   }
00104 
00105   return cloud;
00106 }
00107 
00108 template <typename Point, typename PointLabel>
00109 void Segmentation_RANSAC<Point,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)
00110 {
00111   BinaryClassification bc;
00112   RunningStat rstat;
00113   points = 0;
00114   avg_dist = 0;
00115   mem = 0;
00116 
00117   bc.addPC(labeled_pc);
00118 
00119   for(size_t i=0; i<shapes_.size(); i++) {
00120     boost::shared_ptr<pcl::SampleConsensusModel<Point> >
00121       model;
00122     pcl::PointCloud<Point> temp;
00123     pcl::PointCloud<PointLabel> temp2;
00124 
00125     if(shapes_[i].type_==SHAPE_S::PLANE)
00126       model.reset(new pcl::SampleConsensusModelPlane<Point> (input_));
00127     //else if(shapes_[i].type_==SHAPE_S::CYLINDER)
00128     //  model.reset(new pcl::SampleConsensusModelCylinder<Point,Point> (input_));
00129     else if(shapes_[i].type_==SHAPE_S::SPHERE)
00130       model.reset(new pcl::SampleConsensusModelSphere<Point> (input_));
00131     else
00132       ROS_ASSERT(0);
00133 
00134     mem+=shapes_[i].inliers_.size()*4;
00135     mem+=shapes_[i].coeff_.rows()*shapes_[i].coeff_.cols()*4+8;
00136 
00137     model->projectPoints(shapes_[i].inliers_, shapes_[i].coeff_, temp, false);
00138 
00139     temp2.width = temp.width;
00140     temp2.height = temp.height;
00141     temp2.header = temp.header;
00142     temp2.resize(temp.size());
00143 
00144     for(size_t j=0; j<temp.size(); j++) {
00145       temp2[j].x=temp[j].x;
00146       temp2[j].y=temp[j].y;
00147       temp2[j].z=temp[j].z;
00148       SetLabeledPoint<PointLabel>( temp2[j], i);
00149     }
00150 
00151     for(size_t j=0; j<shapes_[i].inliers_.size(); j++) {
00152       Point pr = (*input_)[shapes_[i].inliers_[j]];
00153       Point pm = temp[j];
00154       const float z = pr.z;
00155       const float d = (pr.getVector3fMap()-pm.getVector3fMap()).norm();
00156 
00157       if(labeled_pc && shapes_[i].inliers_[j]<(int)labeled_pc->size())
00158         bc.addMark(i, (*labeled_pc)[shapes_[i].inliers_[j]].label);
00159 
00160       if(!pcl_isfinite(d)) continue;
00161 
00162       rstat.Push(d);
00163       avg_dist += z;
00164       ++points;
00165     }
00166   }
00167 
00168   bc.finish().get_rate(true_positive, false_positive);
00169 
00170   //points = levels_[0].w*levels_[0].h;
00171   used = rstat.NumDataValues();
00172 
00173   mean = rstat.Mean();
00174   var = rstat.Variance();
00175   avg_dist /= points;
00176 }


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