/home/jpiovano/src/Odyssee++/trunk/Libs/LevelSet/src/SegmentationModule/Histogram.h
Go to the documentation of this file.00001
00009 #ifndef HISTOGRAM_H
00010 #define HISTOGRAM_H
00011
00012 namespace segmentation {
00013
00020 template <typename Pixel = float>
00021 class Histogram
00022 {
00023 public:
00024
00027
00029 Histogram( Pixel _min = 0,
00030 Pixel _max = 256,
00031 bool _adaptive = true,
00032 int * _data = NULL
00033 );
00034
00036 template <typename Pixel2>
00037 Histogram(const Histogram<Pixel2>& _hist)
00038 :m_min(_hist.m_min), m_max(_hist.m_max), m_adaptive(_hist.m_adaptive), m_nb_pix(0)
00039 {
00040 m_hist = new int[static_cast<int>(m_max - m_min)];
00041
00042 for (int i = 0; i < static_cast<int>(m_max-m_min) ; i++){
00043 m_hist[i] = _hist.data()[i];
00044 m_nb_pix += _hist.data()[i];
00045 }
00046 }
00047
00049 Histogram& operator=(int * _data);
00050
00051 virtual ~Histogram();
00052
00054
00056
00058 Pixel min();
00060 Pixel max();
00062 int size();
00064 int * data() const;
00066 int nb_pixels();
00068 int get_index(Pixel _val);
00070 int operator[](Pixel _val);
00071
00073
00075
00077 bool& adaptative();
00079 void clear();
00081 void resize(Pixel _min, Pixel _max);
00083 void add_pixel(Pixel _val);
00085 void remove_pixel(Pixel _val);
00086
00088
00090
00092 void compute_statistics(float& mean, float& var, bool compute_variance=false);
00093
00095
00097
00098 int * m_hist;
00099 bool m_adaptive;
00100 Pixel m_min;
00101 Pixel m_max;
00102 int m_nb_pix;
00103 };
00104
00105
00107
00108
00109
00110 template <typename Pixel>
00111 Histogram<Pixel>::Histogram( Pixel _min, Pixel _max, bool _adaptive, int * _data)
00112 :m_min(_min), m_max(_max), m_adaptive(_adaptive), m_nb_pix(0)
00113 {
00114 m_hist = new int[static_cast<int>(m_max - m_min)];
00115
00116 if (_data == NULL)
00117 for (int i = 0; i < static_cast<int>(m_max-m_min) ; i++)
00118 m_hist[i] = 0;
00119 else
00120 for (int i = 0; i < static_cast<int>(m_max-m_min) ; i++){
00121 m_hist[i] = _data[i];
00122 m_nb_pix += _data[i];
00123 }
00124 }
00125
00126 template <typename Pixel>
00127 Histogram<Pixel> & Histogram<Pixel>::operator=(int * _data)
00128 {
00129 m_nb_pix = 0;
00130
00131 for (int i = 0; i < static_cast<int>(m_max-m_min) ; i++){
00132 m_hist[i] = _data[i];
00133 m_nb_pix += _data[i];
00134 }
00135 }
00136
00137 template <typename Pixel>
00138 Histogram<Pixel>::~Histogram()
00139 {
00140 delete m_hist;
00141 }
00142
00143
00145
00146
00147
00148 template <typename Pixel>
00149 Pixel Histogram<Pixel>::min()
00150 {
00151 return m_min;
00152 }
00153
00154 template <typename Pixel>
00155 Pixel Histogram<Pixel>::max()
00156 {
00157 return m_max;
00158 }
00159
00160 template <typename Pixel>
00161 int Histogram<Pixel>::size()
00162 {
00163 return static_cast<int>(m_max-m_min);
00164 }
00165
00166 template <typename Pixel>
00167 int * Histogram<Pixel>::data() const
00168 {
00169 return m_hist;
00170 }
00171
00172 template <typename Pixel>
00173 int Histogram<Pixel>::nb_pixels()
00174 {
00175 return m_nb_pix;
00176 }
00177
00178 template <typename Pixel>
00179 int Histogram<Pixel>::get_index(Pixel _val)
00180 {
00181 if (_val < m_min) return 0;
00182 else if (_val >= m_max) return size() - 1;
00183
00184 return static_cast<int>(_val - m_min);
00185 }
00186
00187 template <typename Pixel>
00188 int Histogram<Pixel>::operator[](Pixel _val)
00189 {
00190 return m_hist[get_index(_val)];
00191 }
00192
00193
00195
00196
00197
00198 template <typename Pixel>
00199 bool& Histogram<Pixel>::adaptative()
00200 {
00201 return m_adaptive;
00202 }
00203
00204 template <typename Pixel>
00205 void Histogram<Pixel>::clear()
00206 {
00207 if (m_adaptive){
00208 m_nb_pix = 0;
00209 for (int i = 0; i < static_cast<int>(m_max-m_min) ; i++)
00210 m_hist[i] = 0;
00211 }
00212 }
00213
00214 template <typename Pixel>
00215 void Histogram<Pixel>::resize(Pixel _min, Pixel _max)
00216 {
00217 if (m_adaptive){
00218 m_min = _min;
00219 m_max = _max;
00220
00221 delete m_hist;
00222
00223 m_hist = new int[static_cast<int>(m_max - m_min)];
00224 }
00225 }
00226
00227 template <typename Pixel>
00228 void Histogram<Pixel>::add_pixel(Pixel _val)
00229 {
00230 if (m_adaptive){
00231 m_hist[get_index(_val)]++;
00232 m_nb_pix++;
00233 }
00234 }
00235
00236 template <typename Pixel>
00237 void Histogram<Pixel>::remove_pixel(Pixel _val)
00238 {
00239 if (m_adaptive && m_hist[get_index(_val)]>0){
00240 m_hist[get_index(_val)]--;
00241 m_nb_pix--;
00242 }
00243 }
00244
00245
00247
00248
00249
00250 template <typename Pixel>
00251 void Histogram<Pixel>::compute_statistics(float& mean, float& var, bool compute_variance)
00252 {
00253 mean = 0;
00254
00255 for (int i = static_cast<int>(m_min) ; i < static_cast<int>(m_max) ; i++)
00256 mean += i * operator[](i);
00257 mean /= static_cast<float>(m_nb_pix);
00258
00259 if (compute_variance){
00260 var = 0;
00261 for (int i = static_cast<int>(m_min) ; i < static_cast<int>(m_max) ; i++)
00262 var += operator[](i)*Maths::sqr(i-static_cast<float>(mean));
00263 var /= static_cast<float>(m_nb_pix);
00264 }
00265 }
00266 }
00267
00268 #endif // HISTOGRAM_H