00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #ifndef PCL_IMPLICIT_SHAPE_MODEL_H_
00037 #define PCL_IMPLICIT_SHAPE_MODEL_H_
00038 
00039 #include <vector>
00040 #include <fstream>
00041 #include <limits>
00042 #include <Eigen/src/Core/Matrix.h>
00043 #include <pcl/pcl_base.h>
00044 #include <pcl/point_types.h>
00045 #include <pcl/point_representation.h>
00046 #include <pcl/features/feature.h>
00047 #include <pcl/features/spin_image.h>
00048 #include <pcl/filters/voxel_grid.h>
00049 #include <pcl/filters/extract_indices.h>
00050 #include <pcl/search/search.h>
00051 #include <pcl/kdtree/kdtree.h>
00052 #include <pcl/kdtree/kdtree_flann.h>
00053 #include <pcl/kdtree/impl/kdtree_flann.hpp>
00054 
00055 namespace pcl
00056 {
00058   struct ISMPeak
00059   {
00061     PCL_ADD_POINT4D;
00062 
00064     double density;
00065 
00067     int class_id;
00068 
00069     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00070   } EIGEN_ALIGN16;
00071 
00072   namespace features
00073   {
00076     template <typename PointT>
00077     class PCL_EXPORTS ISMVoteList
00078     {
00079       public:
00080 
00082         ISMVoteList ();
00083 
00085         virtual
00086         ~ISMVoteList ();
00087 
00093         void
00094         addVote (pcl::InterestPoint& in_vote, const PointT &vote_origin, int in_class);
00095 
00099         typename pcl::PointCloud<pcl::PointXYZRGB>::Ptr
00100         getColoredCloud (typename pcl::PointCloud<PointT>::Ptr cloud = 0);
00101 
00108         void
00109         findStrongestPeaks (std::vector<ISMPeak, Eigen::aligned_allocator<ISMPeak> > &out_peaks, int in_class_id, double in_non_maxima_radius, double in_sigma);
00110 
00115         double
00116         getDensityAtPoint (const PointT &point, double sigma_dist);
00117 
00119         unsigned int
00120         getNumberOfVotes ();
00121 
00122       protected:
00123 
00125         void
00126         validateTree ();
00127 
00128         Eigen::Vector3f
00129         shiftMean (const Eigen::Vector3f& snapPt, const double in_dSigmaDist);
00130 
00131       protected:
00132 
00134         pcl::PointCloud<pcl::InterestPoint>::Ptr votes_;
00135 
00137         bool tree_is_valid_;
00138 
00140         typename pcl::PointCloud<PointT>::Ptr votes_origins_;
00141 
00143         std::vector<int> votes_class_;
00144 
00146         pcl::KdTreeFLANN<pcl::InterestPoint>::Ptr tree_;
00147 
00149         std::vector<int> k_ind_;
00150 
00152         std::vector<float> k_sqr_dist_;
00153     };
00154  
00158     struct PCL_EXPORTS ISMModel
00159     {
00161       ISMModel ();
00162 
00164       ISMModel (ISMModel const & copy);
00165 
00167       virtual
00168       ~ISMModel ();
00169 
00173       bool
00174       saveModelToFile (std::string& file_name);
00175 
00179       bool
00180       loadModelFromfile (std::string& file_name);
00181 
00183       void
00184       reset ();
00185 
00187       ISMModel & operator = (const ISMModel& other);
00188 
00190       std::vector<std::vector<float> > statistical_weights_;
00191 
00193       std::vector<float> learned_weights_;
00194 
00196       std::vector<unsigned int> classes_;
00197 
00199       std::vector<float> sigmas_;
00200 
00202       Eigen::MatrixXf directions_to_center_;
00203 
00205       Eigen::MatrixXf clusters_centers_;
00206 
00208       std::vector<std::vector<unsigned int> > clusters_;
00209 
00211       unsigned int number_of_classes_;
00212 
00214       unsigned int number_of_visual_words_;
00215 
00217       unsigned int number_of_clusters_;
00218 
00220       unsigned int descriptors_dimension_;
00221 
00222       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00223     };
00224   }
00225 
00226   namespace ism
00227   {
00239     template <int FeatureSize, typename PointT, typename NormalT = pcl::Normal>
00240     class PCL_EXPORTS ImplicitShapeModelEstimation
00241     {
00242       public:
00243 
00244         typedef boost::shared_ptr<pcl::features::ISMModel> ISMModelPtr;
00245 
00246       protected:
00247 
00249         struct PCL_EXPORTS LocationInfo
00250         {
00257           LocationInfo (unsigned int model_num, const PointT& dir_to_center, const PointT& origin, const NormalT& normal) :
00258             model_num_ (model_num),
00259             dir_to_center_ (dir_to_center),
00260             point_ (origin),
00261             normal_ (normal) {};
00262 
00264           unsigned int model_num_;
00265 
00267           PointT dir_to_center_;
00268 
00270           PointT point_;
00271 
00273           NormalT normal_;
00274         };
00275 
00278         typedef struct PCL_EXPORTS TC
00279         {
00280           enum
00281           {
00282             COUNT = 1,
00283             EPS = 2
00284           };
00285 
00291           TC(int type, int max_count, float epsilon) :
00292             type_ (type),
00293             max_count_ (max_count),
00294             epsilon_ (epsilon) {};
00295 
00302           int type_;
00303 
00305           int max_count_;
00306 
00308           float epsilon_;
00309         } TermCriteria;
00310 
00312         struct PCL_EXPORTS VisualWordStat
00313         {
00315           VisualWordStat () :
00316             class_ (-1),
00317             learned_weight_ (0.0f),
00318             dir_to_center_ (0.0f, 0.0f, 0.0f) {};
00319 
00321           int class_;
00322 
00324           float learned_weight_;
00325 
00327           pcl::PointXYZ dir_to_center_;
00328         };
00329 
00330       public:
00331 
00333         ImplicitShapeModelEstimation ();
00334 
00336         virtual
00337         ~ImplicitShapeModelEstimation ();
00338 
00340         std::vector<typename pcl::PointCloud<PointT>::Ptr>
00341                 getTrainingClouds ();
00342 
00346         void
00347                 setTrainingClouds (const std::vector< typename pcl::PointCloud<PointT>::Ptr >& training_clouds);
00348 
00350         std::vector<unsigned int>
00351         getTrainingClasses ();
00352 
00356         void
00357         setTrainingClasses (const std::vector<unsigned int>& training_classes);
00358 
00360         std::vector<typename pcl::PointCloud<NormalT>::Ptr>
00361         getTrainingNormals ();
00362 
00366         void
00367         setTrainingNormals (const std::vector< typename pcl::PointCloud<NormalT>::Ptr >& training_normals);
00368 
00370         float
00371         getSamplingSize ();
00372 
00376         void
00377         setSamplingSize (float sampling_size);
00378 
00380         boost::shared_ptr<pcl::Feature<PointT, pcl::Histogram<FeatureSize> > >
00381         getFeatureEstimator ();
00382 
00387         void
00388         setFeatureEstimator (boost::shared_ptr<pcl::Feature<PointT, pcl::Histogram<FeatureSize> > > feature);
00389 
00391         unsigned int
00392         getNumberOfClusters ();
00393 
00397         void
00398         setNumberOfClusters (unsigned int num_of_clusters);
00399 
00401         std::vector<float>
00402         getSigmaDists ();
00403 
00410         void
00411         setSigmaDists (const std::vector<float>& training_sigmas);
00412 
00417         bool
00418         getNVotState ();
00419 
00423         void
00424         setNVotState (bool state);
00425 
00430         bool
00431         trainISM (ISMModelPtr& trained_model);
00432 
00440         boost::shared_ptr<pcl::features::ISMVoteList<PointT> >
00441         findObjects (ISMModelPtr model, typename pcl::PointCloud<PointT>::Ptr in_cloud, typename pcl::PointCloud<Normal>::Ptr in_normals, int in_class_of_interest);
00442 
00443       protected:
00444 
00450         bool
00451         extractDescriptors (std::vector<pcl::Histogram<FeatureSize> >& histograms,
00452                             std::vector<LocationInfo, Eigen::aligned_allocator<LocationInfo> >& locations);
00453 
00459         bool
00460         clusterDescriptors (std::vector< pcl::Histogram<FeatureSize> >& histograms, Eigen::MatrixXi& labels, Eigen::MatrixXf& clusters_centers);
00461 
00465         void
00466         calculateSigmas (std::vector<float>& sigmas);
00467 
00479         void
00480         calculateWeights (const std::vector< LocationInfo, Eigen::aligned_allocator<LocationInfo> >& locations,
00481                           const Eigen::MatrixXi &labels,
00482                           std::vector<float>& sigmas,
00483                           std::vector<std::vector<unsigned int> >& clusters,
00484                           std::vector<std::vector<float> >& statistical_weights,
00485                           std::vector<float>& learned_weights);
00486 
00493         void
00494         simplifyCloud (typename pcl::PointCloud<PointT>::ConstPtr in_point_cloud,
00495                        typename pcl::PointCloud<NormalT>::ConstPtr in_normal_cloud,
00496                        typename pcl::PointCloud<PointT>::Ptr out_sampled_point_cloud,
00497                        typename pcl::PointCloud<NormalT>::Ptr out_sampled_normal_cloud);
00498 
00503         void
00504         shiftCloud (typename pcl::PointCloud<PointT>::Ptr in_cloud, Eigen::Vector3f shift_point);
00505 
00511         Eigen::Matrix3f
00512         alignYCoordWithNormal (const NormalT& in_normal);
00513 
00518         void
00519         applyTransform (Eigen::Vector3f& io_vec, const Eigen::Matrix3f& in_transform);
00520 
00527         void
00528         estimateFeatures (typename pcl::PointCloud<PointT>::Ptr sampled_point_cloud,
00529                           typename pcl::PointCloud<NormalT>::Ptr normal_cloud,
00530                           typename pcl::PointCloud<pcl::Histogram<FeatureSize> >::Ptr feature_cloud);
00531 
00542         double
00543         computeKMeansClustering (const Eigen::MatrixXf& points_to_cluster,
00544                                  int number_of_clusters,
00545                                  Eigen::MatrixXi& io_labels,
00546                                  TermCriteria criteria,
00547                                  int attempts,
00548                                  int flags,
00549                                  Eigen::MatrixXf& cluster_centers);
00550 
00558         void
00559         generateCentersPP (const Eigen::MatrixXf& data,
00560                            Eigen::MatrixXf& out_centers,
00561                            int number_of_clusters,
00562                            int trials);
00563 
00568         void
00569         generateRandomCenter (const std::vector<Eigen::Vector2f>& boxes, Eigen::VectorXf& center);
00570 
00575         float
00576         computeDistance (Eigen::VectorXf& vec_1, Eigen::VectorXf& vec_2);
00577 
00579         ImplicitShapeModelEstimation&
00580         operator= (const ImplicitShapeModelEstimation&);
00581 
00582       protected:
00583 
00585         std::vector<typename pcl::PointCloud<PointT>::Ptr> training_clouds_;
00586 
00588         std::vector<unsigned int> training_classes_;
00589 
00591         std::vector<typename pcl::PointCloud<NormalT>::Ptr> training_normals_;
00592 
00596         std::vector<float> training_sigmas_;
00597 
00599         float sampling_size_;
00600 
00602         boost::shared_ptr<pcl::Feature<PointT, pcl::Histogram<FeatureSize> > > feature_estimator_;
00603 
00605         unsigned int number_of_clusters_;
00606 
00608         bool n_vot_ON_;
00609 
00613         static const int PP_CENTERS = 2;
00614 
00617         static const int USE_INITIAL_LABELS = 1;
00618     };
00619   }
00620 }
00621 
00622 POINT_CLOUD_REGISTER_POINT_STRUCT (pcl::ISMPeak,
00623   (float, x, x)
00624   (float, y, y)
00625   (float, z, z)
00626   (float, density, ism_density)
00627   (float, class_id, ism_class_id)
00628 )
00629 
00630 #endif  //#ifndef PCL_IMPLICIT_SHAPE_MODEL_H_