HistogramImageSimilarity.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_HistogramImageSimilarity_H
21 #define MIRTK_HistogramImageSimilarity_H
22 
23 #include "mirtk/ImageSimilarity.h"
24 
25 #include "mirtk/Math.h"
26 #include "mirtk/Histogram2D.h"
27 #include "mirtk/Parallel.h"
28 
29 
30 namespace mirtk {
31 
32 
33 /**
34  * Base class for probabilistic image similarity measures
35  *
36  * Subclasses of this intensity-based image similarity measure compute similarity
37  * from the joint and marginal probabilities of the intensities in the images.
38  * An estimate of the probabilities is obtained using a joint histogram and
39  * cubic B-spline Parzen Windows for a continuous representation.
40  */
42 {
43  mirtkAbstractMacro(HistogramImageSimilarity);
44 
45  // ---------------------------------------------------------------------------
46  // Types
47 public:
48 
49  /// Type of joint histogram bin values
50  #if MIRKT_USE_FLOAT_BY_DEFAULT
51  typedef float BinType;
52  #else
53  typedef double BinType;
54  #endif
55 
56  /// Type of joint histogram
58 
59  /// Get default number of bins for given input image
60  ///
61  /// \param[in] image Intensity image.
62  /// \param[in] min_intensity Pre-computed minimum intensity value.
63  /// \param[in] max_intensity Pre-computed maximum intensity value.
64  ///
65  /// \returns Number of histogram bins.
66  static int DefaultNumberOfBins(const BaseImage *image,
67  double min_intensity = NaN,
68  double max_intensity = NaN);
69 
70  // ---------------------------------------------------------------------------
71  // Attributes
72 
73  /// Joint histogram of raw intensity samples (no Parzen window function)
74  ///
75  /// This class allocates a joint histogram instance and takes care of updating
76  /// it upon each Update call. When an external _Samples histogram is provided,
77  /// however, the owner of this external histogram is reponsible for updating
78  /// and this histogram before the Update of this image similarity.
79  mirtkLooseComponentMacro(JointHistogramType, Samples);
80 
81  /// Joint histogram of image intensities (cubic B-spline Parzen windows)
82  mirtkAttributeMacro(JointHistogramType, Histogram);
83 
84  /// Whether to use a Parzen window function
85  mirtkPublicAttributeMacro(bool, UseParzenWindow);
86 
87  /// Whether to pad histogram when performing Parzen window estimation
88  mirtkPublicAttributeMacro(bool, PadHistogram);
89 
90  /// Number of histogram bins for target image intensities
91  mirtkPublicAttributeMacro(int, NumberOfTargetBins);
92 
93  /// Number of histogram bins for source image intensities
94  mirtkPublicAttributeMacro(int, NumberOfSourceBins);
95 
96  /// Copy attributes of this class from another instance
98 
99  // ---------------------------------------------------------------------------
100  // Construction/Destruction
101 protected:
102 
103  /// Constructor
104  HistogramImageSimilarity(const char * = "", double = 1.0);
105 
106  /// Copy constructor
108 
109  /// Assignment operator
111 
112  /// Destructor
113  virtual ~HistogramImageSimilarity();
114 
115  // ---------------------------------------------------------------------------
116  // Parameters
117 protected:
118 
119  /// Set parameter value from string
120  virtual bool SetWithPrefix(const char *, const char *);
121 
122 public:
123 
124  // Do not hide other overload
126 
127  /// Get parameter key/value as string map
128  virtual ParameterList Parameter() const;
129 
130  // ---------------------------------------------------------------------------
131  // Initialization
132 
133  /// Initialize similarity measure once input and parameters have been set
134  virtual void Initialize();
135 
136  // ---------------------------------------------------------------------------
137  // Evaluation
138 
139  /// Update moving image and internal state of similarity measure
140  virtual void Update(bool = true);
141 
142  /// Exclude region from similarity evaluation
143  ///
144  /// Called by ApproximateGradient \b before the registered image region of
145  /// the transformed image is updated.
146  virtual void Exclude(const blocked_range3d<int> &);
147 
148  /// Include region in similarity evaluation
149  ///
150  /// Called by ApproximateGradient \b after the registered image region of
151  /// the transformed image is updated.
152  virtual void Include(const blocked_range3d<int> &);
153 
154 protected:
155 
156  /// Update estimated joint histogram after raw samples histogram has changed
157  virtual void UpdateHistogram();
158 
159  // ---------------------------------------------------------------------------
160  // Debugging
161 
162 public:
163 
164  /// Print debug information
165  virtual void Print(Indent = 0) const;
166 
167  /// Write input of data fidelity term
168  virtual void WriteDataSets(const char *, const char *, bool = true) const;
169 
170 };
171 
172 
173 } // namespace mirtk
174 
175 #endif // MIRTK_HistogramImageSimilarity_H
HistogramImageSimilarity & operator=(const HistogramImageSimilarity &)
Assignment operator.
virtual ~HistogramImageSimilarity()
Destructor.
virtual void UpdateHistogram()
Update estimated joint histogram after raw samples histogram has changed.
virtual ParameterList Parameter() const
Get parameter key/value as string map.
Histogram2D< BinType > JointHistogramType
Type of joint histogram.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
Definition: Object.h:38
virtual void Initialize()
Initialize similarity measure once input and parameters have been set.
static int DefaultNumberOfBins(const BaseImage *image, double min_intensity=NaN, double max_intensity=NaN)
double BinType
Type of joint histogram bin values.
virtual void WriteDataSets(const char *, const char *, bool=true) const
Write input of data fidelity term.
mirtkPublicAttributeMacro(bool, UseParzenWindow)
Whether to use a Parzen window function.
Definition: IOConfig.h:41
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 Include(const blocked_range3d< int > &)
Three-dimensional range.
Definition: Parallel.h:197
mirtkLooseComponentMacro(JointHistogramType, Samples)
virtual void Exclude(const blocked_range3d< int > &)
mirtkAttributeMacro(JointHistogramType, Histogram)
Joint histogram of image intensities (cubic B-spline Parzen windows)
void CopyAttributes(const HistogramImageSimilarity &)
Copy attributes of this class from another instance.
virtual void Print(Indent=0) const
Print debug information.
HistogramImageSimilarity(const char *="", double=1.0)
Constructor.
virtual void Update(bool=true)
Update moving image and internal state of similarity measure.