train_data_create_tool.cpp
Go to the documentation of this file.
00001 #include <pcl/visualization/pcl_visualizer.h>
00002 #include <pcl/io/pcd_io.h>
00003 #include <pcl/features/fpfh.h>
00004 #include <pcl/features/normal_3d.h>
00005 #include <pcl/features/moment_of_inertia_estimation.h>
00006 #include <boost/foreach.hpp>
00007 #include <algorithm>
00008 #include <Eigen/Core>
00009 #include <Eigen/Eigen>
00010 #include <Eigen/Dense>
00011 #include <sys/types.h>
00012 #include <dirent.h>
00013 #include <errno.h>
00014 #include <vector>
00015 #include <string>
00016 #include <iostream>
00017 #include <fstream>
00018 using namespace std;
00019 
00020 class Viewer
00021 {
00022 public:
00023   Viewer()
00024     : viewer_(new pcl::visualization::PCLVisualizer( "Point Cloud Viewer" )),
00025       dir_("."),
00026       buffer_cloud_(new pcl::PointCloud<pcl::PointXYZI>)
00027   {
00028     getdir(dir_, files_);
00029   }
00030 
00031   void run()
00032   {
00033     viewer_->registerKeyboardCallback( &Viewer::keyboard_callback, *this );
00034     viewer_->setBackgroundColor (255, 255, 255);
00035     string filename = files_.back();
00036     std::cout << filename << std::endl;
00037     files_.pop_back();
00038     std::cout << "==================================" << std::endl;
00039     std::cout << "Now opening file is : " << filename << std::endl;
00040     pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
00041     pcl::io::loadPCDFile (filename, *cloud);
00042     //buffer_cloud_ = cloud->makeShared();
00043     pcl::io::loadPCDFile (filename, *buffer_cloud_);
00044     std::cout << "cloud points : " << cloud->points.size() << std::endl;
00045     viewer_->addPointCloud(cloud, "Cloud");
00046    
00047     while (!viewer_->wasStopped ())
00048     {
00049       viewer_->spinOnce (100);
00050       boost::this_thread::sleep (boost::posix_time::microseconds (100000));
00051     }
00052   }
00053 
00054 private:
00055 
00056   void keyboard_callback( const pcl::visualization::KeyboardEvent& event, void* )
00057   {
00058     if( event.getKeyCode() && event.keyDown() ){
00059 
00060       if(event.getKeyCode() == 'y' || event.getKeyCode() == 'n'){
00061         std::cout << "Key : " << event.getKeyCode() << std::endl;
00062         //ここで特徴量の計算
00063         std::vector<float> features;
00064         feature_calculation(buffer_cloud_, features);
00065         if (event.getKeyCode() == 'y') {
00066           output_features(1, features);
00067         }else if(event.getKeyCode() == 'n')
00068         {
00069           output_features(0, features);
00070         }
00071         //新しいポイントクラウドの読み込み
00072         string filename = files_.back();
00073         std::cout << "==================================" << std::endl;
00074         std::cout << "Now opening file is : " << filename << std::endl;
00075         files_.pop_back();
00076         pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
00077         pcl::io::loadPCDFile (filename, *cloud);
00078         pcl::io::loadPCDFile (filename, *buffer_cloud_);
00079         //buffer_cloud_ = cloud->makeShared();
00080         std::cout << "cloud points : " << cloud->points.size() << std::endl;
00081         viewer_->updatePointCloud(cloud, "Cloud");
00082 
00083 
00084       }
00085     }
00086   }
00087 
00088   void feature_calculation(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud,
00089                            std::vector<float> &features)
00090   {
00091     int n;
00092     float f21,f22,f23,f24,f25,f26;
00093     float v1, v2, v3;
00094     double v11, v22, v33;
00095     float f31,f32,f33,f34,f35,f36,f37;
00096     pcl::PointXYZI min_point;
00097     pcl::PointXYZI max_point;
00098     std::vector<float> moment_of_inertia;
00099     std::vector<float> eccentricity;
00100     Eigen::Matrix3f cmat;
00101     Eigen::Vector4f xyz_centroid;
00102     EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
00103     pcl::MomentOfInertiaEstimation <pcl::PointXYZI> feature_extractor;
00104     feature_extractor.setInputCloud (cloud);
00105     feature_extractor.compute ();
00106     feature_extractor.getMomentOfInertia (moment_of_inertia);
00107     feature_extractor.getEccentricity (eccentricity);
00108     feature_extractor.getAABB (min_point, max_point);
00109     pcl::compute3DCentroid (*cloud, xyz_centroid);
00110     pcl::computeCovarianceMatrix (*cloud, xyz_centroid, covariance_matrix);
00111     n = cloud->points.size();
00112     f21 = covariance_matrix (0,0) / (n - 1);
00113     f22 = covariance_matrix (0,1) / (n - 1);
00114     f23 = covariance_matrix (0,2) / (n - 1);
00115     f24 = covariance_matrix (1,1) / (n - 1);
00116     f25 = covariance_matrix (1,2) / (n - 1);
00117     f26 = covariance_matrix (2,2) / (n - 1);
00118     cmat (0,0) = f21;
00119     cmat (0,1) = f22;
00120     cmat (0,2) = f23;
00121     cmat (1,0) = f22;
00122     cmat (1,1) = f24;
00123     cmat (1,2) = f25;
00124     cmat (2,0) = f23;
00125     cmat (2,1) = f25;
00126     cmat (2,2) = f26;
00127     Eigen::EigenSolver<Eigen::MatrixXf> es(cmat, false);
00128     complex<double> lambda1 = es.eigenvalues()[0];
00129     complex<double> lambda2 = es.eigenvalues()[1];
00130     complex<double> lambda3 = es.eigenvalues()[2];
00131     v11 = lambda1.real();
00132     v22 = lambda2.real();
00133     v33 = lambda3.real();
00134     std::vector<double> v;
00135     v.push_back (v11);
00136     v.push_back (v22);
00137     v.push_back (v33);
00138     std::sort(v.begin(), v.end() );
00139     v3 = v[0];
00140     v2 = v[1];
00141     v1 = v[2];
00142     v.clear();
00143 
00144     double nnn = v1 * v2 * v3;
00145     f31 = (v1 - v2) / v1;
00146     f32 = (v2 - v3) / v1;
00147     f33 = v3 / v1;
00148     f34 = pow(nnn, 1.0 / 3.0);
00149     f35 = (v1 - v3) / v1;
00150     f36 = -(v1 * log(v1) + v2 * log(v2) + v3 * log(v3));
00151     f37 = v3 / (v1 + v2 + v3);
00152 
00153     features.push_back(f21);
00154     features.push_back(f22);
00155     features.push_back(f23);
00156     features.push_back(f24);
00157     features.push_back(f25);
00158     features.push_back(f26);
00159     features.push_back(f31);
00160     features.push_back(f32);
00161     features.push_back(f33);
00162     features.push_back(f34);
00163     features.push_back(f35);
00164     features.push_back(f36);
00165     features.push_back(f37);
00166 
00167   }
00168 
00169   void output_features(int label, std::vector<float> features)
00170   {
00171     output_file_.open("train.csv", ios::ate | ios::app);
00172     output_file_ << label;
00173     for (int i = 0; i < features.size(); ++i) {
00174       output_file_ << " " << i+1 << ":" << features[i];
00175     }
00176     output_file_ << "\n";
00177     output_file_.close();
00178   }
00179   
00180   int getdir (string dir, vector<string> &files)
00181   {
00182     DIR *dp;
00183     struct dirent *dirp;
00184     if((dp  = opendir(dir.c_str())) == NULL) {
00185         cout << "Error(" << errno << ") opening " << dir << endl;
00186         return errno;
00187     }
00188 
00189     while ((dirp = readdir(dp)) != NULL) {
00190         files.push_back(string(dirp->d_name));
00191     }
00192     closedir(dp);
00193     sort( files_.begin(), files_.end() );
00194     for (size_t i = 0; i < files_.size(); ++i) {
00195       std::cout << files_[i] << std::endl;
00196     }
00197 
00198     return 0;
00199   }
00200 
00201   boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer_;
00202   string dir_;
00203   vector<string> files_;
00204   pcl::PointCloud<pcl::PointXYZI>::Ptr buffer_cloud_;
00205   std::ofstream output_file_;
00206 };
00207 
00208 int main( int argc, char* argv[] )
00209 {
00210   Viewer viewer;
00211   viewer.run();
00212 
00213   return 0;
00214 }


target_obejct_detector
Author(s): CIR-KIT
autogenerated on Thu Jun 6 2019 20:19:57