Jerome Piovano

Research in Medical Imaging and Computer Vision

/home/jpiovano/src/Odyssee++/trunk/Libs/LevelSet/src/SegmentationModule/CircularityModule.h

Go to the documentation of this file.
00001 
00009 #ifndef CIRCULARITYMODULE_H
00010 #define CIRCULARITYMODULE_H
00011 
00012 #include <SegmentationModule/SegmentationModule.h>
00013 #include <image/RecFilters.H>
00014 #include <image/IIR.H>
00015 #include <DericheFilter.h> 
00016 
00017 
00018 namespace segmentation {
00019 
00020 
00026         template <unsigned DIM, typename Pixel = float>
00027         class CircularityModule : public SegmentationModule<DIM, Pixel>
00028         {
00029         public:
00030 
00031                 typedef           SegmentationModule<DIM, Pixel>  base;
00032                 typedef typename  base::LevelSet                  LevelSet;
00033                 typedef typename  base::Index                     Index;
00034                 typedef typename  base::Schemes                   Schemes;
00035                 
00036                 typedef           BaseImage<DIM, float>           ImageFloat;
00037                 typedef           Histogram<int>                  Histogram;
00038 
00039 
00042 
00044                 CircularityModule(float       _weight = 1.f,      
00045                                   bool        _var=false          
00046                                  );
00047 
00049                 virtual ~CircularityModule();
00050 
00052 
00054 
00056                 void use_variance(bool _var=true);
00058                 Histogram * hist_x();
00060                 Histogram * hist_y();
00061 
00063 
00065 
00067                 float value(const LevelSet & phi, const Index & ind);
00069                 float energy(const LevelSet & phi, const Index & ind);
00071                 void init(const LevelSet & phi);
00073                 void update(const LevelSet & phi);
00075                 void save_result(const LevelSet & phi, std::string evodir, int iter);
00076 
00078                 void update_params(const LevelSet & phi);
00080                 void init_histograms(const LevelSet & phi);
00082                 void update_histograms(const LevelSet & phi);
00084                 void save_histograms(std::string evodir, int iter);
00085         
00087 
00088         protected:
00089 
00092 
00093                 std::vector<Pixel>          m_ll_x;       
00094                 std::vector<Pixel>          m_ll_y;       
00095                 Histogram *                 m_hist_x;     
00096                 Histogram *                 m_hist_y;     
00097                 Images::Index<DIM, float>   m_centroid;   
00098                 Images::Index<DIM, float>   m_variance;   
00099                 bool                        m_var;        
00100 
00102 
00103         };
00104 
00105 
00107         // Constructors - Destructor
00108 
00109 
00110         template <unsigned DIM, typename Pixel>
00111                 CircularityModule<DIM, Pixel>::CircularityModule(float _weight, bool _var) 
00112                 : SegmentationModule<DIM, Pixel>(_weight, true), m_hist_x(NULL), m_hist_y(NULL)
00113         { }
00114 
00115         template <unsigned DIM, typename Pixel>
00116                 CircularityModule<DIM, Pixel>::~CircularityModule() 
00117         { 
00118                 if (m_hist_x != NULL){
00119                         delete m_hist_x;
00120                         delete m_hist_y;
00121                 }
00122         }
00123 
00124 
00126         //  Member functions
00127 
00128 
00129         template <unsigned DIM, typename Pixel>
00130                 void CircularityModule<DIM, Pixel>::use_variance(bool _var)
00131         {
00132                 m_var = _var;
00133         }
00134 
00135         template <unsigned DIM, typename Pixel>
00136                 typename CircularityModule<DIM, Pixel>::Histogram* CircularityModule<DIM, Pixel>::hist_x()
00137         {
00138                 return m_hist_x;
00139         }
00140 
00141         template <unsigned DIM, typename Pixel>
00142                 typename CircularityModule<DIM, Pixel>::Histogram* CircularityModule<DIM, Pixel>::hist_y()
00143         {
00144                 return m_hist_y;
00145         }
00146 
00148         // Member functions
00149 
00150 
00151         template <unsigned DIM, typename Pixel>
00152                 float CircularityModule<DIM, Pixel>::value(const LevelSet & phi, const Index & ind)
00153         { 
00154                 const int _x = ind(1);
00155                 const int _y = ind(2);
00156 
00157                 return  - Globals::delta(phi(ind)) * (Maths::sqr(_x - m_centroid(1))  + Maths::sqr(_y - m_centroid(2)));
00158 
00159                 
00160 /*              
00161                 const int _x = ind(1);
00162                 const int _y = ind(2);
00163 
00164                 const Pixel ll_x = m_ll_x[m_hist_x->get_index(_x)];
00165                 const Pixel ll_y = m_ll_y[m_hist_y->get_index(_y)];
00166 
00167                 //return Globals::delta(phi(ind)) * (ll_pos - ll_neg);
00168                 return  Globals::delta(phi(ind)) * (ll_x + ll_y);
00169 */
00170         }       
00171 
00172         template <unsigned DIM, typename Pixel>
00173                 float CircularityModule<DIM, Pixel>::energy(const LevelSet & phi, const Index & ind)
00174         {
00175                 //TODO 
00176                 return 0;
00177         }
00178 
00179         template <unsigned DIM, typename Pixel>
00180                 void CircularityModule<DIM, Pixel>::init(const LevelSet & phi)
00181         {
00182                 base::init(phi);
00183                 init_histograms(phi);
00184                 update_params(phi);
00185         }
00186 
00187         template <unsigned DIM, typename Pixel>
00188                 void CircularityModule<DIM, Pixel>::update(const LevelSet & phi)
00189         {
00190                 update_histograms(phi);
00191                 update_params(phi);
00192         }
00193 
00194         template <unsigned DIM, typename Pixel>
00195                  void CircularityModule<DIM, Pixel>::update_params(const LevelSet & phi) 
00196         {
00197                 m_centroid = 0.0f;
00198                 m_variance = 0.0f;
00199 
00200                 // compute the mean and variance of the shape in the x and y dimension
00201                 m_hist_x->compute_statistics(m_centroid(1), m_variance(1), m_var);
00202                 m_hist_y->compute_statistics(m_centroid(2), m_variance(2), m_var);
00203 
00204                 // if the size of the histogram has changed, resize de log-likelihood vectors
00205                 if (m_ll_x.size() != m_hist_x->size()) m_ll_x.resize(m_hist_x->size());
00206                 if (m_ll_y.size() != m_hist_y->size()) m_ll_y.resize(m_hist_y->size());
00207 
00208                 if (m_var) {
00209                         for(int i=0; i<m_ll_x.size(); i++)
00210                                 m_ll_x[i] = -0.5f*log(2*M_PI*m_variance(1)) - 0.5f*Maths::sqr( i+m_hist_x->min() - m_centroid(1))/m_variance(1);
00211                         for(int i=0; i<m_ll_y.size(); i++)
00212                                 m_ll_y[i] = -0.5f*log(2*M_PI*m_variance(2)) - 0.5f*Maths::sqr( i+m_hist_y->min() - m_centroid(2))/m_variance(2);
00213                 }
00214                 else{
00215                         for(int i=0; i<m_ll_x.size(); i++)
00216                                 m_ll_x[i] =  - Maths::sqr(i+m_hist_x->min() - m_centroid(1));
00217                         for(int i=0; i<m_ll_y.size(); i++) 
00218                                 m_ll_y[i] =  - Maths::sqr(i+m_hist_y->min() - m_centroid(2));
00219                 }
00220         }
00221 
00222         template <unsigned DIM, typename Pixel>
00223                 void CircularityModule<DIM, Pixel>::init_histograms(const LevelSet & phi)
00224         {
00225                 if (m_hist_x == NULL){
00226                         m_hist_x = new Histogram(0, phi.extent(0), true);
00227                         m_hist_y = new Histogram(0, phi.extent(1), true);
00228                 }
00229 
00230                 //computation of the first histograms
00231 
00232                 m_hist_x->clear();
00233                 m_hist_y->clear();
00234 
00235                 for (typename LevelSet::template iterator<domain> ind=phi.begin() ; ind!=phi.end() ; ++ind ){
00236                         const Pixel _phi = phi(ind);
00237                         const int     _x = (ind.position())(1);
00238                         const int     _y = (ind.position())(2);
00239 
00240                         if (_phi>=0) {
00241                                 m_hist_x->add_pixel(_x);
00242                                 m_hist_y->add_pixel(_y);
00243                         }
00244                 }
00245         }
00246 
00247         template <unsigned DIM, typename Pixel>
00248                 void CircularityModule<DIM, Pixel>::update_histograms(const LevelSet & phi)
00249         {
00250                  if (m_hist_x == NULL){
00251                         std::cerr << "Error : Histograms not initialized"  << std::endl;
00252                         exit(1);
00253                 }
00254 
00255                 //computation of the histograms using previous ones
00256 
00257                 if (!m_hist_x->adaptative() && !m_hist_y->adaptative()) return;
00258 
00259                 for (unsigned int i = 0 ; i < phi.pos_to_neg_index() ; i++){
00260                         const Index& ind = phi.get_point(i);
00261                         const int     _x = ind(1);
00262                         const int     _y = ind(2);
00263 
00264                         m_hist_x->remove_pixel(_x);
00265                         m_hist_y->remove_pixel(_y);
00266                 }
00267 
00268                 for (unsigned int i = phi.neg_to_pos_index() ; i < phi.nb_points() ; i++){
00269                         const Index& ind = phi.get_point(i);
00270                         const int     _x = ind(1);
00271                         const int     _y = ind(2);
00272 
00273                         m_hist_x->add_pixel(_x);
00274                         m_hist_y->add_pixel(_y);
00275                 }
00276         }
00277 
00278         template <unsigned DIM, typename Pixel>
00279                 void CircularityModule<DIM, Pixel>::save_result(const LevelSet & phi, std::string evodir, int iter)
00280         {
00281                 ImageFloat tmp(phi.shape());
00282                 std::ostringstream name;
00283 
00284                 for (typename LevelSet::template iterator<domain> i=phi.begin() ; i!=phi.end() ; ++i ){
00285                         const Pixel _phi = phi(i);
00286                         if (_phi<=0) tmp(i) = 1;
00287                         else {
00288                                 double coeff = exp(-0.5f*( Maths::sqr(i(1)-m_centroid(1))/Maths::sqr(m_variance(1)) + Maths::sqr(i(2)-m_centroid(2))/Maths::sqr(m_variance(2)) ))/2;
00289                                 tmp(i) = coeff;
00290                         }
00291                 }
00292 
00293                 name.str("");
00294                 name << evodir << "centroid_" << std::setw(6) << std::setfill('0') << iter << ".inr";
00295                 std::ofstream file(name.str().c_str());
00296                 file << format("inrimage") << tmp;
00297                 std::cout <<  " - Saved " << name.str() << " ..."  << std::endl;
00298 
00299                 save_histograms(evodir, iter);
00300 
00301                 if (m_var) {
00302                         
00303                         std::ofstream file2;
00304                         // plot des gaussienes associes a mean et var
00305 
00306                         name.str("");
00307                         name << evodir << "Gaussian_x_" << std::setw(6) << std::setfill('0') << iter << ".txt";
00308                         file2.open(name.str().c_str());
00309                         for(int i=m_hist_x->min(); i < m_hist_x->max() ; i++) 
00310                                 file2 << i << " " << Globals::gauss_density(m_centroid(1), m_variance(1), i) << std::endl;
00311                         file2.close();
00312                         std::cout <<  " - Saved " << name.str() << " ..."  << std::endl;
00313         
00314                         name.str("");
00315                         name << evodir << "Gaussian_y_" << std::setw(6) << std::setfill('0') << iter << ".txt";
00316                         file2.open(name.str().c_str());
00317                         for(int i=m_hist_y->min(); i < m_hist_y->max() ; i++) 
00318                                 file2 << i << " " << Globals::gauss_density(m_centroid(2), m_variance(2), i) << std::endl;
00319                         file2.close();
00320                         std::cout <<  " - Saved " << name.str() << " ..."  << std::endl;
00321                 }
00322         }
00323 
00324         
00325         template <unsigned DIM, typename Pixel>
00326                 void CircularityModule<DIM, Pixel>::save_histograms(std::string evodir, int iter) 
00327         {
00328                 if (m_hist_x == NULL){
00329                         std::cerr << "Error : Histograms not initialized"  << std::endl;
00330                         exit(1);
00331                 }
00332 
00333                 std::ostringstream name;
00334                 std::ofstream file;
00335 
00336                 name.str("");
00337                 name << evodir << "Hist_x_" << std::setw(6) << std::setfill('0') << iter << ".txt";
00338                 file.open(name.str().c_str());
00339                 for(int i=m_hist_x->min(); i < m_hist_x->max() ; i++) 
00340                         file << i << " " << (*m_hist_x)[i]  << std::endl;
00341                 file.close();
00342                 std::cout <<  " - Saved " << name.str() << " ..."  << std::endl;
00343 
00344                 name.str("");
00345                 name << evodir << "Hist_y_" << std::setw(6) << std::setfill('0') << iter << ".txt";
00346                 file.open(name.str().c_str());
00347                 for(int i=m_hist_y->min(); i < m_hist_y->max() ; i++) 
00348                         file << i << " " << (*m_hist_y)[i] << std::endl;
00349                 file.close();
00350                 std::cout <<  " - Saved " << name.str() << " ..."  << std::endl;
00351         }
00352 
00353 }
00354 
00355 #endif // CIRCULARITYMODULE_H

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