build_all_object_models.cpp
Go to the documentation of this file.
00001 #include "object_recognition.h"
00002 
00003 #include <string>
00004 #include <sstream>
00005 #include <iostream>
00006 #include <pcl/console/parse.h>
00007 #include <pcl/io/pcd_io.h>
00008 #include <pcl/visualization/pcl_visualizer.h>
00009 #include <boost/filesystem.hpp>
00010 namespace bf = boost::filesystem;
00011 
00012 inline void
00013 getModelsInDirectory (bf::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths)
00014 {
00015   bf::directory_iterator end_itr;
00016   for (bf::directory_iterator itr (dir); itr != end_itr; ++itr)
00017   {
00018     //check if its a directory, then get models in it
00019     if (bf::is_directory (*itr))
00020     {
00021 #if BOOST_FILESYSTEM_VERSION == 3
00022       std::string so_far = rel_path_so_far + itr->path ().filename ().string () + "/";
00023 #else
00024       std::string so_far = rel_path_so_far + itr->path ().filename () + "/";
00025 #endif
00026       bf::path curr_path = itr->path ();
00027       getModelsInDirectory (curr_path, so_far, relative_paths);
00028     }
00029     else
00030     {
00031       std::vector<std::string> strs;
00032 #if BOOST_FILESYSTEM_VERSION == 3
00033       std::string file = itr->path ().filename ().string ();
00034 #else
00035       std::string file = itr->path ().filename ();
00036 #endif
00037       boost::split (strs, file, boost::is_any_of ("."));
00038       std::string extension = strs[strs.size () - 1];
00039 
00040       if((file.compare (0, 3, "raw") == 0) && extension == "pcd") {
00041 #if BOOST_FILESYSTEM_VERSION == 3
00042         std::string path = rel_path_so_far + itr->path ().filename ().string ();
00043 #else
00044         std::string path = rel_path_so_far + itr->path ().filename ();
00045 #endif
00046         relative_paths.push_back (path);
00047       }
00048     }
00049   }
00050 }
00051 
00052 int
00053 main (int argc, char ** argv)
00054 {
00055   if (argc < 3)
00056   {
00057     pcl::console::print_info ("Syntax is: %s input_directory <options>\n", argv[0]);
00058     pcl::console::print_info ("  where options are:\n");
00059     pcl::console::print_info ("    --filter parameters.txt ............ Threshold, downsample, and denoise\n");
00060     pcl::console::print_info ("    --segment parameters.txt  .......... Remove dominant plane and cluster\n");
00061     pcl::console::print_info ("    --feature parameters.txt ........... Compute normals, keypoints, and descriptors\n");
00062     pcl::console::print_info ("    --registration parameters.txt ...... Align best fitting model to query\n");
00063     pcl::console::print_info ("  and where the parameters files must contain the following values (one per line):\n");
00064     pcl::console::print_info ("    filter parameters:\n");
00065     pcl::console::print_info ("      * min_depth\n");
00066     pcl::console::print_info ("      * max_depth\n");
00067     pcl::console::print_info ("      * downsample_leaf_size\n");
00068     pcl::console::print_info ("      * outlier_rejection_radius\n");
00069     pcl::console::print_info ("      * outlier_rejection_min_neighbors\n");
00070     pcl::console::print_info ("    segmentation parameters:\n");
00071     pcl::console::print_info ("      * plane_inlier_distance_threshold\n");
00072     pcl::console::print_info ("      * max_ransac_iterations\n");
00073     pcl::console::print_info ("      * cluster_tolerance\n");
00074     pcl::console::print_info ("      * min_cluster_size\n");
00075     pcl::console::print_info ("      * max_cluster_size\n");
00076     pcl::console::print_info ("    feature estimation parameters:\n");
00077     pcl::console::print_info ("      * surface_normal_radius\n");
00078     pcl::console::print_info ("      * keypoints_min_scale\n");
00079     pcl::console::print_info ("      * keypoints_nr_octaves\n");
00080     pcl::console::print_info ("      * keypoints_nr_scales_per_octave\n");
00081     pcl::console::print_info ("      * keypoints_min_contrast\n");
00082     pcl::console::print_info ("      * local_descriptor_radius\n");
00083     pcl::console::print_info ("    registration parameters:\n");
00084     pcl::console::print_info ("      * initial_alignment_min_sample_distance\n");
00085     pcl::console::print_info ("      * initial_alignment_max_correspondence_distance\n");
00086     pcl::console::print_info ("      * initial_alignment_nr_iterations\n");
00087     pcl::console::print_info ("      * icp_max_correspondence_distance\n");
00088     pcl::console::print_info ("      * icp_rejection_threshold\n");
00089     pcl::console::print_info ("      * icp_transformation_epsilon\n");
00090     pcl::console::print_info ("      * icp_max_iterations\n");
00091     pcl::console::print_info ("Note: The output's base filename must be specified without the .pcd extension\n");
00092     pcl::console::print_info ("      Four output files will be created with the following suffixes:\n");
00093     pcl::console::print_info ("        * '_points.pcd'\n");
00094     pcl::console::print_info ("        * '_keypoints.pcd'\n");
00095     pcl::console::print_info ("        * '_localdesc.pcd'\n");
00096     pcl::console::print_info ("        * '_globaldesc.pcd'\n");
00097 
00098     return (1);
00099   }
00100 
00101   ObjectRecognitionParameters params;
00102   ifstream params_stream;
00103 
00104   //Parse filter parameters
00105   std::string filter_parameters_file;
00106   pcl::console::parse_argument (argc, argv, "--filter", filter_parameters_file) > 0;
00107   params_stream.open (filter_parameters_file.c_str ());
00108   if (params_stream.is_open())
00109   {
00110     params_stream >> params.min_depth;
00111     params_stream >> params.max_depth;
00112     params_stream >> params.downsample_leaf_size;
00113     params_stream >> params.outlier_rejection_radius;
00114     params_stream >> params.outlier_rejection_min_neighbors;
00115     params_stream.close ();
00116   }
00117   else
00118   {
00119     pcl::console::print_info ("Failed to open the filter parameters file (%s)\n", filter_parameters_file.c_str ());
00120     return (1);
00121   }
00122 
00123   // Parse segmentation parameters
00124   std::string segmentation_parameters_file;
00125   pcl::console::parse_argument (argc, argv, "--segment", segmentation_parameters_file) > 0;
00126   params_stream.open (segmentation_parameters_file.c_str ());
00127   if (params_stream.is_open())
00128   {
00129     params_stream >> params.plane_inlier_distance_threshold;
00130     params_stream >> params.max_ransac_iterations;
00131     params_stream >> params.cluster_tolerance;
00132     params_stream >> params.min_cluster_size;
00133     params_stream >> params.max_cluster_size;
00134     params_stream.close ();
00135   }
00136   else
00137   {
00138     pcl::console::print_info ("Failed to open the segmentation parameters file (%s)\n",
00139                               segmentation_parameters_file.c_str ());
00140     return (1);
00141   }
00142 
00143   // Parse feature estimation parameters
00144   std::string feature_estimation_parameters_file;
00145   pcl::console::parse_argument (argc, argv, "--feature", feature_estimation_parameters_file) > 0;
00146   params_stream.open (feature_estimation_parameters_file.c_str ());
00147   if (params_stream.is_open())
00148   {
00149     params_stream >> params.surface_normal_radius;
00150     params_stream >> params.keypoints_min_scale;
00151     params_stream >> params.keypoints_nr_octaves;
00152     params_stream >> params.keypoints_nr_scales_per_octave;
00153     params_stream >> params.keypoints_min_contrast;
00154     params_stream >> params.local_descriptor_radius;
00155     params_stream.close ();
00156   }
00157   else
00158   {
00159     pcl::console::print_info ("Failed to open the feature estimation parameters file (%s)\n",
00160                               feature_estimation_parameters_file.c_str ());
00161     return (1);
00162   }
00163 
00164   // Parse the registration parameters
00165   std::string registration_parameters_file;
00166   pcl::console::parse_argument (argc, argv, "--registration", registration_parameters_file) > 0;
00167   params_stream.open (registration_parameters_file.c_str ());
00168   if (params_stream.is_open())
00169   {
00170     params_stream >> params.initial_alignment_min_sample_distance;
00171     params_stream >> params.initial_alignment_max_correspondence_distance;
00172     params_stream >> params.initial_alignment_nr_iterations;
00173     params_stream >> params.icp_max_correspondence_distance;
00174     params_stream >> params.icp_outlier_rejection_threshold;
00175     params_stream >> params.icp_transformation_epsilon;
00176     params_stream >> params.icp_max_iterations;
00177     params_stream.close ();
00178   }
00179   else
00180   {
00181     pcl::console::print_info ("Failed to open the registration parameters file (%s)\n",
00182                               registration_parameters_file.c_str ());
00183     return (1);
00184   }
00185 
00186   std::string directory (argv[1]);
00187   //Find all raw* files in input_directory
00188   bf::path dir_path = directory;
00189   std::vector < std::string > files;
00190   std::string start = "";
00191   getModelsInDirectory (dir_path, start, files);
00192 
00193   for(size_t i=0; i < files.size(); i++) {
00194     // Load input file
00195 
00196     std::string filename = directory;
00197     filename.append("/");
00198     filename.append(files[i]);
00199     PointCloudPtr input (new PointCloud);
00200     pcl::io::loadPCDFile (filename, *input);
00201     pcl::console::print_info ("Loaded %s (%zu points)\n", filename.c_str(), input->size ());
00202 
00203     std::cout << files[i] << std::endl;
00204     // Construct the object model
00205     ObjectRecognition obj_rec (params);
00206     ObjectModel model;
00207     obj_rec.constructObjectModel (input, model);
00208 
00209     //get directory name
00210     std::vector < std::string > strs;
00211     boost::split (strs, files[i], boost::is_any_of ("/\\"));
00212 
00213     std::string id = strs[0];
00214     std::string raw_file = strs[1];
00215 
00216     strs.clear();
00217     boost::split (strs, raw_file, boost::is_any_of ("_"));
00218 
00219     std::stringstream base_filestream;
00220     base_filestream << directory << "/" << id << "/" << id << strs[1].substr(0,1);
00221     // Save the model files
00222     std::string base_filename (base_filestream.str()), output_filename;
00223 
00224     output_filename = base_filename;
00225     output_filename.append ("_points.pcd");
00226     pcl::io::savePCDFile (output_filename, *(model.points));
00227     pcl::console::print_info ("Saved points as %s\n", output_filename.c_str ());
00228 
00229     output_filename = base_filename;
00230     output_filename.append ("_keypoints.pcd");
00231     pcl::io::savePCDFile (output_filename, *(model.keypoints));
00232     pcl::console::print_info ("Saved keypoints as %s\n", output_filename.c_str ());
00233 
00234     output_filename = base_filename;
00235     output_filename.append ("_localdesc.pcd");
00236     pcl::io::savePCDFile (output_filename, *(model.local_descriptors));
00237     pcl::console::print_info ("Saved local descriptors as %s\n", output_filename.c_str ());
00238 
00239     output_filename = base_filename;
00240     output_filename.append ("_globaldesc.pcd");
00241     pcl::io::savePCDFile (output_filename, *(model.global_descriptor));
00242     pcl::console::print_info ("Saved global descriptor as %s\n", output_filename.c_str ());
00243   }
00244 
00245   return (0);
00246 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:36