contrib/brl/bbas/bsta/bsta_otsu_threshold.txx
Go to the documentation of this file.
00001 #ifndef bsta_otsu_threshold_txx_
00002 #define bsta_otsu_threshold_txx_
00003 
00004 #include "bsta_otsu_threshold.h"
00005 
00006 template <class T>
00007 bsta_otsu_threshold<T>::bsta_otsu_threshold(bsta_histogram<T> const& hist)
00008 : distribution_1d_(hist)
00009 {
00010 }
00011 
00012 template <class T>
00013 bsta_otsu_threshold<T>::bsta_otsu_threshold(vcl_vector<T> data, T low , T high)
00014 :distribution_1d_(low,high,data)
00015 {
00016 }
00017 
00018 template <class T>
00019 bsta_otsu_threshold<T>::~bsta_otsu_threshold()
00020 {
00021 }
00022 
00023 template <class T>
00024 unsigned bsta_otsu_threshold<T>::bin_threshold() const
00025 {
00026   T no_of_pixels_in_class1=0;
00027   int threshold =0;
00028   T max_between_class_variance=0;
00029   T no_of_pixels_in_class2 = distribution_1d_.area();
00030 
00031   for (unsigned t=0; t<distribution_1d_.nbins(); ++t)
00032   {
00033     T mu1 = distribution_1d_.mean(0, t);
00034     T mu2 = distribution_1d_.mean(t, distribution_1d_.nbins()-1);
00035 
00036     T between_class_variance=no_of_pixels_in_class1*no_of_pixels_in_class2*
00037                              (mu1-mu2)*(mu1-mu2);
00038 
00039     if (between_class_variance>max_between_class_variance)
00040     {
00041       max_between_class_variance=between_class_variance;
00042       threshold=t;
00043     }
00044     no_of_pixels_in_class1+=distribution_1d_.counts(t);
00045     no_of_pixels_in_class2-=distribution_1d_.counts(t);
00046   }
00047 
00048   return threshold;
00049 }
00050 
00051 #undef BSTA_OTSU_THRESHOLD_INSTANTIATE
00052 #define BSTA_OTSU_THRESHOLD_INSTANTIATE(T) \
00053 template class bsta_otsu_threshold<T >
00054 
00055 #endif // bsta_otsu_threshold_txx_