aimstil  5.0.5
NormalizedCorrelation.h
Go to the documentation of this file.
1 #ifndef TIL_NORMALIZEDCORRELATION_H
2 #define TIL_NORMALIZEDCORRELATION_H
3 
4 
5 // Standard library includes
6 
7 #include <math.h>
8 #include <limits>
9 
10 
11 // Local includes
12 
13 #include "til/til_common.h"
14 
15 #include "til/imageBasicStats.h"
16 
17 
18 
19 // Namespace
20 
21 namespace til {
22 
23 
24 
25 typedef double t_corprec;
26 
27 // returns the normalized correlation between image 1 and image 2
28 // im1 and im2 should have the same size
29 
30 
31 template <class TImage1, class TImage2>
33 {
34  private:
35 
36  TImage1 m_im1;
37  TImage2 m_im2;
38 
39  t_corprec m_meanIm1;
40  t_corprec m_meanIm2;
41 
42  t_corprec m_stdIm1;
43  t_corprec m_stdIm2;
44 
45 
46  public:
47 
48  // constructors and destructors
49 
51  {
52  m_meanIm1 = m_meanIm2 = t_corprec(0);
53  m_stdIm1 = m_stdIm2 = t_corprec(0);
54  };
55 
56  virtual ~NormalizedCorrelation() {};
57 
58 
59  // Set up parameters
60 
61  void setFirstImage(TImage1 &im1)
62  {
63  allocationCheck(im1);
64 
65  if (m_im1 != im1)
66  {
67  m_im1 = im1;
68  m_meanIm1 = (t_corprec) mean(m_im1);
69  m_stdIm1 = (t_corprec) ::sqrt(var(m_im1));
70  }
71  }
72 
73  void setSecondImage(TImage2 &im2)
74  {
75  allocationCheck(im2);
76 
77  if (m_im2 != im2)
78  {
79  m_im2 = im2;
80  m_meanIm2 = (t_corprec) mean(m_im2);
81  m_stdIm2 = (t_corprec) ::sqrt(var(m_im2));
82  }
83  }
84 
85  TImage1 & getFirstImage()
86  {
87  return m_im1;
88  }
89 
90  TImage2 & getSecondImage()
91  {
92  return m_im2;
93  }
94 
95  void setImages(TImage1 &im1, TImage2 &im2)
96  {
97  allocationCheck(im1);
98  allocationCheck(im2);
99  similarityCheck(im1,im2);
100 
101  this->setFirstImage(im1);
102  this->setSecondImage(im2);
103  }
104 
105  t_corprec compute();
106 };
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
139 //
140 // CODE
141 //
143 
144 
145 template <class TImage1, class TImage2>
147 {
148 
149  const t_corprec EPSILON = 4*std::numeric_limits<t_corprec>::epsilon() ;
150 
151 
152  // If one of the standard deviation is too small
153  // we will not be able to normalize and have to make
154  // some decisions
155 
156  if (m_stdIm1 <= EPSILON || m_stdIm2 <= EPSILON)
157  {
158  if (m_stdIm1 <= EPSILON && m_stdIm2 <= EPSILON)
159  {
160  // If both images are very uniform,
161  // we throw an exception
162  // (chosing a numerical value to return is quite
163  // hard in that case)
164 
165  throw std::runtime_error("Division by zero");
166  }
167 
168  // otherwise, we return 0
169  // This is not mathematically correct but works
170  // for most applications.
171  // TODO: maybe actually throwing a special exception
172  // is better, and let the user decide if it wants
173  // a zero or not.
174 
175  return t_corprec(0.0);
176  }
177 
178 
179  allocationCheck(m_im1);
180  allocationCheck(m_im2);
181  similarityCheck(m_im1, m_im2);
182 
183  t_corprec res = (t_corprec) 0;
184 
185  typename Iterator<TImage1>::ConstLinear i1(m_im1);
186  typename Iterator<TImage2>::ConstLinear i2(m_im2);
187 
188 
189  for (; !i1.isAtEnd(); ++i1, ++i2)
190  {
191  res += t_corprec(*i1 - m_meanIm1) * (*i2 - m_meanIm2);
192  }
193 
194 
195 
196  return res / (m_im1.size() * m_stdIm1 * m_stdIm2);
197 
198 }
199 
200 
201 } // namespace
202 
203 #endif
204 
A trait class to assign iterators to image types.
void setImages(TImage1 &im1, TImage2 &im2)
void sqrt(const TImage &in, TImage &out)
Definition: imageArith.h:326
boost::enable_if< is_Image< TImage >, t_bsprec >::type var(const TImage &im, t_bsprec meanIm)
Returns the variance of the intensities of the input image (Normalization by size) ...
void similarityCheck(const TImage1 &im1, const TImage2 &im2)
Check whether both images are allocated and have the same size and voxel size.
Definition: imageTools.h:75
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
General macros, definitions and functions.
INLINE void allocationCheck(const TImage &im)
Check whether smart pointer and image are allocated.
Definition: imageTools.h:42
t_bsprec mean(const TImage &im)
Returns the mean of the intensities of the input image.
double t_corprec