/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: itkAnytimeImageToImageMetric2.txx,v $ Language: C++ Date: $Date: 2007/09/28 21:38:43 $ Version: $Revision: 1.32 $ Copyright (c) Insight Software Consortium. All rights reserved. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef _itkAnytimeImageToImageMetric2_txx #define _itkAnytimeImageToImageMetric2_txx #include "itkAnytimeImageToImageMetric2.h" #include "itkEuler2DTransform.h" #include "itkAffineTransform.h" #include "../transforms/itkTransformFacade.h" #include "../special/itkFastOrientedImage.h" namespace itk { /** * Compute image derivatives using a central difference function * if we are not using a BSplineInterpolator, which includes * derivatives. */ //const FixedImageIndexType& fixedIndex, // const FixedImagePointType& fixedPoint, #define FIXGRADIENT \ if(fastOrientedImage) { \ gradient=fastOrientedImage->TransformImageGradientToSpatialGradient(gradient); \ } template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::ComputeImageDerivatives( typename FixedImageSpatialSampleContainer::const_iterator fiter, const MovingImagePointType& mappedPoint, ParametersType& parametergradient, const MovingImageContinuousIndexType& tempIndex) const { //const TransformJacobianType & jacobian; GradientPixelType gradient; const unsigned int ParametersDimension=this->m_Transform->GetNumberOfParameters(); const unsigned int ImageDimension = FixedImageType::ImageDimension; const FastOrientedImage * fastOrientedImage= dynamic_cast *>(this->m_FixedImage.GetPointer()); switch (m_GradientMethod) { case GradientImage: { // Get the gradient by NearestNeighboorInterpolation: // which is equivalent to round up the point components. typename MovingImageType::IndexType mappedIndex; for( unsigned int j = 0; j < MovingImageType::ImageDimension; j++ ) { mappedIndex[j] = static_cast( vnl_math_rnd( tempIndex[j] ) ); } gradient = this->GetGradientImage()->GetPixel( mappedIndex ); FIXGRADIENT; const TransformJacobianType & jacobian = this->m_Transform->GetJacobian( fiter->FixedImagePointValue ); for(unsigned int par=0; par::Zero; for(unsigned int dim=0; dimEvaluateDerivative( mappedPoint ); FIXGRADIENT; const TransformJacobianType & jacobian = this->m_Transform->GetJacobian( fiter->FixedImagePointValue ); for(unsigned int par=0; par::Zero; for(unsigned int dim=0; dimEvaluate( mappedPoint ); FIXGRADIENT; const TransformJacobianType & jacobian = this->m_Transform->GetJacobian( fiter->FixedImagePointValue ); for(unsigned int par=0; par::Zero; for(unsigned int dim=0; dimFixedImageParameterGradient[par]; } } break; case ESM: //itkExceptionMacro( "I dont know how to do the ESM method yet"); if(m_ESMMode) { // This should be identical to inverse compositional for(unsigned int par=0; parFixedImageParameterGradient[par]; } } else { // this should be identical to normal, forward method // Get the gradient by NearestNeighboorInterpolation: // which is equivalent to round up the point components. typename MovingImageType::IndexType mappedIndex; for( unsigned int j = 0; j < MovingImageType::ImageDimension; j++ ){ mappedIndex[j] = static_cast( vnl_math_rnd( tempIndex[j] ) ); } gradient = this->GetGradientImage()->GetPixel( mappedIndex ); FIXGRADIENT; const TransformJacobianType & jacobian = this->m_Transform->GetJacobian( fiter->FixedImagePointValue ); for(unsigned int par=0; par::Zero; for(unsigned int dim=0; dim void AnytimeImageToImageMetric2 ::ComputeFixedGradient() { GradientImageFilterPointer gradientFilter = GradientImageFilterType::New(); const unsigned int ImageDimension = FixedImageType::ImageDimension; gradientFilter->SetInput( this->m_FixedImage ); const typename FixedImageType::SpacingType& spacing = this->m_FixedImage->GetSpacing(); double maximumSpacing=0.0; for(unsigned int i=0; i maximumSpacing ) { maximumSpacing = spacing[i]; } } gradientFilter->SetSigma( maximumSpacing ); gradientFilter->SetNormalizeAcrossScale( true ); gradientFilter->Update(); this->m_FixedGradientImage = gradientFilter->GetOutput(); } /** * Initialize */ template void AnytimeImageToImageMetric2 ::Initialize(void) throw ( ExceptionObject ) { const unsigned int ImageDimension = FixedImageType::ImageDimension; if(!this->m_MovingImage) { itkExceptionMacro(<<"MovingImage is not present"); } // If the image is provided by a source, update the source. if( this->m_MovingImage->GetSource() ) { this->m_MovingImage->GetSource()->Update(); } // if any of the moving image dimensions are less than 4, we have to make // a new image that is a multiple stack of the original image so that // the interpolator and derivative will work. typedef typename MovingImageType::SizeType SizeType; typedef typename MovingImageType::SpacingType SpacingType; SizeType imagesize=this->m_MovingImage->GetBufferedRegion().GetSize(); SizeType newsize=imagesize; SpacingType currentSpacing=this->m_MovingImage->GetSpacing(); std::cout<<"image size: " <::Pointer resampler= itk::ResampleImageFilter::New(); resampler->SetInput(this->m_MovingImage); resampler->SetOutputSpacing(currentSpacing); resampler->SetOutputOrigin(this->m_MovingImage->GetOrigin()); resampler->SetOutputDirection(this->m_MovingImage->GetDirection()); resampler->SetSize(newsize); resampler->Update(); //std::cerr<<"Old Image"<m_MovingImage=resampler->GetOutput(); //std::cerr<<"New Image"<m_ComputeGradient=true; } else { this->m_ComputeGradient=false; } if(m_GradientMethod==BSpline) { m_InterpolatorIsBSpline = true; BSplineInterpolatorType * testPtr = dynamic_cast( this->m_Interpolator.GetPointer() ); if ( !testPtr ) { m_InterpolatorIsBSpline = false; m_GradientMethod=Calculator; std::cerr<<"Interpolator not BSpline, using calculator for derivative"<SetInputImage( this->m_MovingImage ); } if(m_GradientMethod==Inverse||m_GradientMethod==InverseCompositional||m_GradientMethod==ESM) { this->ComputeFixedGradient(); // TransformType* ptr=dynamic_cast(this->m_Transform->CreateAnother().GetPointer()); // this->m_IdentityTransform=TransformPointer (ptr); // this->m_IdentityTransform=(TransformPointer)this->m_Transform->CreateAnother(); this->m_IdentityTransform=(TransformType *)this->m_Transform->CreateAnother().GetPointer(); this->m_IdentityTransform->SetFixedParameters(this->m_Transform->GetFixedParameters()); m_ESMMode=false; m_ICHessianComputed=false; //std::cout<<"id params : "<m_IdentityTransform->GetParameters()<m_IdentityTransform->SetIdentity(); } this->Superclass::Superclass::Initialize(); // Cache the number of transformation parameters this->m_NumberOfParameters = this->m_Transform->GetNumberOfParameters(); this->dpsidphi.SetSize(this->m_NumberOfParameters,this->m_NumberOfParameters); this->m_CurrentChannel=0; //m_TotalPixelsProcessed=0; for (int i=0;im_nFixedImageSamples[i]=0; this->m_nSamples[i]=0; this->m_CachedParameters[i]=new ParametersType(this->m_NumberOfParameters); this->m_DerivativeCache[i]=new DerivativeType(this->m_NumberOfParameters); this->m_ComputationLevel[i]=1.0; this->m_HessianCache[i]=new HessianType(this->m_NumberOfParameters,this->m_NumberOfParameters); this->m_HessianSumCache[i]=new HessianType(this->m_NumberOfParameters,this->m_NumberOfParameters); this->m_HessianSumCacheESM[i]=new HessianType(this->m_NumberOfParameters,this->m_NumberOfParameters); } /** * Allocate memory for the fixed image sample container. */ //std::cout<<"Initializing space for "<< m_NumberOfSpatialSamples<< " samples...."<m_FixedImageSamples.resize( m_NumberOfSpatialSamples); } catch (std::exception e) { itkExceptionMacro( "Resize for " << this->m_NumberOfSpatialSamples<<" samples failed: "); } //std::cout<<"done"<SampleFixedImageDomain( this->m_FixedImageSamples ); if(m_GradientMethod==Inverse||m_GradientMethod==InverseCompositional||m_GradientMethod==ESM) { this->SampleJacobians(this->m_FixedImageSamples); } } /** * Compute the dpsidphi matrix */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::ComputeDpsidphi( ) const { itk::TransformFacade tf(this->m_Transform); tf.ComputeDpsidphi(dpsidphi); } /** * Compute the Jacobians of the fixed image pixels */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::SampleJacobians( FixedImageSpatialSampleContainer& samples ) { //ERRMSG("Computing Jacobians"); typename FixedImageSpatialSampleContainer::iterator iter; typename FixedImageSpatialSampleContainer::const_iterator end=samples.end(); const FastOrientedImage * fastOrientedImage= dynamic_cast *>(this->m_FixedImage.GetPointer()); //TransformParametersType parameters=this->m_Transform->GetParameters(); TransformParametersType identity=(dynamic_cast(this->m_Transform->CreateAnother().GetPointer()))->GetParameters(); //parameters.Fill(0); this->m_Transform->SetParameters(identity); const unsigned int ParametersDimension = this->GetNumberOfParameters(); const unsigned int ImageDimension = FixedImageType::ImageDimension; GradientPixelType gradient; for( iter=samples.begin(); iter != end;++iter ) { gradient=this->m_FixedGradientImage->GetPixel( iter->FixedImageIndex ); FIXGRADIENT; const TransformJacobianType & jacobian = this->m_IdentityTransform->GetJacobian( iter->FixedImagePointValue ); // std::cout<<"point: "<<(*iter).FixedImagePointValue<m_Transform->GetJacobian( (*iter).FixedImagePointValue ); // std::cout<<"G: "< void AnytimeImageToImageMetric2 ::SampleFixedImageDomain( FixedImageSpatialSampleContainer& samples ) { // Set up a random interator within the user specified fixed image region. RandomIterator randIter( this->m_FixedImage, this->GetFixedImageRegion() ); randIter.SetNumberOfSamples( this->GetFixedImageRegion().GetNumberOfPixels() ); randIter.ReinitializeSeed(m_RandomSeed); if(m_PriorityImage!=NULL) randIter.SetPriorityImage(m_PriorityImage); randIter.GoToBegin(); m_NumberOfAvailableSamples=0; typename FixedImageSpatialSampleContainer::iterator iter; typename FixedImageSpatialSampleContainer::const_iterator end=samples.end(); //std::cout<<"Trying to get "<m_FixedImageMask ) { typename Superclass::InputPointType inputPoint; iter=samples.begin(); while( iter != end && !randIter.IsAtEnd()) { // Get sampled index FixedImageIndexType index = randIter.GetIndex(); // Check if the Index is inside the mask, translate index to point this->m_FixedImage->TransformIndexToPhysicalPoint( index, inputPoint ); // If not inside the mask, ignore the point if( !this->m_FixedImageMask->IsInside( inputPoint ) ) { ++randIter; // jump to another random position continue; } // Save index (*iter).FixedImageIndex = index; // Get sampled fixed image value (*iter).FixedImageValue = randIter.Get(); // Translate index to point (*iter).FixedImagePointValue = inputPoint; // Jump to random position ++randIter; ++iter; ++m_NumberOfAvailableSamples; } } else { for( iter=samples.begin(); iter != end && !randIter.IsAtEnd(); ++iter, ++m_NumberOfSpatialSamples ) { // Get sampled index FixedImageIndexType index = randIter.GetIndex(); // Save index (*iter).FixedImageIndex = index; // Get sampled fixed image value (*iter).FixedImageValue = randIter.Get(); // Translate index to point this->m_FixedImage->TransformIndexToPhysicalPoint( index, (*iter).FixedImagePointValue ); // Jump to random position ++randIter; ++m_NumberOfAvailableSamples; } } m_FixedImageSamples.resize( m_NumberOfAvailableSamples); // m_NumberOfSpatialSamples=samples.size(); //std::cout<<"Size of sample set"< void AnytimeImageToImageMetric2 ::GetHessian( const ParametersType& parameters, HessianType& Hessian ) const { //this->ResetComputation(this->m_CurrentChannel); this->GetHessian(parameters,Hessian,this->m_CurrentChannel); } /////////////////////////////////////////// /** * Get the match Measure */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetHessian( const ParametersType& parameters, HessianType& Hessian,const int channel, const double level) const { this->SetComputationLevel(level,channel); //this->ResetComputation(this->m_CurrentChannel); this->GetHessian(parameters,Hessian,channel); } /////////////////////////////////////////// /** * Get the match Measure */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetHessian( const ParametersType& parameters, HessianType& Hessian, const double level) const { this->SetComputationLevel(level,this->m_CurrentChannel); //this->ResetComputation(this->m_CurrentChannel); this->GetHessian(parameters,Hessian,this->m_CurrentChannel); } /////////////////////////////////////////// /** * Get the match Measure */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetValueAndHessian( const ParametersType& parameters, MeasureType& Value,HessianType& Hessian ) const { //this->ResetComputation(this->m_CurrentChannel); this->GetValueAndHessian(parameters, Value, Hessian,this->m_CurrentChannel); } /////////////////////////////////////////// /** * Get the match Measure */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetValueAndHessian( const ParametersType& parameters, MeasureType& Value, HessianType& Hessian,const int channel, const double level) const { this->SetComputationLevel(level,channel); //this->ResetComputation(this->m_CurrentChannel); this->GetValueAndHessian(parameters,Value,Hessian,channel); } /////////////////////////////////////////// /** * Get the match Measure */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetValueAndHessian( const ParametersType& parameters, MeasureType& Value, HessianType& Hessian, const double level) const { this->SetComputationLevel(level,this->m_CurrentChannel); //this->ResetComputation(this->m_CurrentChannel); this->GetValueAndHessian(parameters,Value,Hessian,this->m_CurrentChannel); } /** Get the derivatives of the match measure. */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetDerivativeAndHessian( const ParametersType & parameters, DerivativeType & derivative, HessianType& Hessian, int channel, double level ) const { this->SetComputationLevel(level,channel); this->GetDerivativeAndHessian(parameters,derivative,Hessian,channel); } /** Get value and derivatives for multiple valued optimizers. */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetValueAndDerivativeAndHessian( const ParametersType & parameters, MeasureType& Value, DerivativeType& Derivative,HessianType& Hessian, int channel, double level ) const { this->SetComputationLevel(level,channel); this->GetValueAndDerivativeAndHessian(parameters,Value,Derivative,Hessian,channel); } /** Get the derivatives of the match measure. */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetDerivativeAndHessian( const ParametersType & parameters, DerivativeType & derivative, HessianType& Hessian,double level ) const { this->SetComputationLevel(level,this->m_CurrentChannel); this->GetDerivativeAndHessian(parameters,derivative,Hessian,this->m_CurrentChannel); } /** Get value and derivatives for multiple valued optimizers. */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetValueAndDerivativeAndHessian( const ParametersType & parameters, MeasureType& Value, DerivativeType& Derivative, HessianType& Hessian, double level ) const{ this->SetComputationLevel(level,this->m_CurrentChannel); this->GetValueAndDerivativeAndHessian(parameters,Value,Derivative,Hessian,this->m_CurrentChannel); } template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetDerivativeAndHessian( const ParametersType & parameters, DerivativeType & derivative, HessianType& Hessian) const { this->GetDerivativeAndHessian(parameters,derivative,Hessian,this->m_CurrentChannel); } /** Get value and derivatives for multiple valued optimizers. */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::GetValueAndDerivativeAndHessian( const ParametersType & parameters, MeasureType& Value, DerivativeType& Derivative,HessianType& Hessian) const { this->GetValueAndDerivativeAndHessian(parameters,Value,Derivative,Hessian,this->m_CurrentChannel); } ///////////////////////// /* * Get the match Measure */ template typename AnytimeImageToImageMetric2::MeasureType AnytimeImageToImageMetric2 ::GetValue( const ParametersType& parameters, const int channel) const { MeasureType value; DerivativeType derivative(this->m_NumberOfParameters); HessianType H; m_NumberOfEvaluations++; m_NumberOfValueEvaluations++; // call the combined version if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,false,false,true,true, parameters, value, derivative,H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,false,false,true,false, parameters, value, derivative,H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,false,false,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,false,false,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,true,false,false); this->InvokeEvent( me ); return value; } /* * Get the Derivative Measure */ template < class TFixedImage, class TMovingImage> void AnytimeImageToImageMetric2 ::GetDerivative( const ParametersType& parameters, DerivativeType & derivative, int channel ) const { MeasureType value; HessianType H; m_NumberOfEvaluations++; m_NumberOfDerivativeEvaluations++; // call the combined version if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(false,true,false,true,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(false,true,false,true,false, parameters, value, derivative, H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(false,true,false,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(false,true,false,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,false,true,false); this->InvokeEvent( me ); } /* * Get both the match Measure and theDerivative Measure */ template void AnytimeImageToImageMetric2 ::GetValueAndDerivative(const ParametersType & parameters, MeasureType & value, DerivativeType & derivative, int channel) const { m_NumberOfEvaluations++; m_NumberOfValueEvaluations++; m_NumberOfDerivativeEvaluations++; HessianType H; if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,true,false,true,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,true,false,true,false, parameters, value, derivative, H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,true,false,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,true,false,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,true,true,false); this->InvokeEvent( me ); } /* * Get the match hessian */ template void AnytimeImageToImageMetric2 ::GetHessian( const ParametersType& parameters,HessianType& H, int channel) const { MeasureType value; DerivativeType derivative(this->m_NumberOfParameters); m_NumberOfEvaluations++; m_NumberOfHessianEvaluations++; // call the combined version if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(false,false,true,true,true, parameters, value, derivative,H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(false,false,true,true,false, parameters, value, derivative,H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(false,false,true,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(false,false,true,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,false,false,true); this->InvokeEvent( me ); } /* * Get the match Measure */ template void AnytimeImageToImageMetric2 ::GetValueAndHessian( const ParametersType& parameters,MeasureType& value,HessianType& H, int channel) const { DerivativeType derivative(this->m_NumberOfParameters); m_NumberOfEvaluations++; m_NumberOfValueEvaluations++; m_NumberOfHessianEvaluations++; // call the combined version if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,false,true,true,true, parameters, value, derivative,H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,false,true,true,false, parameters, value, derivative,H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,false,true,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,false,true,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,true,false,true); this->InvokeEvent( me ); } /* * Get the Derivative Measure */ template < class TFixedImage, class TMovingImage> void AnytimeImageToImageMetric2 ::GetDerivativeAndHessian( const ParametersType& parameters, DerivativeType & derivative, HessianType& H,int channel ) const { MeasureType value; m_NumberOfEvaluations++; m_NumberOfDerivativeEvaluations++; m_NumberOfHessianEvaluations++; // call the combined version if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(false,true,true,true,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(false,true,true,true,false, parameters, value, derivative, H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(false,true,true,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(false,true,true,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,false,true,true); this->InvokeEvent( me ); } /* * Get both the match Measure and theDerivative Measure */ template void AnytimeImageToImageMetric2 ::GetValueAndDerivativeAndHessian(const ParametersType & parameters, MeasureType & value, DerivativeType & derivative, HessianType & H, int channel) const { m_NumberOfEvaluations++; m_NumberOfValueEvaluations++; m_NumberOfDerivativeEvaluations++; m_NumberOfHessianEvaluations++; if(this->m_FixedImageMask) { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,true,true,true,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,true,true,true,false, parameters, value, derivative, H, channel ); } }else { if(this->m_MovingImageMask) { this->GetValueAndDerivativeAndHessian_calc(true,true,true,false,true, parameters, value, derivative, H, channel ); } else { this->GetValueAndDerivativeAndHessian_calc(true,true,true,false,false, parameters, value, derivative, H, channel ); } } MetricEvaluationEvent me(value,parameters,true,true,true); this->InvokeEvent( me ); } /* * Get both the match Measure and theDerivative Measure */ template void AnytimeImageToImageMetric2 ::GetValueAndDerivativeAndHessian_calc(bool calcvalue,bool calcderivative,bool calchessian, bool fixedmask,bool movingmask,const ParametersType & parameters, MeasureType & value, DerivativeType & derivative, HessianType & H, int channel) const { itkExceptionMacro("No punky, ya really have to override this one in the derived class. Do not pass go, do not collect $200"); } /*template template < bool calcvalue,bool calcderivative,bool calchessian, bool fixedmask,bool movingmask> void AnytimeImageToImageMetric2 ::GetValueAndDerivativeAndHessian_calc(const ParametersType & parameters, MeasureType & value, DerivativeType & derivative, HessianType & H, int channel) const { itkExceptionMacro("No punky, ya really have to override this one in the derived class. Do not pass go, do not collect $200"); } */ template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::ConvertDerivative( GradientCalculationMethod gradmethod, ParametersType& derivative, ParametersType& derivativeESM ) const{ if(gradmethod==Inverse) { if(!m_DPsiDPhiComputed) this->ComputeDpsidphi(); DerivativeType oldderivative=derivative; //std::cout<<"Old deriviative:"<m_NumberOfParameters;phi++) { derivative[phi]=0.0; for(unsigned int psi=0;psim_NumberOfParameters;psi++) { derivative[phi]+=this->dpsidphi[psi][phi]*oldderivative[psi]; } } } if(gradmethod==ESM) { if(!m_DPsiDPhiComputed) this->ComputeDpsidphi(); //DerivativeType oldderivative=derivativeESM; //std::cout<<"Old deriviative:"<m_NumberOfParameters;phi++) { // derivative[phi]=0.0; for(unsigned int psi=0;psim_NumberOfParameters;psi++) { derivative[phi]+=this->dpsidphi[psi][phi]*derivativeESM[psi]; } derivative[phi]/=2.0; } } if(gradmethod==InverseCompositional) { for(unsigned int phi=0;phim_NumberOfParameters;phi++) { derivative[phi]*=-1.0; } } } template < class TFixedImage, class TMovingImage > void AnytimeImageToImageMetric2 ::ConvertHessian( GradientCalculationMethod gradmethod, HessianType& Hessian ,HessianType& HessianESM) const{ /*if((gradmethod==Inverse||gradmethod==InverseCompositional)&&m_ICHessianComputed) { // if its been computed, copy it over Hessian=m_ICHessian; } else if((gradmethod==Inverse||gradmethod==InverseCompositional)&&!m_ICHessianComputed) { // if we get here, then we just computed it, so copy it into the cache m_ICHessian=Hessian; m_ICHessianComputed=true; } */ if(gradmethod==Inverse) { if(!m_DPsiDPhiComputed) this->ComputeDpsidphi(); HessianType oldHessian=Hessian; //std::cout<<"Old deriviative:"<m_NumberOfParameters;phi1++) { for(unsigned int phi2=0;phi2m_NumberOfParameters;phi2++) { Hessian[phi1][phi2]=0.0; for(unsigned int psi1=0;psi1m_NumberOfParameters;psi1++) { for(unsigned int psi2=0;psi2m_NumberOfParameters;psi2++) { Hessian[phi1][phi2]+= this->dpsidphi[psi1][phi1]*oldHessian[psi1][psi2]* this->dpsidphi[psi2][phi2]; } } } } } if(gradmethod==ESM) { if(!m_DPsiDPhiComputed) this->ComputeDpsidphi(); //HessianType oldHessian=Hessian; //std::cout<<"Old deriviative:"<m_NumberOfParameters;phi1++) { for(unsigned int phi2=0;phi2m_NumberOfParameters;phi2++) { // Hessian[phi1][phi2]=0.0; for(unsigned int psi1=0;psi1m_NumberOfParameters;psi1++) { for(unsigned int psi2=0;psi2m_NumberOfParameters;psi2++) { Hessian[phi1][phi2]+= this->dpsidphi[psi1][phi1]*HessianESM[psi1][psi2]* this->dpsidphi[psi2][phi2]; } } Hessian[phi1][phi2]/=2.0; } } } } template TransformBase::Pointer AnytimeImageToImageMetric2 ::GetTransformCopy(void) const { TransformBase::Pointer rv=dynamic_cast (this->m_Transform->CreateAnother().GetPointer()); rv->SetFixedParameters(this->m_Transform->GetFixedParameters()); //std::cout<<"Transform is: "<GetTransform()->GetNameOfClass()< void AnytimeImageToImageMetric2 ::ConvertDerivativeAndHessian( GradientCalculationMethod gradmethod, ParametersType& derivative,HessianType& Hessian, ParametersType& derivativeESM,HessianType& HessianESM ) const{ if(gradmethod==Inverse||gradmethod==ESM) { this->ComputeDpsidphi(); m_DPsiDPhiComputed=true; } this->ConvertDerivative(gradmethod,derivative,derivativeESM); this->ConvertHessian(gradmethod,Hessian,HessianESM); m_DPsiDPhiComputed=false; } } // end namespace itk #endif