2 #ifndef vpdt_update_mog_h_ 3 #define vpdt_update_mog_h_ 18 # include <vcl_msvc_warnings.h> 24 template <
class mog_type>
33 typedef typename gaussian_type::field_type
F;
39 unsigned int max_cmp = 5)
47 void insert(mog_type& mixture,
const F& sample,
T init_weight)
const 54 mixture.remove_last();
59 if (mixture.num_components() > 0)
60 init_weight /= (1-init_weight)*mixture.norm_const();
73 unsigned int match(
const mog_type& mix,
const F& sample,
74 const T& gt2,
T& sqr_dist )
const 76 const unsigned int mix_nc = mix.num_components();
77 for (
unsigned int i=0; i<mix_nc; ++i) {
79 sqr_dist = g.sqr_mahal_dist(sample);
97 template <
class mog_type>
102 typedef typename gaussian_type::field_type
F;
107 unsigned int max_cmp = 5,
110 T init_weight =
T(0.1),
111 T min_stdev =
T(0.16) )
125 void update( mog_type& mix,
const F& sample,
T alpha )
const 129 if (i<mix.num_components())
134 T rho = alpha * std::exp(-sqr_dist/2);
141 mix.set_weight(i, mix.weight(i) + alpha/((1-alpha)*mix.norm_const()));
169 template <
class mog_type>
174 typedef typename gaussian_type::field_type
F;
179 unsigned int max_cmp = 5,
181 T min_stdev =
T(0.16),
182 bool use_wta =
false )
191 this->
update(mix, sample,
T(1)/num_obs);
200 const unsigned int mix_nc = mix.num_components();
201 for (
unsigned int i=0; i<mix_nc; ++i) {
203 num_obs += g.num_observations;
209 std::vector<std::pair<unsigned int,double> >
210 matches(
const mog_type& mix,
const F& sample)
const 212 const unsigned int mix_nc = mix.num_components();
214 std::vector<std::pair<unsigned int,double> > matchez;
216 for (
unsigned int i=0; i<mix_nc; ++i) {
218 double sqr_dist = g.sqr_mahal_dist(sample);
220 matchez.push_back(std::pair<unsigned int,double>(i,sqr_dist));
223 if (matchez.size() == 1) {
224 matchez[0].second = 1.0;
227 else if (matchez.size() > 1) {
228 for (
unsigned int j=0; j<matchez.size(); ++j) {
229 unsigned int& i = matchez[j].first;
230 double& p = matchez[j].second;
232 p = mix.weight(i) * g.norm_const() * std::exp(-p/2);
236 for (
unsigned int j=0; j<matchez.size(); ++j) {
237 matchez[j].second /= sum_p;
247 double max_p =
m[0].second;
248 unsigned int max_j = 0;
249 for (
unsigned int j=1; j<
m.size(); ++j) {
250 if (
m[j].second > max_p) {
255 m[0].first =
m[max_j].first;
261 void update( mog_type& mix,
const F& sample,
T alpha )
const 263 const unsigned int mix_nc = mix.num_components();
269 std::vector<std::pair<unsigned int,double> >
m =
matches(mix, sample);
275 T w_inc = alpha / ((
T(1)-alpha)*mix.norm_const());
276 for (
unsigned int j=0; j<
m.size(); ++j) {
277 unsigned int i =
m[j].first;
278 double p =
m[j].second;
280 g.num_observations +=
T(p);
281 T rho = (
T(1)-alpha)/g.num_observations + alpha;
289 mix.set_weight(i, mix.weight(i)+p*w_inc);
316 template <
class mog_type>
321 typedef typename gaussian_type::field_type
F;
326 unsigned int max_cmp = 5,
328 T min_stdev =
T(0.16),
329 unsigned int ws = 300)
335 void operator() ( mog_type& mix,
const F& sample,
T sample_weight =
T(1) )
const 337 this->
update(mix, sample, sample_weight);
346 const unsigned int mix_nc = mix.num_components();
347 for (
unsigned int i=0; i<mix_nc; ++i) {
354 void update( mog_type& mix,
const F& sample,
T samp_weight )
const 356 assert(samp_weight > 0.0);
362 unsigned int i = this->
match(mix,sample,
gt2_,sqr_dist);
363 if (i<mix.num_components())
368 T rho = (
T(1)-alpha)/w + alpha;
377 this->
insert(mix,sample,alpha);
382 const unsigned int mix_nc = mix.num_components();
383 for (
unsigned int i=0; i<mix_nc; ++i) {
384 mix.set_weight(i, mix.weight(i)*scale);
399 #endif // vpdt_update_mog_h_ mog_type::component_type gaussian_type
mog_type::component_type gaussian_type
T gt2_
Squared Gaussian Mahalanobis distance threshold.
vpdt_field_traits< F >::scalar_type T
void winner_take_all(std::vector< std::pair< unsigned int, double > > &m) const
Apply a winner-take-all strategy to the matches.
void operator()(mog_type &mix, const F &sample) const
The main function.
gaussian_type::field_type F
T total_weight(const mog_type &mix) const
Count the total mixture weight.
gaussian_type::field_type F
T min_var_
Minimum variance allowed in each Gaussian component.
Iterative updating of Gaussians.
gaussian_type init_gaussian_
A model for new Gaussians inserted.
T init_weight_
The initial weight for added Gaussians.
std::vector< std::pair< unsigned int, double > > matches(const mog_type &mix, const F &sample) const
find all matches within the gt2_ threshold and compute the probability of each.
A mixture of Gaussians adaptive updater base on Leotta-Mundy.
T gt2_
Squared Gaussian Mahalanobis distance threshold.
vpdt_mog_updater(const gaussian_type &init_gaussian, unsigned int max_cmp=5)
Constructor.
gaussian_type::field_type F
T alpha_
The learning rate.
vpdt_field_traits< F >::scalar_type T
A mixture of Gaussians updater.
vpdt_field_traits< F >::scalar_type T
void vpdt_update_gaussian(vpdt_gaussian< F, Covar > &gaussian, typename vpdt_field_traits< F >::scalar_type rho, const F &sample)
Update the statistics given a 1D Gaussian distribution and a learning rate.
void insert(mog_type &mixture, const F &sample, T init_weight) const
insert a sample in the mixture.
mog_type::field_type field_type
vpdt_field_traits< F >::scalar_type T
mog_type::component_type gaussian_type
Attach a "number of observations" variable to any distribution.
T min_var_
Minimum variance allowed in each Gaussian component.
A mixture of Gaussians adaptive updater based on Stauffer-Grimson.
The Stauffer-Grimson ordering function of mixture component fitness.
void update(mog_type &mix, const F &sample, T samp_weight) const
Update the mixture with sample using learning rate alpha.
vpdt_mog_sg_updater(const gaussian_type &init_gaussian, unsigned int max_cmp=5, T g_thresh=T(2.5), T alpha=T(0.1), T init_weight=T(0.1), T min_stdev=T(0.16))
Constructor.
vpdt_mog_lee_updater(const gaussian_type &init_gaussian, unsigned int max_cmp=5, T g_thresh=T(2.5), T min_stdev=T(0.16), bool use_wta=false)
Constructor.
void update(mog_type &mix, const F &sample, T alpha) const
Update the mixture with sample using learning rate alpha.
A generic Gaussian distribution.
void operator()(mog_type &mix, const F &sample, T sample_weight=T(1)) const
The main function.
bool use_winner_take_all_
Use a winner-take_all strategy.
mog_type::component_type gaussian_type
unsigned int max_components_
The maximum number of components in the mixture.
specialized template trait classes for properties of a field type
A mixture of Gaussians adaptive updater based on D.
void operator()(mog_type &mix, const F &sample) const
The main function.
T gt2_
Squared Gaussian Mahalanobis distance threshold.
vpdt_mog_lm_updater(const gaussian_type &init_gaussian, unsigned int max_cmp=5, T g_thresh=T(2.5), T min_stdev=T(0.16), unsigned int ws=300)
Constructor.
The field traits class (scalar).
T min_var_
Minimum variance allowed in each Gaussian component.
A mixture of a fixed type of distributions.
Ordering functions for sorting mixtures of Gaussian components.
T total_num_obs(const mog_type &mix) const
Count the total number of observations in all components.
mog_type distribution_type
gaussian_type::field_type F
unsigned int window_size_
The moving window size in number of frames.
void update(mog_type &mix, const F &sample, T alpha) const
Update the mixture with sample using learning rate alpha.
unsigned int match(const mog_type &mix, const F &sample, const T >2, T &sqr_dist) const
Return the index of the first Gaussian within the threshold distance.