/home/oan/prosjekt/gotools/segmentation/gpl_distro/lsseg_1.0_gpl/include/ChanVeseForce.h

Go to the documentation of this file.
00001 //===========================================================================
00002 // The Level-Set Segmentation Library (LSSEG)
00003 //
00004 //
00005 // Copyright (C) 2000-2005 SINTEF ICT, Applied Mathematics, Norway.
00006 //
00007 // This program is free software; you can redistribute it and/or          
00008 // modify it under the terms of the GNU General Public License            
00009 // as published by the Free Software Foundation version 2 of the License. 
00010 //
00011 // This program is distributed in the hope that it will be useful,        
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00014 // GNU General Public License for more details.                           
00015 //
00016 // You should have received a copy of the GNU General Public License      
00017 // along with this program; if not, write to the Free Software            
00018 // Foundation, Inc.,                                                      
00019 // 59 Temple Place - Suite 330,                                           
00020 // Boston, MA  02111-1307, USA.                                           
00021 //
00022 // Contact information: e-mail: tor.dokken@sintef.no                      
00023 // SINTEF ICT, Department of Applied Mathematics,                         
00024 // P.O. Box 124 Blindern,                                                 
00025 // 0314 Oslo, Norway.                                                     
00026 // 
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //
00033 //===========================================================================
00034 //===========================================================================
00035 //                                                                           
00036 // File: FG_ChanVese.h                                                       
00037 //                                                                           
00038 // Created: Wed Jan 11 09:51:32 2006                                         
00039 //                                                                           
00040 // Author: Odd A. Andersen <Odd.Andersen@sintef.no>
00041 //                                                                           
00042 // Revision: $Id: ChanVeseForce.h,v 1.6 2006/09/20 22:55:47 oan Exp $
00043 //                                                                           
00044 // Description:
00048 //                                                                           
00049 //===========================================================================
00050 
00051 #ifndef _FG_CHANVESE_H
00052 #define _FG_CHANVESE_H
00053 
00054 #include "errormacros.h"
00055 #include "ForceGenerator.h"
00056 
00057 namespace lsseg{
00058 
00059 
00060 //===========================================================================
00073 class ChanVeseForce : public ForceGenerator
00074 //===========================================================================
00075 {
00076 public:
00078      ChanVeseForce(const Image<double>* img) : img_(img) 
00079     {
00080         assert(img_->numChannels() == 1); // @@ temporary restriction...
00081     }
00082 
00083     virtual ~ChanVeseForce() { }
00084 
00085     virtual void init(const Image<double>* img, const Mask* mask) 
00086     {
00087         MESSAGE("Warning!  Mask ignored in ChanVeseForce::init()");
00088         img_ = img;
00089     }
00090 
00091     virtual void update(const LevelSetFunction& phi) 
00092     {
00093         assert(phi.spatial_compatible(*img_));
00094         const int SIZE = phi.size();
00095         unsigned int nb_in = 0;
00096         unsigned int nb_out = 0;
00097         mu_inside_ = mu_outside_ = 0;
00098         for (int i = 0; i < SIZE; ++i) {
00099             if (phi[i] > 0) {
00100                 ++nb_out;
00101                 mu_outside_ += (*img_)[i];
00102             } else {
00103                 ++nb_in;
00104                 mu_inside_ += (*img_)[i];
00105             }
00106         }
00107         if (nb_in) {
00108             mu_inside_ /= nb_in;
00109         }
00110         if (nb_out) {
00111             mu_outside_ /= nb_out;
00112         }
00113     }
00114 
00115     virtual void force(LevelSetFunction& phi, const Mask* mask = 0) const
00116     {
00117         assert(!m || img_->spatial_compatible(*m));
00118         assert(img_->spatial_compatible(phi));
00119         const size_t SIZE = phi.size();
00120         
00121         // explicit checking of mask presence for optimization reasons
00122         if (m) {
00123             if (mask_) {
00124                 for (size_t it = 0; it < SIZE; ++it) {
00125                     phi[it] = ((*m)[it] && (*mask_)[it]) ? force(it) : 0;
00126                 }
00127             } else {
00128                 for (size_t it = 0; it < SIZE; ++it) {
00129                     phi[it] = (*m)[it] ? force(it) : 0;
00130                 }
00131             }
00132         } else {
00133             if (mask_) {
00134                 for (size_t it = 0; it < SIZE; ++it) {
00135                     phi[it] = (*mask_)[it] ? force(it) : 0;
00136                 }
00137             } else {
00138                 for (size_t it = 0; it < SIZE; ++it) {
00139                     phi[it] = force(it);
00140                 }
00141             }
00142         }
00143     }
00144     
00145 
00146     virtual double force3D(int x, int y, int z) const 
00147     {
00148         int ix = img_->indexOf(x, y, z);
00149         return force(ix);
00150     }
00151 
00152     virtual double force2D(int x, int y) const
00153     {
00154         int ix = img_->indexOf(x,y);
00155         return force(ix);
00156     }
00157 
00158     virtual double force(size_t ix) const 
00159     {
00160         double tmp = (*img_)[ix];
00161         double d_out = mu_outside_ - tmp;
00162         double d_in = mu_inside_ - tmp;
00163         return d_out * d_out - d_in * d_in;
00164     }
00165 
00166 private:
00168     double mu_inside_;
00170     double mu_outside_;
00172     const Image<double>* const img_;
00173 };
00174 
00175 }; // end namespace lsseg
00176 
00177 #endif // _FG_CHANVESE_H
00178 

Generated on Tue Nov 28 18:35:47 2006 for lsseg by  doxygen 1.4.7