cmtkEntropyMinimizationIntensityCorrectionFunctionalDevice.txx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 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: 2113 $
00026 //
00027 //  $LastChangedDate: 2010-07-30 11:22:13 -0700 (Fri, 30 Jul 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkEntropyMinimizationIntensityCorrectionFunctionalDevice.h"
00034 #include "cmtkEntropyMinimizationIntensityCorrectionFunctionalDevice_kernels.h"
00035 
00036 template<unsigned int NOrderAdd,unsigned int NOrderMul>
00037 void
00038 cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>
00039 ::SetInputImage( UniformVolume::SmartConstPtr& inputImage )
00040 {
00041   this->Superclass::SetInputImage( inputImage );
00042   this->m_InputImageDevice = DeviceUniformVolume::Create( *inputImage, 512 );
00043   this->m_NumberOfPixels = inputImage->GetNumberOfPixels();
00044 
00045   this->m_HistogramDevice = DeviceHistogram::Create( this->m_NumberOfHistogramBins );
00046 }
00047 
00048 template<unsigned int NOrderAdd,unsigned int NOrderMul>
00049 void
00050 cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>
00051 ::SetForegroundMask( const UniformVolume& foregroundMask )
00052 {
00053   this->Superclass::SetForegroundMask( foregroundMask );
00054 
00055   std::vector<int> maskCopy( this->m_NumberOfPixels );
00056   for ( size_t i = 0; i < this->m_NumberOfPixels; ++i )
00057     {
00058     if ( this->m_ForegroundMask[i] )
00059       maskCopy[i] = 1;
00060     else
00061       maskCopy[i] = 0;
00062     }
00063 
00064   this->m_ForegroundMaskDevice = DeviceMemory<int>::Create( this->m_NumberOfPixels, &maskCopy[0], 512 );
00065 }
00066 
00067 #pragma GCC diagnostic ignored "-Wtype-limits"
00068 template<unsigned int NOrderAdd,unsigned int NOrderMul>
00069 typename cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>::ReturnType
00070 cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>
00071 ::EvaluateWithGradient
00072 ( CoordinateVector& v, CoordinateVector& g, const Types::Coordinate step )
00073 { 
00074   const typename Self::ReturnType baseValue = this->EvaluateAt( v );
00075   
00076   for ( size_t dim = 0; dim < this->VariableParamVectorDim(); ++dim ) 
00077     {
00078     const Types::Coordinate stepScale = this->GetParamStep( dim, step );
00079     if ( stepScale <= 0 ) 
00080       {
00081       g[dim] = 0;
00082       } 
00083     else
00084       {
00085       const Types::Coordinate v0 = v[dim];
00086       
00087       v[dim] += stepScale;
00088       const typename Self::ReturnType upper = this->EvaluateAt( v );
00089       
00090       v[dim] = v0 - stepScale;
00091       const  typename Self::ReturnType lower = this->EvaluateAt( v );
00092       
00093       v[dim] = v0;
00094       
00095       if ( (upper > baseValue) || (lower > baseValue) ) 
00096         {
00097         g[dim] = upper-lower;
00098         } 
00099       else 
00100         {
00101         g[dim] = 0;
00102         }
00103       }
00104     }  
00105 
00106   return baseValue;
00107 }
00108 
00109 template<unsigned int NOrderAdd,unsigned int NOrderMul>
00110 void
00111 cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>
00112 ::UpdateOutputImageDevice()
00113 {
00114   if ( !this->m_OutputDataDevice )
00115     this->m_OutputDataDevice = DeviceMemory<float>::Create( this->m_NumberOfPixels, 512 );
00116 
00117   float* input = this->m_InputImageDevice->GetDataOnDevice().Ptr();
00118   float* output = this->m_OutputDataDevice->Ptr();
00119 
00120   const int dims0 = this->m_InputImage->m_Dims[0];
00121   const int dims1 = this->m_InputImage->m_Dims[1];
00122   const int dims2 = this->m_InputImage->m_Dims[2];
00123 
00124   if ( Self::PolynomialTypeMul::NumberOfMonomials )
00125     {
00126     std::vector<float> parameters( Self::PolynomialTypeMul::NumberOfMonomials ), corrections( Self::PolynomialTypeMul::NumberOfMonomials );
00127     for ( size_t i = 0; i < Self::PolynomialTypeMul::NumberOfMonomials; ++i )
00128       {
00129       parameters[i] = static_cast<float>( this->m_CoefficientsMul[i] );
00130       corrections[i] = static_cast<float>( this->m_AddCorrectionMul[i] );
00131       }
00132     EntropyMinimizationIntensityCorrectionFunctionalDeviceUpdateOutputImage( output, input, dims0, dims1, dims2, NOrderMul, 1 /*multiply*/, Self::PolynomialTypeMul::NumberOfMonomials, &parameters[0], &corrections[0] );
00133     input = output; // if additive bias also, apply to output of multiplicative stage
00134     }
00135 
00136   if ( Self::PolynomialTypeAdd::NumberOfMonomials )
00137     {
00138     std::vector<float> parameters( Self::PolynomialTypeAdd::NumberOfMonomials ), corrections( Self::PolynomialTypeAdd::NumberOfMonomials );
00139     for ( size_t i = 0; i < Self::PolynomialTypeAdd::NumberOfMonomials; ++i )
00140       {
00141       parameters[i] = static_cast<float>( this->m_CoefficientsAdd[i] );
00142       corrections[i] = static_cast<float>( this->m_AddCorrectionAdd[i] );
00143       }
00144     EntropyMinimizationIntensityCorrectionFunctionalDeviceUpdateOutputImage( output, input, dims0, dims1, dims2, NOrderAdd, 0 /*multiply*/, Self::PolynomialTypeAdd::NumberOfMonomials, &parameters[0], &corrections[0] );
00145     }
00146 }
00147 
00148 template<unsigned int NOrderAdd,unsigned int NOrderMul>
00149 typename cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>::ReturnType 
00150 cmtk::EntropyMinimizationIntensityCorrectionFunctionalDevice<NOrderAdd,NOrderMul>
00151 ::EvaluateDevice()
00152 {
00153   const Types::DataItemRange range = this->m_EntropyHistogram->GetRange();
00154   this->m_HistogramDevice->Reset();
00155   this->m_HistogramDevice->Populate( *this->m_OutputDataDevice, *this->m_ForegroundMaskDevice, range.m_LowerBound, range.m_UpperBound, this->m_UseLogIntensities );
00156 
00157   return -this->m_HistogramDevice->GetEntropy();
00158 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines