Jerome Piovano

Research in Medical Imaging and Computer Vision

/home/jpiovano/src/Odyssee++/trunk/Libs/LevelSet/src/Globals.h

Go to the documentation of this file.
00001 
00009 #ifndef GLOBALS_H
00010 #define GLOBALS_H
00011         
00012 #include <fstream>
00013 #include <iostream>
00014 #include <iomanip>
00015 #include <cmath>
00016 #include <sys/time.h>
00017 #include <string.h>
00018         
00019 #include <Image.H>
00020         
00021 using std::ostream;
00022 using std::vector;
00023 using namespace Images;
00024 
00025 // Save results each SAVE_EVOLUTION iteration
00026 //#define SAVE_EVOLUTION 1
00027 
00028 // Print infos about execution time
00029 //#define PRINT_TIME
00030 
00031         
00032 namespace levelset {
00033                 
00038         class Globals
00039         {
00040         public:
00041 
00044 
00045                 static const float DIV_SQRT_2PI = 1.0f/2.506628274631f;
00046                 static const float LOG_2PI      = 0.79817987f;
00047                 static const float DELTA        = 1.0f;
00048                 static const float EPS          = 1e-6f;
00049 
00051 
00053 
00055                 static void initChrono(timeval & t) 
00056                 {
00057                         gettimeofday(&t, NULL);
00058                 }
00059                                         
00061                 static void printChrono(std::string s, timeval & t) 
00062                 {
00063                         double tmp = t.tv_sec+(t.tv_usec/1000000.0);
00064                         gettimeofday(&t, NULL);
00065                         double seconds = t.tv_sec+(t.tv_usec/1000000.0) - tmp;
00066                         std::cout << s << seconds << " s." << std::endl;
00067                 }
00068 
00070 
00072 
00074                 template<typename T>
00075                 static float gauss_density(float mean, float var, T val) {
00076                         return (DIV_SQRT_2PI/(var+EPS))*exp(-0.5f*Maths::sqr(val-mean)/ (var+EPS));
00077                 }
00078                                         
00080                 template<typename T>
00081                 static float log_ll(float mean, float var, T val) {
00082                         return -0.5f*LOG_2PI - log(var+EPS) - 0.5f*sqr(val-mean)/ (var+EPS);
00083                 }
00084                                         
00086                 static float delta(float v, float epsilon=1.0f) {
00087                         if(v> epsilon) return 0;
00088                         if(v<-epsilon) return 0;
00089                         return float(1+cos(float(M_PI)*v/epsilon))/epsilon/2.0;
00090                 }
00091                                         
00093                 static float heaviside(float v, float epsilon=1.0f){
00094                         if(v>epsilon) return 1;
00095                         if(v<-epsilon) return 0;
00096                         return float(0.5f*(1+v/epsilon+(1/M_PI)*sin(M_PI*v/epsilon)));
00097                 }
00098                 
00100 
00102                         
00104                 template<unsigned DIM, typename Pixel>
00105                 static void stats(const BaseImage<DIM, Pixel>& image, Pixel& min, Pixel& max, float& mean, float* variance=NULL)
00106                 {
00107                         typename BaseImage<DIM, Pixel>::template const_iterator<pixel> ind=image.begin();
00108                         min    = *ind;
00109                         max    = *ind;
00110                         mean   = 0;
00111                                                 
00112                         while (ind != image.end()){
00113                                 if (*ind > max) max = *ind;
00114                                 if (*ind < min) min = *ind;
00115                                 mean += *ind;
00116                                 ind++;
00117                         }
00118                         
00119                         mean /= image.size();
00120                         
00121                         if (variance != NULL){
00122                                 for (ind=image.begin() ; ind!=image.end() ; ind++)
00123                                 *variance += ( *ind - mean )*( *ind - mean );//Maths::sqr( *ind - mean )
00124                                 *variance /= image.size();
00125 
00126                                 //DEBUG PR VOIR
00127                                 *variance = sqrt(*variance);
00128                         }
00129                 }
00130 
00132                 template<unsigned DIM, typename Pixel>
00133                 static void normalize(BaseImage<DIM, Pixel>& image, Pixel min, Pixel max)
00134                 {
00135                         Pixel _min, _max, _mean;
00136                         Globals::stats(image, _min, _max, _mean);
00137                 
00138                         for (typename BaseImage<DIM, Pixel>::template iterator<pixel> ind=image.begin() ; ind!=image.end() ; ++ind )
00139                                 *ind =  (Pixel) ((*ind-_min)/(_max-_min)*(max-min)+min);
00140                 }
00141                 
00143                 template<unsigned DIM, typename Pixel>
00144                 static void cut(BaseImage<DIM, Pixel>& image, Pixel min, Pixel max)
00145                 {
00146                         for (typename BaseImage<DIM, Pixel>::template iterator<pixel> ind=image.begin() ; ind!=image.end() ; ++ind )
00147                                 *ind =  (*ind < min)? min : ( (*ind > max)? max : *ind );
00148                 }
00149                         
00151         };
00152 }
00153                 
00154 #endif // GLOBALS_H

For further information, please contact Jerome Piovano - Last update 2008-02-08