Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 , Self::PolynomialTypeMul::NumberOfMonomials, ¶meters[0], &corrections[0] );
00133 input = output;
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 , Self::PolynomialTypeAdd::NumberOfMonomials, ¶meters[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 }