evaluation_features_fpfh.cpp
Go to the documentation of this file.
00001 
00063 #include <fstream>
00064 #include <boost/program_options.hpp>
00065 
00066 #include <pcl/io/pcd_io.h>
00067 #include <pcl/common/file_io.h>
00068 #include <pcl/point_types.h>
00069 #include <pcl/visualization/pcl_visualizer.h>
00070 #include <pcl/visualization/point_cloud_handlers.h>
00071 
00072 #include <cob_3d_evaluation_features/label_results.h>
00073 #include <opencv2/ml/ml.hpp>
00074 
00075 using namespace pcl;
00076 using namespace std;
00077 typedef visualization::PointCloudColorHandlerRGBField<PointXYZRGB> ColorHdlRGB;
00078 
00079 string rgba_, fpfh_, svm_;
00080 string logfile_;
00081 bool write_header_ = false, vis_ = false;
00082 
00083 void readOptions(int argc, char* argv[])
00084 {
00085   using namespace boost::program_options;
00086   options_description options("Options");
00087   options.add_options()
00088     ("help", "produce help message")
00089     ("logfile", value<string>(&logfile_), "set logfile for results")
00090     ("fpfh", value<string>(&fpfh_), "fpfh features pcd")
00091     ("rgba", value<string>(&rgba_), "rgba pcd")
00092     ("svm", value<string>(&svm_), "svm")
00093     ("header,H", "create new logfile with header")
00094     ("visualize,v", "show results in visualizer")
00095     ;
00096 
00097   positional_options_description p_opt;
00098   p_opt.add("rgba", 1).add("fpfh", 1).add("svm", 1).add("logfile", 1);
00099   variables_map vm;
00100   store(command_line_parser(argc, argv).options(options).positional(p_opt).run(), vm);
00101   notify(vm);
00102 
00103   if (vm.count("help") || argc <= 1)
00104   {
00105     cout << options << endl;
00106     exit(0);
00107   }
00108   if (vm.count("header")) write_header_ = true;
00109   if (vm.count("visualize")) vis_ = true;
00110 }
00111 
00112 int main (int argc, char** argv)
00113 {
00114   readOptions(argc, argv);
00115   PointCloud<PointXYZRGB>::Ptr p(new PointCloud<PointXYZRGB>);
00116   PointCloud<PointXYZRGB>::Ptr p_out(new PointCloud<PointXYZRGB>);
00117   PointCloud<FPFHSignature33>::Ptr fpfh(new PointCloud<FPFHSignature33>);
00118 
00119   if (write_header_)
00120   {
00121     if (logfile_ != "") cob_3d_mapping_common::writeHeader(logfile_,"svm");
00122     else cout << cob_3d_mapping_common::writeHeader("","svm") << endl;;
00123   }
00124 
00125   bool is_mls;
00126   string rn, rf;
00127   cob_3d_mapping_common::parseFileName(fpfh_, rn, rf, is_mls);
00128   rn = "0."+rn;
00129   rf = "0."+rf;
00130   //cout << "load data" << endl;
00131   io::loadPCDFile<PointXYZRGB>(rgba_,*p);
00132   io::loadPCDFile<FPFHSignature33>(fpfh_, *fpfh);
00133   *p_out = *p;
00134   //cout << "load svm" << endl;
00135   CvSVM svm;
00136   svm.load(svm_.c_str());
00137   cv::Mat fpfh_histo(1, 33, CV_32FC1);
00138 
00139   int exp_rgb, pre_rgb, predict;
00140 
00141   // new instance class for holding evaluation results:
00142   cob_3d_mapping_common::LabelResults stats(rn,rf,is_mls);
00143   //cout << "start labeling" << endl;
00144   for (size_t idx = 0; idx < p->points.size(); idx++)
00145   {
00146     //cout << idx << ": ";
00147     exp_rgb = *reinterpret_cast<int*>(&p->points[idx].rgb); // expected label
00148     //cout << exp_rgb << " / ";
00149     memcpy(fpfh_histo.ptr<float>(0), fpfh->points[idx].histogram, sizeof(fpfh->points[idx].histogram));
00150     predict = (int)svm.predict(fpfh_histo);
00151     //cout << predict << endl;
00152     switch(predict)
00153     {
00154     case SVM_PLANE:
00155       pre_rgb = LBL_PLANE;
00156       if (exp_rgb != LBL_PLANE && exp_rgb != LBL_UNDEF) stats.fp[EVAL_PLANE]++;
00157       break;
00158     case SVM_EDGE:
00159       pre_rgb = LBL_EDGE;
00160       if (exp_rgb != LBL_EDGE && exp_rgb != LBL_UNDEF) stats.fp[EVAL_EDGE]++;
00161       if (exp_rgb != LBL_COR && exp_rgb != LBL_EDGE && exp_rgb != LBL_UNDEF) stats.fp[EVAL_EDGECORNER]++;
00162       break;
00163     case SVM_COR:
00164       pre_rgb = LBL_COR;
00165       if (exp_rgb != LBL_COR && exp_rgb != LBL_UNDEF) stats.fp[EVAL_COR]++;
00166       if (exp_rgb != LBL_COR && exp_rgb != LBL_EDGE && exp_rgb != LBL_UNDEF) stats.fp[EVAL_EDGECORNER]++;
00167       break;
00168     case SVM_SPH:
00169       pre_rgb = LBL_SPH;
00170       if (exp_rgb != LBL_SPH && exp_rgb != LBL_UNDEF) stats.fp[EVAL_SPH]++;
00171       if (exp_rgb != LBL_SPH && exp_rgb != LBL_CYL && exp_rgb != LBL_UNDEF) stats.fp[EVAL_CURVED]++;
00172       break;
00173     case SVM_CYL:
00174       pre_rgb = LBL_CYL;
00175       if (exp_rgb != LBL_CYL && exp_rgb != LBL_UNDEF) stats.fp[EVAL_CYL]++;
00176       if (exp_rgb != LBL_SPH && exp_rgb != LBL_CYL && exp_rgb != LBL_UNDEF) stats.fp[EVAL_CURVED]++;
00177       break;
00178     default:
00179       pre_rgb = LBL_UNDEF;
00180       break;
00181     }
00182 
00183     switch(exp_rgb)
00184     {
00185     case LBL_PLANE:
00186       if (pre_rgb != exp_rgb) stats.fn[EVAL_PLANE]++;
00187       stats.exp[EVAL_PLANE]++;
00188       break;
00189     case LBL_EDGE:
00190       if (pre_rgb != exp_rgb)
00191       {
00192         stats.fn[EVAL_EDGE]++;
00193         if (pre_rgb != LBL_COR) stats.fn[EVAL_EDGECORNER]++;
00194       }
00195       stats.exp[EVAL_EDGE]++;
00196       stats.exp[EVAL_EDGECORNER]++;
00197       break;
00198     case LBL_COR:
00199       if (pre_rgb != exp_rgb)
00200       {
00201         stats.fn[EVAL_COR]++;
00202         if (pre_rgb != LBL_EDGE) stats.fn[EVAL_EDGECORNER]++;
00203       }
00204       stats.exp[EVAL_COR]++;
00205       stats.exp[EVAL_EDGECORNER]++;
00206       break;
00207     case LBL_SPH:
00208       if (pre_rgb != exp_rgb)
00209       {
00210         stats.fn[EVAL_SPH]++;
00211         if (pre_rgb != LBL_CYL) stats.fn[EVAL_CURVED]++;
00212       }
00213       stats.exp[EVAL_SPH]++;
00214       stats.exp[EVAL_CURVED]++;
00215       break;
00216     case LBL_CYL:
00217       if (pre_rgb != exp_rgb)
00218       {
00219         stats.fn[EVAL_CYL]++;
00220         if (pre_rgb != LBL_SPH) stats.fn[EVAL_CURVED]++;
00221       }
00222       stats.exp[EVAL_CYL]++;
00223       stats.exp[EVAL_CURVED]++;
00224       break;
00225     default:
00226       stats.undef++;
00227       break;
00228     }
00229     p_out->points[idx].rgb = *reinterpret_cast<float*>(&pre_rgb);
00230   }
00231   //cout << "calc stats" << endl;
00232   stats.calcResults();
00233   //cout << "write stats" << endl;
00234   if (logfile_ != "")
00235     stats.writeToFile(logfile_, svm_);
00236   else
00237     cout << stats.writeToFile("", svm_) << endl;;
00238 
00239   if (vis_)
00240   {
00241     ColorHdlRGB col_hdl(p_out);
00242     visualization::PCLVisualizer v;
00243     v.setBackgroundColor(255,255,255);
00244     v.addPointCloud<PointXYZRGB>(p_out, col_hdl ,"fpfh");
00245     while(!v.wasStopped())
00246     {
00247       v.spinOnce(100);
00248       usleep(100000);
00249     }
00250   }
00251 
00252   return 0;
00253 }


cob_3d_evaluation_features
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:04:38