void
pcl::rec_3d_framework::LocalRecognitionPipeline<Distance, PointInT, FeatureT>::loadFeaturesAndCreateFLANN ()
{
    boost::shared_ptr < std::vector<ModelT> > models = source_->getModels ();
    std::cout << "Models size:" << models->size () << std::endl;

    for (size_t i = 0; i < models->size (); i++)
    {
        std::string path = source_->getModelDescriptorDir (models->at (i), training_dir_, descr_name_);
        bf::path inside = path;
        bf::directory_iterator end_itr;

        for (bf::directory_iterator itr_in (inside); itr_in != end_itr; ++itr_in)
        {
#if BOOST_FILESYSTEM_VERSION == 3
            std::string file_name = (itr_in->path ().filename ()).string();
#else
            std::string file_name = (itr_in->path ()).filename ();
#endif

            std::vector < std::string > strs;
            boost::split (strs, file_name, boost::is_any_of ("_"));

            if (strs[0] == "descriptor")
            {
                std::string full_file_name = itr_in->path ().string ();
                std::string name = file_name.substr (0, file_name.length () - 4);
                std::vector < std::string > strs;
                boost::split (strs, name, boost::is_any_of ("_"));

                flann_model descr_model;
                descr_model.model = models->at (i);
                descr_model.view_id = atoi (strs[1].c_str ());

                if (use_cache_)
                {

                    std::stringstream dir_keypoints;
                    std::string path = source_->getModelDescriptorDir (models->at (i), training_dir_, descr_name_);
                    dir_keypoints << path << "/keypoint_indices_" << descr_model.view_id << ".pcd";

                    std::stringstream dir_pose;
                    dir_pose << path << "/pose_" << descr_model.view_id << ".txt";

                    Eigen::Matrix4f pose_matrix;
                    PersistenceUtils::readMatrixFromFile (dir_pose.str (), pose_matrix);

                    std::pair<std::string, int> pair_model_view = std::make_pair (models->at (i).id_, descr_model.view_id);
                    poses_cache_[pair_model_view] = pose_matrix;

                    //load keypoints and save them to cache
                    typename pcl::PointCloud<PointInT>::Ptr keypoints (new pcl::PointCloud<PointInT> ());
                    pcl::io::loadPCDFile (dir_keypoints.str (), *keypoints);
                    keypoints_cache_[pair_model_view] = keypoints;
                }

                typename pcl::PointCloud<FeatureT>::Ptr signature (new pcl::PointCloud<FeatureT> ());
                pcl::io::loadPCDFile (full_file_name, *signature);

                int size_feat = sizeof(signature->points[0].histogram) / sizeof(float);

                for (size_t dd = 0; dd < signature->points.size (); dd++)
                {
                    descr_model.keypoint_id = static_cast<int> (dd);
                    descr_model.descr.resize (size_feat);

                    memcpy (&descr_model.descr[0], &signature->points[dd].histogram[0], size_feat * sizeof(float));

                    flann_models_.push_back (descr_model);
                }
            }
        }
    }

    convertToFLANN (flann_models_, flann_data_);

    flann_index_ = new flann::Index<DistT> (flann_data_, flann::KDTreeIndexParams (4));
    flann_index_->buildIndex ();
}
Пример #2
0
  void
  pcl::rec_3d_framework::GlobalNNCVFHRecognizer<Distance, PointInT, FeatureT>::loadFeaturesAndCreateFLANN ()
  {

    boost::shared_ptr < std::vector<ModelT> > models = source_->getModels ();

    std::map < std::string, boost::shared_ptr<std::vector<int> > > single_categories;
    if (use_single_categories_)
    {
      for (size_t i = 0; i < models->size (); i++)
      {
        std::map<std::string, boost::shared_ptr<std::vector<int> > >::iterator it;
        std::string cat_model = models->at (i).class_;
        it = single_categories.find (cat_model);
        if (it == single_categories.end ())
        {
          boost::shared_ptr < std::vector<int> > v (new std::vector<int>);
          single_categories[cat_model] = v;
        }
      }
    }

    for (size_t i = 0; i < models->size (); i++)
    {
      std::string path = source_->getModelDescriptorDir (models->at (i), training_dir_, descr_name_);
      bf::path inside = path;
      bf::directory_iterator end_itr;

      for (bf::directory_iterator itr_in (inside); itr_in != end_itr; ++itr_in)
      {
#if BOOST_FILESYSTEM_VERSION == 3
        std::string file_name = (itr_in->path ().filename ()).string();
#else
        std::string file_name = (itr_in->path ()).filename ();
#endif

        std::vector < std::string > strs;
        boost::split (strs, file_name, boost::is_any_of ("_"));

        if (strs[0] == "descriptor")
        {

          int view_id = atoi (strs[1].c_str ());
          std::vector < std::string > strs1;
          boost::split (strs1, strs[2], boost::is_any_of ("."));
          int descriptor_id = atoi (strs1[0].c_str ());

          std::string full_file_name = itr_in->path ().string ();
          typename pcl::PointCloud<FeatureT>::Ptr signature (new pcl::PointCloud<FeatureT>);
          pcl::io::loadPCDFile (full_file_name, *signature);

          flann_model descr_model;
          descr_model.model = models->at (i);
          descr_model.view_id = view_id;
          descr_model.descriptor_id = descriptor_id;

          int size_feat = sizeof(signature->points[0].histogram) / sizeof(double);
          descr_model.descr.resize (size_feat);
          memcpy (&descr_model.descr[0], &signature->points[0].histogram[0], size_feat * sizeof(double));

          if (use_single_categories_)
          {
            std::map<std::string, boost::shared_ptr<std::vector<int> > >::iterator it;
            std::string cat_model = models->at (i).class_;
            it = single_categories.find (cat_model);
            if (it == single_categories.end ())
            {
              std::cout << cat_model << std::endl;
              std::cout << "Should not happen..." << std::endl;
            }
            else
            {
              it->second->push_back (static_cast<int> (flann_models_.size ()));
            }
          }

          flann_models_.push_back (descr_model);

          if (use_cache_)
          {

            std::stringstream dir_pose;
            dir_pose << path << "/pose_" << descr_model.view_id << ".txt";

            Eigen::Matrix4d pose_matrix;
            PersistenceUtils::readMatrixFromFile (dir_pose.str (), pose_matrix);

            std::pair<std::string, int> pair_model_view = std::make_pair (models->at (i).id_, descr_model.view_id);
            poses_cache_[pair_model_view] = pose_matrix;
          }
        }
      }
    }

    convertToFLANN (flann_models_, flann_data_);
    flann_index_ = new flann::Index<DistT> (flann_data_, flann::LinearIndexParams ());
    flann_index_->buildIndex ();

    //single categories...
    if (use_single_categories_)
    {
      std::map<std::string, boost::shared_ptr<std::vector<int> > >::iterator it;

      single_categories_data_.resize (single_categories.size ());
      single_categories_index_.resize (single_categories.size ());
      single_categories_pointers_to_models_.resize (single_categories.size ());

      int kk = 0;
      for (it = single_categories.begin (); it != single_categories.end (); it++)
      {
        //create index and flann data
        convertToFLANN (flann_models_, it->second, single_categories_data_[kk]);
        single_categories_index_[kk] = new flann::Index<DistT> (single_categories_data_[kk], flann::LinearIndexParams ());
        single_categories_pointers_to_models_[kk] = it->second;

        category_to_vectors_indices_[it->first] = kk;
        kk++;
      }
    }
  }