cmtkEntropyMinimizationIntensityCorrectionFunctionalBase.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkEntropyMinimizationIntensityCorrectionFunctionalBase_h_included_
00034 #define __cmtkEntropyMinimizationIntensityCorrectionFunctionalBase_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkFunctional.h>
00039 #include <System/cmtkSmartPtr.h>
00040 #include <System/cmtkThreads.h>
00041 
00042 #include <Base/cmtkUniformVolume.h>
00043 #include <Base/cmtkDataGrid.h>
00044 #include <Base/cmtkHistogram.h>
00045 #include <Base/cmtkLogHistogram.h>
00046 #include <Base/cmtkTemplateArray.h>
00047 
00048 #include <vector>
00049 
00050 namespace
00051 cmtk
00052 {
00053 
00056 
00057 class EntropyMinimizationIntensityCorrectionFunctionalBase
00058   : public Functional
00059 {
00060 public:
00062   typedef EntropyMinimizationIntensityCorrectionFunctionalBase Self;
00063 
00065   typedef SmartPointer<Self> SmartPtr;
00066 
00068   typedef Functional Superclass;
00069 
00071   EntropyMinimizationIntensityCorrectionFunctionalBase() 
00072     : m_SamplingDensity( 1.0 ),
00073       m_NumberOfHistogramBins( 256 ),
00074       m_UseLogIntensities( false )
00075   {}
00076 
00078   virtual ~EntropyMinimizationIntensityCorrectionFunctionalBase() {}
00079 
00081   virtual size_t GetNumberOfMonomialsAdd() const = 0;
00082 
00084   virtual size_t GetNumberOfMonomialsMul() const = 0; 
00085 
00087   virtual void SetInputImage( UniformVolume::SmartConstPtr& inputImage );
00088 
00090   virtual void SetForegroundMask( const UniformVolume& foregroundMask );
00091 
00093   virtual void SetSamplingDensity( const float samplingDensity )
00094   {
00095     this->m_SamplingDensity = samplingDensity;
00096   }
00097 
00099   virtual void SetNumberOfHistogramBins( const size_t numberOfHistogramBins )
00100   {
00101     this->m_NumberOfHistogramBins = numberOfHistogramBins;
00102   }
00103 
00109   void SetUseLogIntensities( const bool flag )
00110   {
00111     this->m_UseLogIntensities = flag;
00112   }
00113 
00115   virtual UniformVolume::SmartPtr& GetOutputImage( const bool update = false )
00116   {
00117     if ( update )
00118       this->UpdateOutputImage( false /*foregroundOnly*/);
00119     
00120     return this->m_OutputImage;
00121   }
00122 
00124   virtual UniformVolume::SmartPtr& GetOutputImage( CoordinateVector& v, const bool foregroundOnly = false );
00125   
00127   virtual UniformVolume::SmartPtr GetBiasFieldAdd( const bool updateCompleteImage = false )
00128   {
00129     if ( updateCompleteImage )
00130       this->UpdateBiasFieldAdd( false /*foregroundOnly*/ );
00131 
00132     UniformVolume::SmartPtr biasField( this->m_OutputImage->CloneGrid() );
00133     biasField->SetData( this->m_BiasFieldAdd );
00134     return biasField;
00135   }
00136 
00138   virtual void SetBiasFieldAdd( const UniformVolume& biasFieldAdd )
00139   {
00140     biasFieldAdd.GetData()->BlockCopy( *(this->m_BiasFieldAdd), 0, 0, this->m_BiasFieldAdd->GetDataSize() );
00141   }
00142 
00144   virtual UniformVolume::SmartPtr GetBiasFieldMul( const bool updateCompleteImage = false )
00145   {
00146     if ( updateCompleteImage )
00147       this->UpdateBiasFieldMul( false /*foregroundOnly*/ );
00148 
00149     UniformVolume::SmartPtr biasField( this->m_OutputImage->CloneGrid() );
00150     biasField->SetData( this->m_BiasFieldMul );
00151     return biasField;
00152   }
00153 
00155   virtual void SetBiasFieldMul( const UniformVolume& biasFieldMul )
00156   {
00157     biasFieldMul.GetData()->BlockCopy( *(this->m_BiasFieldMul), 0, 0, this->m_BiasFieldMul->GetDataSize() );
00158   }
00159 
00161   virtual Self::ReturnType Evaluate()
00162   {
00163     return static_cast<Self::ReturnType>( -this->m_OutputImage->GetData()->GetEntropy( *this->m_EntropyHistogram ) );
00164   }
00165 
00167   virtual Self::ReturnType EvaluateAt( CoordinateVector& v );
00168 
00169 #ifdef CMTK_BUILD_DEMO
00170 
00171   virtual void SnapshotAt( CoordinateVector& v );
00172 #endif
00173 
00174 protected:
00176   UniformVolume::SmartConstPtr m_InputImage;
00177 
00179   Types::DataItem m_InputImageRange;
00180 
00182   UniformVolume::SmartPtr m_OutputImage;
00183 
00185   typedef Histogram<unsigned int> HistogramType;
00186 
00188   typedef LogHistogram<unsigned int> LogHistogramType;
00189 
00191   HistogramType::SmartPtr m_EntropyHistogram;
00192 
00194   std::vector<bool> m_ForegroundMask;
00195 
00197   virtual void UpdateCorrectionFactors() = 0;
00198 
00202   virtual void UpdateOutputImage( const bool foregroundOnly = true );
00203 
00205   FloatArray::SmartPtr m_BiasFieldAdd;
00206 
00208   FloatArray::SmartPtr m_BiasFieldMul;
00209 
00213   virtual void UpdateBiasFields( const bool foregroundOnly = true ) = 0;
00214 
00218   virtual void UpdateBiasFieldAdd( const bool foregroundOnly = true ) = 0;
00219 
00223   virtual void UpdateBiasFieldMul( const bool foregroundOnly = true ) = 0;
00224 
00226   size_t m_NumberOfPixels;
00227 
00228 protected:  
00233   float m_SamplingDensity;
00234 
00236   size_t m_NumberOfHistogramBins;
00237 
00239   bool m_UseLogIntensities;
00240 
00241 private:  
00243   class UpdateOutputImageThreadParameters :
00244     public ThreadParameters<Self>
00245   {
00246   public:
00248     bool m_ForegroundOnly;
00249   };
00250 
00252   static void UpdateOutputImageThreadFunc( void* args, const size_t taskIdx, const size_t taskCnt, const size_t, const size_t );
00253 };
00254 
00256 
00257 } // namespace cmtk
00258 
00259 #endif // #ifndef __cmtkEntropyMinimizationIntensityCorrectionFunctionalBase_h_included_
00260 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines