1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkHistogramImageToImageMetric_h
19 #define itkHistogramImageToImageMetric_h
20 
21 #include "itkHistogram.h"
22 #include "itkImageToImageMetric.h"
23 
24 namespace itk
25 {
26 /** \class HistogramImageToImageMetric
27     \brief Computes similarity between two objects to be registered
28 
29   This class is templated over the type of the fixed and moving
30   images to be compared.
31 
32   The metric computes the similarity measure between pixels in the
33   moving image and pixels in the fixed image using a histogram.
34 
35   \ingroup RegistrationMetrics
36  * \ingroup ITKRegistrationCommon
37  */
38 template< typename TFixedImage, typename TMovingImage >
39 class ITK_TEMPLATE_EXPORT HistogramImageToImageMetric:
40   public ImageToImageMetric< TFixedImage, TMovingImage >
41 {
42 public:
43   ITK_DISALLOW_COPY_AND_ASSIGN(HistogramImageToImageMetric);
44 
45   /** Standard class type aliases. */
46   using Self = HistogramImageToImageMetric;
47   using Superclass = ImageToImageMetric< TFixedImage, TMovingImage >;
48   using Pointer = SmartPointer< Self >;
49   using ConstPointer = SmartPointer< const Self >;
50 
51   /** Run-time type information (and related methods). */
52   itkTypeMacro(HistogramImageToImageMetric, ImageToImageMetric);
53 
54   /** Types transferred from the base class */
55   using RealType = typename Superclass::RealType;
56   using TransformType = typename Superclass::TransformType;
57   using TransformPointer = typename Superclass::TransformPointer;
58   using TransformParametersType = typename Superclass::TransformParametersType;
59   using TransformJacobianType = typename Superclass::TransformJacobianType;
60   using GradientPixelType = typename Superclass::GradientPixelType;
61   using InputPointType = typename Superclass::InputPointType;
62   using OutputPointType = typename Superclass::OutputPointType;
63   using MeasureType = typename Superclass::MeasureType;
64   using DerivativeType = typename Superclass::DerivativeType;
65   using FixedImageType = typename Superclass::FixedImageType;
66   using FixedImagePixelType = typename Superclass::FixedImageType::PixelType;
67   using MovingImageType = typename Superclass::MovingImageType;
68   using MovingImagePixelType = typename Superclass::MovingImageType::PixelType;
69   using FixedImageConstPointerType = typename Superclass::FixedImageConstPointer;
70   using MovingImageConstPointerType = typename Superclass::MovingImageConstPointer;
71 
72   /** Typedefs for histogram. This should have been defined as
73       Histogram<RealType,2> but a bug in VC++7 produced an internal compiler
74       error with such declaration. */
75   using HistogramType = Statistics::Histogram< double >;
76 
77   using MeasurementVectorType = typename HistogramType::MeasurementVectorType;
78   using HistogramSizeType = typename HistogramType::SizeType;
79   using HistogramPointer = typename HistogramType::Pointer;
80 
81   /** Initializes the metric. */
82   void Initialize() override;
83 
84   /** Define the transform and thereby the parameter space of the metric
85    *   and the space of its derivatives */
86   void SetTransform(TransformType *transform) override;
87 
88   /** Sets the histogram size. Note this function must be called before
89       \c Initialize(). */
90   itkSetMacro(HistogramSize, HistogramSizeType);
91 
92   /** Gets the histogram size. */
93   itkGetConstReferenceMacro(HistogramSize, HistogramSizeType);
94 
95   /** Factor to increase the upper bound for the samples in the histogram.
96       Default value is 0.001 */
97   itkSetMacro(UpperBoundIncreaseFactor, double);
98   itkGetConstMacro(UpperBoundIncreaseFactor, double);
99 
100   /** The padding value. */
101   itkSetMacro(PaddingValue, FixedImagePixelType);
102 
103   /** Returns the padding value. */
104   itkGetConstReferenceMacro(PaddingValue, FixedImagePixelType);
105 
106   /** Return the joint histogram. This is updated during every call to the
107    *  GetValue() method. The histogram can for instance be used by
108    *  itk::HistogramToImageFilter to plot the joint histogram. */
109   itkGetConstReferenceMacro(Histogram, HistogramPointer);
110 
111   /** Set whether the padding value should be used to determine which pixels
112       should be ignored when calculating the similarity measure. Those pixels
113       in the fixed image which have the padding value will be ignored. */
114   itkSetMacro(UsePaddingValue, bool);
115   itkGetConstMacro(UsePaddingValue, bool);
116 
117   /** Sets the step length used to calculate the derivative. */
118   itkSetMacro(DerivativeStepLength, double);
119 
120   /** Returns the step length used to calculate the derivative. */
121   itkGetConstMacro(DerivativeStepLength, double);
122 
123   /** The scales type. */
124   using ScalesType = Array< double >;
125 
126   /** Sets the derivative step length scales. */
127   itkSetMacro(DerivativeStepLengthScales, ScalesType);
128 
129   /** Returns the derivate step length scales. */
130   itkGetConstReferenceMacro(DerivativeStepLengthScales, ScalesType);
131 
132   /**  Get the value for single valued optimizers. */
133   MeasureType GetValue(const TransformParametersType & parameters) const override;
134 
135   /** Get the derivatives of the match measure. */
136   void GetDerivative(const TransformParametersType & parameters,
137                      DerivativeType & derivative) const override;
138 
139   /**  Get value and derivatives for multiple valued optimizers. */
140   void GetValueAndDerivative(const TransformParametersType & parameters,
141                              MeasureType & Value,
142                              DerivativeType & Derivative) const override;
143 
144   /** Set the lower bounds of the intensities to be considered for computing
145     * the histogram. This option allows to focus the computation of the Metric in
146     * a particular range of intensities that correspond to features of interest. */
147   void SetLowerBound(const MeasurementVectorType & bound);
148 
149   /** Returns the current state of m_LowerBound. */
150   const MeasurementVectorType & GetLowerBound() const;
151 
152   /** Set the upper bounds of the intensities to be considered for computing
153     * the histogram. This option allows to focus the computation of the Metric in
154     * a particular range of intensities that correspond to features of interest.  */
155   void SetUpperBound(const MeasurementVectorType & bound);
156 
157   /** Returns the current state of m_UpperBound. */
158   const MeasurementVectorType & GetUpperBound() const;
159 
160 protected:
161   /** Constructor is protected to ensure that \c New() function is used to
162       create instances. */
163   HistogramImageToImageMetric();
164   ~HistogramImageToImageMetric() override = default;
165 
166   /** The histogram size. */
167   HistogramSizeType m_HistogramSize;
168   /** The lower bound for samples in the histogram. */
169   mutable MeasurementVectorType m_LowerBound;
170   /** The upper bound for samples in the histogram. */
171   mutable MeasurementVectorType m_UpperBound;
172   /** The increase in the upper bound. */
173   double m_UpperBoundIncreaseFactor;
174 
175   /** Boolean flag to indicate whether the user supplied lower bounds or
176     * whether they should be computed from the min of image intensities */
177   bool m_LowerBoundSetByUser;
178 
179   /** Boolean flag to indicate whether the user supplied upper bounds or
180     * whether they should be computed from the max of image intensities */
181   bool m_UpperBoundSetByUser;
182 
183   /** Computes the joint histogram from the transformation parameters
184       passed to the function. */
185   void ComputeHistogram(const TransformParametersType & parameters,
186                         HistogramType & histogram) const;
187 
188   /** Computes the joint histogram from the transformation parameters
189       passed to the function. */
190   void ComputeHistogram(const TransformParametersType & parameters,
191                         unsigned int parameter,
192                         double step,
193                         HistogramType & histogram) const;
194 
195   /** Copies a histogram. */
196   void CopyHistogram(HistogramType & target, HistogramType & source) const;
197 
198   /** Evaluates the similarity measure using the given histogram. All
199       subclasses must reimplement this method. */
200   virtual MeasureType EvaluateMeasure(HistogramType & histogram) const = 0;
201 
202   /** PrintSelf function */
203   void PrintSelf(std::ostream & os, Indent indent) const override;
204 
205 private:
206   /** The padding value. */
207   FixedImagePixelType m_PaddingValue;
208 
209   /** True if those pixels in the fixed image with the same value as the
210       padding value should be ignored when calculating the similarity
211       measure. */
212   bool m_UsePaddingValue;
213 
214   /** The step length used to calculate the derivative. */
215   double m_DerivativeStepLength;
216 
217   /** The derivative step length scales. */
218   ScalesType m_DerivativeStepLengthScales;
219 
220   /** Pointer to the joint histogram. This is updated during every call to
221    * GetValue() */
222   HistogramPointer m_Histogram;
223 };
224 } // end namespace itk
225 
226 #ifndef ITK_MANUAL_INSTANTIATION
227 #include "itkHistogramImageToImageMetric.hxx"
228 #endif
229 
230 #endif // itkHistogramImageToImageMetric_h
231