1 // This is brl/bbas/bsta/algo/bsta_mean_shift.h
2 #ifndef bsta_mean_shift_h_
3 #define bsta_mean_shift_h_
4 //:
5 // \file
6 // \brief Classes to run mean shift algorithm on data distributions to find its modes
7 //        Implements mean shift with a flat kernel of fixed bandwidth_
8 //
9 // \author Ozge C. Ozcanli
10 // \date February 10, 2009
11 //
12 // \verbatim
13 //  Modifications
14 //   (none yet)
15 // \endverbatim
16 //
17 
18 #include <iostream>
19 #include <utility>
20 #include <bsta/bsta_parzen_sphere.h>
21 #include <bsta/bsta_mixture.h>
22 #include <bsta/bsta_attributes.h>
23 #include <bsta/bsta_gaussian_sphere.h>
24 #include <bsta/bsta_gaussian_full.h>
25 #include <vnl/vnl_random.h>
26 #ifdef _MSC_VER
27 #  include <vcl_msvc_warnings.h>
28 #endif
29 #include <bsta/algo/bsta_sample_set.h>
30 
31 template <class T, unsigned n>
32 class bsta_mean_shift
33 {
34  public:
35   typedef typename bsta_parzen_sphere<T,n>::vector_type vector_;
36   //typedef typename bsta_parzen_sphere<T,n>::covar_type var_t;
37 
38   //Constructor
bsta_mean_shift()39   bsta_mean_shift() : max_iter_(1000) {}
40 
41   //: initializes seeds randomly and finds all the resulting modes
42   //  \p epsilon : the difference required for updating to come to a halt
43   //  \p percentage: the percentage of the sample set to initialize as seed
44   bool find_modes(bsta_sample_set<T,n>& set, vnl_random & rng, float percentage = 10.0f, T epsilon = 10e-3);
45 
46   //: use all the samples to get its mode, no need for random seed picking
47   bool find_modes(bsta_sample_set<T,n>& set, T epsilon = 10e-3);
48 
49   //: trim modes that are within epsilon distance to each other
50   //  \p epsilon : the difference required for two modes to be merged
51   bool trim_modes(bsta_sample_set<T,n>& set, T epsilon = 10e-3);
52 
53   //: merge the mode with samples less than cnt to one of the modes depending on its samples mean-shift paths
54   bool merge_modes(bsta_sample_set<T,n>& set, int cnt, T epsilon);
55 
clear()56   void clear() { modes_.clear(); }
57 
size()58   unsigned size() const { return modes_.size(); }
modes()59   std::vector<vector_ >& modes() { return modes_; }
60 
61   //: the default for maximum iterations to quit search starting from a seed is 1000
set_max_iter(unsigned iter)62   void set_max_iter(unsigned iter) { max_iter_ = iter; }
63 
64  private:
65 
66   std::vector<vector_ > modes_;  // modes of the distribution
67 
68   unsigned max_iter_;
69 };
70 
71 #endif // bsta_mean_shift_h_
72