/home/jpiovano/src/Odyssee++/trunk/Libs/LevelSet/src/SegmentationModule/SparseHistogram.h
00001 +
00009 #ifndef HISTOGRAM_H
00010 #define HISTOGRAM_H
00011
00012 namespace segmentation {
00013
00035 template <unsigned DIM, typename ElementType = float>
00036 class Histogram
00037 {
00038 public:
00039
00040 typedef BaseImage<DIM, int> base;
00041 typedef typename base::Shape Shape;
00042 typedef typename base::Index Index;
00043
00044 typedef Index<DIM,ElementType> Element;
00045
00048
00050 Histogram();
00051
00053 Histogram(const Element & _br,
00054 const Element & _tl,
00055 bool _adaptive = true,
00056 const Element & _quant_step=1
00057 );
00058
00060 template <typename ElementType2>
00061 Histogram(const Histogram<DIM, ElementType2>& hist):base(hist), m_adaptive(hist.m_adaptive), m_nb_elt(0), m_quant_step(hist.m_quant_step)
00062 {
00063 for (typename base::template iterator<domain> i=this->begin() ; i!=this->end() ; ++i )
00064 m_nb_elt += base::operator()(i)
00065 }
00066
00067 using base::operator=;
00068
00069 void resize(const Shape & s);
00070
00072
00074
00076 Element & quantification_step();
00078 bool& adaptative();
00080 int nb_elements();
00082 void add_element(const Element & _elt);
00084 void remove_element(const Element & _elt);
00086 void clear();
00087
00089
00091
00093 void compute_statistics(float& mean, float& var, bool compute_variance=false);
00094
00096
00098
00099 bool m_adaptive;
00100 int m_nb_elt;
00101 Element m_quant_step;
00102 };
00103
00104
00106
00107
00108
00109 template <unsigned DIM, typename ElementType>
00110 Histogram<DIM, ElementType>::Histogram()
00111 : base(), m_adaptive(true), m_nb_elt(0), m_quant_step(1)
00112 { }
00113
00114 template <unsigned DIM, typename ElementType>
00115 Histogram<DIM, ElementType>::Histogram(const Shape & _shape, bool _adaptive )
00116 : base(_shape), m_adaptive(_adaptive), m_nb_elt(0), m_quant_step(1)
00117 {
00118 base::operator=(0);
00119 }
00120
00121 template<unsigned DIM, typename ElementType>
00122 void Histogram<DIM, ElementType>::resize(const Shape& s)
00123 {
00124 base::resize(s);
00125 m_mask.resize(s);
00126 base::operator=(0);
00127 m_nb_elt = 0;
00128 }
00129
00130
00132
00133
00134
00135 template <unsigned DIM, typename ElementType>
00136 Element & Histogram<DIM, ElementType>::quantification_step()
00137 {
00138 return m_quant_step;
00139 }
00140
00141 template <unsigned DIM, typename ElementType>
00142 bool& Histogram<DIM, ElementType>::adaptative()
00143 {
00144 return m_adaptive;
00145 }
00146
00147 template <unsigned DIM, typename ElementType>
00148 void Histogram<DIM, ElementType>::clear()
00149 {
00150 if (m_adaptive){
00151 m_nb_elt = 0;
00152 base::operator=(0);
00153 }
00154 }
00155
00156 template <unsigned DIM, typename ElementType>
00157 int Histogram<DIM, ElementType>::nb_elements()
00158 {
00159 return m_nb_elt;
00160 }
00161
00162 template <unsigned DIM, typename ElementType>
00163 void Histogram<DIM, ElementType>::add_element(const Element & _elt)
00164 {
00165 if (m_adaptive){
00166
00167 Index ind;
00168 for (i=0; i<DIM; i++)
00169 ind(i) = floor(_elt(i) / m_quant_step(i));
00170
00171 base::operator()(ind)++;
00172 m_nb_elt++;
00173 }
00174 }
00175
00176 template <unsigned DIM, typename ElementType>
00177 void Histogram<DIM, ElementType>::remove_element(const Element & _elt)
00178 {
00179 if (m_adaptive){
00180
00181 Index ind;
00182 for (i=0; i<DIM; i++)
00183 ind(i) = floor(_elt(i) / m_quant_step(i));
00184
00185 if (base::operator()(ind) > 0){
00186 base::operator()(ind)--;
00187 m_nb_elt--;
00188 }
00189 }
00190 }
00191
00192 template <unsigned DIM, typename ElementType>
00193 void Histogram<DIM, ElementType>::clear()
00194 {
00195 if (m_adaptive){
00196 m_nb_elt = 0;
00197 base::operator=(0);
00198 }
00199 }
00200
00201
00203
00204
00205
00206 template <unsigned DIM, typename ElementType>
00207 void Histogram<DIM, ElementType>::compute_statistics(Element& mean, Element& var, bool compute_variance)
00208 {
00209 mean = 0;
00210
00211 for (typename base::template iterator<domain> i=this->begin() ; i!=this->end() ; ++i )
00212 mean += i.position() * base::operator()(i)
00213
00214 mean /= static_cast<float>(m_nb_elt);
00215
00216 if (compute_variance){
00217 var = 0;
00218 for (typename base::template iterator<domain> i=this->begin() ; i!=this->end() ; ++i )
00219 var += base::operator()(i)*Maths::sqr(i.position() - mean);
00220
00221 var /= static_cast<float>(m_nb_elt);
00222 }
00223 }
00224 }
00225
00226 #endif // HISTOGRAM_H