NormalizedIntensityCrossCorrelation.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2017 Imperial College London
5  * Copyright 2013-2017 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_NormalizedIntensityCrossCorrelation_H
21 #define MIRTK_NormalizedIntensityCrossCorrelation_H
22 
23 #include "mirtk/ImageSimilarity.h"
24 
25 #include "mirtk/Voxel.h"
26 #include "mirtk/GenericImage.h"
27 #include "mirtk/Vector3D.h"
28 #include "mirtk/Parallel.h"
29 
30 
31 namespace mirtk {
32 
33 
34 /**
35  * (Local) Normalized cross correlation image similarity measure
36  */
38 {
39  mirtkEnergyTermMacro(NormalizedIntensityCrossCorrelation, EM_NCC);
40 
41  // ---------------------------------------------------------------------------
42  // Types
43 public:
44 
45  typedef GradientImageType::VoxelType RealType;
48 
49  /// Enumeration of local window size units
50  enum Units { UNITS_Default, UNITS_MM, UNITS_Voxel };
51 
52 private:
53 
54  /// Enumeration of local window types
55  enum KernelType { CustomKernel /**< reserved */, BoxWindow, GaussianKernel };
56 
57  // ---------------------------------------------------------------------------
58  // Attributes
59 
60  /// Type of kernel for output in parameter file
61  mirtkAttributeMacro(enum KernelType, KernelType);
62 
63  /// Local window kernel in x dimension
64  /// \note The kernel image is deleted by the destructor of this class.
65  mirtkComponentMacro(KernelImage, KernelX);
66 
67  /// Local window kernel in y dimension
68  /// \note The kernel image is deleted by the destructor of this class.
69  mirtkComponentMacro(KernelImage, KernelY);
70 
71  /// Local window kernel in z dimension
72  /// \note The kernel image is deleted by the destructor of this class.
73  mirtkComponentMacro(KernelImage, KernelZ);
74 
75  /// Term A of normalized cross-correlation
76  mirtkComponentMacro(RealImage, A);
77 
78  /// Term B of normalized cross-correlation
79  mirtkComponentMacro(RealImage, B);
80 
81  /// Term C of normalized cross-correlation
82  mirtkComponentMacro(RealImage, C);
83 
84  /// Mean normalized source image
85  mirtkComponentMacro(RealImage, S);
86 
87  /// Mean normalized target image
88  mirtkComponentMacro(RealImage, T);
89 
90  /// Global inner products computed over whole image domain
91  double _GlobalA, _GlobalB, _GlobalC, _GlobalS, _GlobalT;
92 
93  /// Temporary image used by ComputeWeightedAverage and WriteDataSets
94  mutable RealImage _Temp;
95 
96  /// Size of box window or Full Width at Tenth Maximum (FWTM) of Gaussian kernel.
97  /// A negative value indicates voxel units, while a positive value corresponds
98  /// to world units (i.e., mm). When 0, the global normalized cross correlation
99  /// is computed instead of local cross correlation within a neighborhood.
100  mirtkPublicAttributeMacro(Vector3D<double>, NeighborhoodSize);
101 
102  /// Radius of local neighborhood window in number of voxels
103  mirtkReadOnlyAttributeMacro(Vector3D<int>, NeighborhoodRadius);
104 
105  /// Sum of local correlation coefficients / global normalized cross correlation
106  double _Sum;
107 
108  /// Size of overlap domain
109  int _N;
110 
111  // ---------------------------------------------------------------------------
112  // Construction/Destruction
113 
114  /// Create 1D Gaussian kernel with given standard deviation
115  static KernelImage *CreateGaussianKernel(double);
116 
117  /// Reset local window kernel
118  virtual void ClearKernel();
119 
120 public:
121 
122  /// Constructor
123  NormalizedIntensityCrossCorrelation(const char * = "");
124 
125  /// Copy constructor
127 
128  /// Assignment operator
130 
131  /// Destructor
133 
134  // ---------------------------------------------------------------------------
135  // Parameters
136 
137  /// Set kernel to a box window with given radius
138  virtual void SetKernelToBoxWindow(double, double = -1, double = -1, Units = UNITS_MM);
139 
140  /// Set kernel to a Gaussian with given standard deviation
141  virtual void SetKernelToGaussian(double, double = -1, double = -1, Units = UNITS_MM);
142 
143 protected:
144 
145  /// Set parameter value from string
146  virtual bool SetWithPrefix(const char *, const char *);
147 
148 public:
149 
150  // Import other overloads
152 
153  /// Get parameter key/value as string map
154  virtual ParameterList Parameter() const;
155 
156  // ---------------------------------------------------------------------------
157  // Initialization
158 
159  /// Initialize similarity measure once input and parameters have been set
160  virtual void Initialize();
161 
162  // ---------------------------------------------------------------------------
163  // Evaluation
164 
165  /// Update moving image and internal state of similarity measure
166  virtual void Update(bool = true);
167 
168  /// Exclude region from similarity evaluation
169  virtual void Exclude(const blocked_range3d<int> &);
170 
171  /// Include region in similarity evaluation
172  virtual void Include(const blocked_range3d<int> &);
173 
174 protected:
175 
176  /// Convolve image with separable window kernel
177  virtual void ComputeWeightedAverage(const blocked_range3d<int> &, RealImage *);
178 
179  /// Compute statistics of local intensity distribution
180  virtual void ComputeStatistics(const blocked_range3d<int> &,
181  const RegisteredImage *,
182  RealImage *, RealImage *);
183 
184  /// Evaluate similarity of images
185  virtual double Evaluate();
186 
187  /// Evaluate non-parametric similarity gradient w.r.t the given image
189 
190  // ---------------------------------------------------------------------------
191  // Debugging
192 public:
193 
194  /// Return unweighted and unnormalized raw energy term value
195  /// \remarks Use for progress reporting only.
196  virtual double RawValue(double) const;
197 
198  /// Print debug information
199  virtual void Print(Indent = 0) const;
200 
201  /// Write input of data fidelity term
202  virtual void WriteDataSets(const char *, const char *, bool = true) const;
203 
204 };
205 
206 
207 } // namespace mirtk
208 
209 #endif // MIRTK_NormalizedIntensityCrossCorrelation_H
virtual void SetKernelToBoxWindow(double, double=-1, double=-1, Units=UNITS_MM)
Set kernel to a box window with given radius.
NormalizedIntensityCrossCorrelation & operator=(const NormalizedIntensityCrossCorrelation &)
Assignment operator.
virtual void Initialize()
Initialize similarity measure once input and parameters have been set.
virtual void Exclude(const blocked_range3d< int > &)
Exclude region from similarity evaluation.
virtual bool NonParametricGradient(const RegisteredImage *, GradientImageType *)
Evaluate non-parametric similarity gradient w.r.t the given image.
virtual void Include(const blocked_range3d< int > &)
Include region in similarity evaluation.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
Definition: Object.h:38
virtual double Evaluate()
Evaluate similarity of images.
virtual void SetKernelToGaussian(double, double=-1, double=-1, Units=UNITS_MM)
Set kernel to a Gaussian with given standard deviation.
Definition: IOConfig.h:41
virtual ParameterList Parameter() const
Get parameter key/value as string map.
virtual void Update(bool=true)
Update moving image and internal state of similarity measure.
virtual void ComputeWeightedAverage(const blocked_range3d< int > &, RealImage *)
Convolve image with separable window kernel.
Normalized cross-correlation.
Definition: EnergyMeasure.h:50
Three-dimensional range.
Definition: Parallel.h:197
NormalizedIntensityCrossCorrelation(const char *="")
Constructor.
virtual void Print(Indent=0) const
Print debug information.
virtual bool SetWithPrefix(const char *, const char *)
Set parameter value from string.
virtual ParameterList Parameter() const
Get parameter key/value as string map.
virtual void WriteDataSets(const char *, const char *, bool=true) const
Write input of data fidelity term.
TVoxel VoxelType
Voxel type.
Definition: GenericImage.h:52
virtual void ComputeStatistics(const blocked_range3d< int > &, const RegisteredImage *, RealImage *, RealImage *)
Compute statistics of local intensity distribution.