/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: itkAnytimeImageToImageMetric.h,v $ Language: C++ Date: $Date: 2007/09/21 13:43:14 $ Version: $Revision: 1.13 $ 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 __itkAnytimeImageToImageMetric_h #define __itkAnytimeImageToImageMetric_h #include "itkImageToImageMetric.h" #include "itkAnytimeImageToImageMetricBase.h" #include "itkImageRandomNonRepeatingConstIteratorWithIndex.h" #include "../IO/itkImageFileWriter2.h" #include "itkNumericTraits.h" //#include "itkImageRandomConstIteratorWithIndex.h" //#include "itkImageRegionConstIterator.h" //#include "itkImageRegionIterator.h" //#include "itkImageIterator.h" namespace itk { /** \class AnytimeImageToImageMetric * \brief Computes similarity between regions of two images. * * This Class is templated over the type of the two input images. * It expects a Transform and an Interpolator to be plugged in. * This particular class is the base class for a hierarchy of * similarity metrics. * * This class computes a value that measures the similarity * between the Fixed image and the transformed Moving image. * The Interpolator is used to compute intensity values on * non-grid positions resulting from mapping points through * the Transform. * * * \ingroup RegistrationMetrics * */ template class ITK_EXPORT AnytimeImageToImageMetric : virtual public AnytimeImageToImageMetricBase, public ImageToImageMetric { public: /** Standard class typedefs. */ typedef AnytimeImageToImageMetric Self; typedef ImageToImageMetric Superclass; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; /** Type used for representing point components */ typedef typename Superclass::ParametersValueType CoordinateRepresentationType; typedef itk::Image PriorityImageType; /** Run-time type information (and related methods). */ itkTypeMacro(AnytimeImageToImageMetric, ImageToImageMetric); unsigned int m_NumberOfChannels; mutable unsigned long m_TotalPixelsProcessed; unsigned long m_NumberOfSpatialSamples; unsigned long m_NumberOfAvailableSamples; int m_RandomSeed; PerformanceProfileTable m_PerformanceProfile; PriorityImageType * m_PriorityImage; itkSetObjectMacro(PriorityImage,PriorityImageType); itkSetMacro(NumberOfChannels,unsigned int); itkSetMacro(TotalPixelsProcessed,unsigned long); itkGetMacro(TotalPixelsProcessed,unsigned long); itkGetMacro(NumberOfAvailableSamples,unsigned long); itkSetMacro(RandomSeed,int); itkGetConstReferenceMacro(NumberOfChannels,unsigned int); /** Number of spatial samples to used to compute metric */ itkSetClampMacro( NumberOfSpatialSamples, unsigned long, 1, NumericTraits::max() ); itkGetConstMacro( NumberOfSpatialSamples, unsigned long); typedef typename Superclass::FixedImageType FixedImageType; typedef typename Superclass::MovingImageType MovingImageType; typedef typename Superclass::TransformType TransformType; typedef typename Superclass::TransformPointer TransformPointer; typedef typename Superclass::InputPointType InputPointType; typedef typename Superclass::OutputPointType OutputPointType; typedef typename Superclass::TransformParametersType TransformParametersType; typedef typename Superclass::TransformJacobianType TransformJacobianType; typedef typename Superclass::InterpolatorType InterpolatorType; typedef typename Superclass::RealType RealType; typedef typename Superclass::GradientImageType GradientImageType; typedef typename Superclass::GradientImagePointer GradientImagePointer; typedef typename Superclass::GradientImageFilterType GradientImageFilterType; typedef typename Superclass::GradientImageFilterPointer GradientImageFilterPointer; typedef typename Superclass::InterpolatorPointer InterpolatorPointer; typedef typename Superclass::FixedImageMaskType FixedImageMaskType; typedef typename Superclass::FixedImageMaskPointer FixedImageMaskPointer; typedef typename Superclass::MovingImageMaskType MovingImageMaskType; typedef typename Superclass::MovingImageMaskPointer MovingImageMaskPointer; typedef typename Superclass::MeasureType MeasureType; typedef typename Superclass::DerivativeType DerivativeType; typedef typename Superclass::ParametersType ParametersType; /** Index and Point typedef support. */ typedef typename FixedImageType::IndexType FixedImageIndexType; typedef typename FixedImageType::PixelType FixedImagePixelType; typedef typename FixedImageIndexType::IndexValueType FixedImageIndexValueType; typedef typename MovingImageType::IndexType MovingImageIndexType; typedef typename TransformType::InputPointType FixedImagePointType; typedef typename TransformType::OutputPointType MovingImagePointType; typedef ImageRandomNonRepeatingConstIteratorWithIndex RandomIterator; typedef itk::Image< unsigned char, TFixedImage::ImageDimension > OutputPixelImageType; typedef itk::ImageFileWriter2 PixelImageWriterType; public: double IncrementComputationLevel(const double percent,const int channel); void SetComputationLevel(const double percent,const int channel) const; double GetComputationLevel(const int channel) const; void ResetComputation (const int channel) const; void SetChannel (const int channel); void LoadPerformanceProfile (const char* filename ) {m_PerformanceProfile.load(filename);} void WritePixelsUsed (const char* filename ); double GetCurrentAccuracy (const int channel, double feedback ) {return m_PerformanceProfile.getCurrentAccuracy (m_ComputationLevel[channel], feedback);} // FIXME - should do bounds checking on channel double EstimateCompletionRequired (double feedback, double requiredAccuracy ) {return m_PerformanceProfile.estimateCompletionRequired (feedback, requiredAccuracy);} MeasureType GetValue( const ParametersType& parameters ) const; MeasureType GetValue( const ParametersType& parameters, const int channel, const double level ) const; MeasureType GetValue( const ParametersType& parameters, const double level ) const; virtual MeasureType GetValue( const ParametersType& parameters, const int channel ) const {return 0.0;}; /** Get the derivatives of the match measure. */ virtual void GetDerivative( const TransformParametersType & parameters, DerivativeType & derivative, int channel ) const {}; /** Get value and derivatives for multiple valued optimizers. */ virtual void GetValueAndDerivative( const TransformParametersType & parameters, MeasureType& Value, DerivativeType& Derivative, int channel ) const {}; /** Get the derivatives of the match measure. */ void GetDerivative( const TransformParametersType & parameters, DerivativeType & derivative,int channel, double level ) const; /** Get value and derivatives for multiple valued optimizers. */ void GetValueAndDerivative( const TransformParametersType & parameters, MeasureType& Value, DerivativeType& Derivative, int channel, double level ) const; /** Get the derivatives of the match measure. */ void GetDerivative( const TransformParametersType & parameters, DerivativeType & derivative, double level ) const; /** Get value and derivatives for multiple valued optimizers. */ void GetValueAndDerivative( const TransformParametersType & parameters, MeasureType& Value, DerivativeType& Derivative, double level ) const; void GetDerivative( const TransformParametersType & parameters, DerivativeType & derivative) const; /** Get value and derivatives for multiple valued optimizers. */ void GetValueAndDerivative( const TransformParametersType & parameters, MeasureType& Value, DerivativeType& Derivative) const; void Initialize(void) throw ( ExceptionObject ); protected: AnytimeImageToImageMetric(){m_PriorityImage=NULL;m_RandomSeed=0;m_TotalPixelsProcessed=0;}; virtual ~AnytimeImageToImageMetric() {}; /** * A fixed image spatial sample consists of the fixed domain point * and the fixed image value at that point. */ class FixedImageSpatialSample { public: FixedImageSpatialSample():FixedImageValue(NumericTraits::Zero) { FixedImagePointValue.Fill(0.0); // FixedImageJacobian.Fill(0.0); FixedImageIndex.Fill(0); //FixedImageParameterGradient.Fill(0.0); } ~FixedImageSpatialSample() {}; TransformParametersType FixedImageParameterGradient; //typename TransformType::JacobianType FixedImageJacobian; FixedImageIndexType FixedImageIndex; FixedImagePointType FixedImagePointValue; FixedImagePixelType FixedImageValue; }; /** FixedImageSpatialSample typedef support. */ typedef std::vector FixedImageSpatialSampleContainer; /** Container to store a set of points and fixed image values. */ FixedImageSpatialSampleContainer m_FixedImageSamples; /** Vars which control the computation **/ mutable double m_ComputationLevel[NUMCHANNELS]; mutable MeasureType m_ValueCache[NUMCHANNELS]; mutable DerivativeType* m_DerivativeCache[NUMCHANNELS]; mutable int m_nFixedImageSamples[NUMCHANNELS]; mutable int m_nSamples[NUMCHANNELS]; /** Vars which control the computation **/ mutable typename FixedImageSpatialSampleContainer::const_iterator m_Index[NUMCHANNELS]; /** Cache the parameters **/ mutable ParametersType * m_CachedParameters[NUMCHANNELS]; unsigned long m_NumberOfParameters; mutable bool m_ComputeDerivative; mutable bool m_ComputeValue; /** Uniformly select a sample set from the fixed image domain. */ virtual void SampleFixedImageDomain( FixedImageSpatialSampleContainer& samples); int m_CurrentChannel; private: AnytimeImageToImageMetric(const Self&) {}; //purposely not implemented void operator=(const Self&); //purposely not implemented }; } // end namespace itk #ifndef ITK_MANUAL_INSTANTIATION #include "itkAnytimeImageToImageMetric.txx" #endif #endif