OPAL
SSD.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <cassert>
13 #include <cstddef>
14 
30 template <class TDb>
31 class SSD {
32 public:
33  using ValueType = typename TDb::ImgPixelType;
34  using ImgType = typename TDb::ImgType;
35 
36 public:
37  // Constructors and destructors.
38 
39  SSD() = default;
40 
56  SSD(const TDb &db, size_t idx,
57  size_t ctrFixedX, size_t ctrFixedY, size_t ctrMovingX, size_t ctrMovingY,
58  size_t radius);
59 
60  ~SSD() = default;
61 
62 public:
63  // General-purpose methods.
64 
68  inline ValueType GetValue() const;
69 
73  inline operator ValueType() const;
74 
78  inline bool operator <(const SSD &other) const;
79 
80 public:
81  // Methods for efficient update.
82 
88  ValueType ShiftRight();
89 
95  ValueType ShiftLeft();
96 
102  ValueType ShiftUp();
103 
109  ValueType ShiftDown();
110 
116  ValueType ShiftImage(const ImgType *img);
117 
118 private:
119 
123  void CalculateValue();
124 
131  bool PatchInsideImage(const size_t x, const size_t y);
132 
133 private:
135  const ImgType *fixedImageIt;
136 
138  const ImgType *movingImageIt;
139 
141  size_t patchSide;
142 
144  size_t fixedTopLeftX;
145 
147  size_t fixedTopLeftY;
148 
150  size_t movingTopLeftX;
151 
153  size_t movingTopLeftY;
154 
156  ValueType value;
157 };
158 
159 
160 // ===== Implementation below =====
161 
162 template <class TDb>
163 SSD<TDb>::SSD(const TDb &db, size_t idx,
164  size_t ctrFixedX, size_t ctrFixedY,
165  size_t ctrMovingX, size_t ctrMovingY,
166  size_t radius)
167  : fixedImageIt(&db.GetImage(0))
168  , movingImageIt(&db.GetImage(idx))
169  , patchSide(2 * radius + 1)
170  , fixedTopLeftX(ctrFixedX - radius)
171  , fixedTopLeftY(ctrFixedY - radius)
172  , movingTopLeftX(ctrMovingX - radius)
173  , movingTopLeftY(ctrMovingY - radius)
174  , value(0)
175 {
176  assert(idx > 0 && "SSD between patches of 0-th image in database!");
177  assert(fixedImageIt && movingImageIt && "One of image pointers is null!");
178 
179  CalculateValue();
180 }
181 
182 
183 template <class TDb>
184 typename SSD<TDb>::ValueType SSD<TDb>::GetValue() const
185 {
186  return value;
187 }
188 
189 
190 template <class TDb>
191 SSD<TDb>::operator ValueType() const
192 {
193  return GetValue();
194 }
195 
196 
197 template <class TDb>
198 bool SSD<TDb>::operator <(const SSD &other) const
199 {
200  return value < other.value;
201 }
202 
203 
204 template <class TDb>
205 typename SSD<TDb>::ValueType SSD<TDb>::ShiftRight()
206 {
207  for (size_t dy = 0; dy < patchSide; ++dy) {
208  // Step to new place (1 px right the right side), add it.
209  ValueType diff1 =
210  (*fixedImageIt)(fixedTopLeftY + dy, fixedTopLeftX + patchSide) -
211  (*movingImageIt)(movingTopLeftY + dy, movingTopLeftX + patchSide);
212 
213  // Step from old place, subtract it (the left side).
214  ValueType diff2 =
215  (*fixedImageIt)(fixedTopLeftY + dy, fixedTopLeftX) -
216  (*movingImageIt)(movingTopLeftY + dy, movingTopLeftX);
217 
218  value += (diff1 * diff1 - diff2 * diff2);
219  }
220  ++fixedTopLeftX;
221  ++movingTopLeftX;
222 
223  return value;
224 }
225 
226 
227 template <class TDb>
228 typename SSD<TDb>::ValueType SSD<TDb>::ShiftLeft()
229 {
230  for (size_t dy = 0; dy < patchSide; ++dy) {
231  // Step to new place (1 px left the left side), add it.
232  ValueType diff1 =
233  (*fixedImageIt)(fixedTopLeftY + dy, fixedTopLeftX - 1) -
234  (*movingImageIt)(movingTopLeftY + dy, movingTopLeftX - 1);
235 
236  // Step from old place, subtract it (the right side).
237  ValueType diff2 =
238  (*fixedImageIt)(fixedTopLeftY + dy, fixedTopLeftX + patchSide - 1) -
239  (*movingImageIt)(movingTopLeftY + dy, movingTopLeftX + patchSide - 1);
240 
241  value += (diff1 * diff1 - diff2 * diff2);
242  }
243  --fixedTopLeftX;
244  --movingTopLeftX;
245 
246  return value;
247 }
248 
249 
250 template <class TDb>
251 typename SSD<TDb>::ValueType SSD<TDb>::ShiftUp()
252 {
253  for (size_t dx = 0; dx < patchSide; ++dx) {
254  // Step to new place (1 px up the upper side), add it.
255  ValueType diff1 =
256  (*fixedImageIt)(fixedTopLeftY - 1, fixedTopLeftX + dx) -
257  (*movingImageIt)(movingTopLeftY - 1, movingTopLeftX + dx);
258 
259  // Step from old place (the lower side), subtract it.
260  ValueType diff2 =
261  (*fixedImageIt)(fixedTopLeftY + patchSide - 1, fixedTopLeftX + dx) -
262  (*movingImageIt)(movingTopLeftY + patchSide - 1, movingTopLeftX + dx);
263 
264  value += (diff1 * diff1 - diff2 * diff2);
265  }
266  --fixedTopLeftY;
267  --movingTopLeftY;
268 
269  return value;
270 }
271 
272 
273 template <class TDb>
274 typename SSD<TDb>::ValueType SSD<TDb>::ShiftDown()
275 {
276  for (size_t dx = 0; dx < patchSide; ++dx) {
277  // Step to new place (1 px down the lower side), add it.
278  ValueType diff1 =
279  (*fixedImageIt)(fixedTopLeftY + patchSide, fixedTopLeftX + dx) -
280  (*movingImageIt)(movingTopLeftY + patchSide, movingTopLeftX + dx);
281 
282  // Step from old place (upper side), subtract it.
283  ValueType diff2 =
284  (*fixedImageIt)(fixedTopLeftY, fixedTopLeftX + dx) -
285  (*movingImageIt)(movingTopLeftY, movingTopLeftX + dx);
286 
287  value += (diff1 * diff1 - diff2 * diff2);
288  }
289  ++fixedTopLeftY;
290  ++movingTopLeftY;
291 
292  return value;
293 }
294 
295 
296 template <class TDb>
297 typename SSD<TDb>::ValueType SSD<TDb>::ShiftImage(const typename SSD<TDb>::ImgType *img)
298 {
299  assert(img && "New image pointer is null!");
300 
301  movingImageIt = img;
302  CalculateValue();
303 
304  return value;
305 }
306 
307 
308 template <class TDb>
310 {
311  value = 0;
312  for (size_t dy = 0; dy < patchSide; ++dy)
313  for (size_t dx = 0; dx < patchSide; ++dx) {
314  ValueType diff = (*fixedImageIt)(fixedTopLeftY + dy, fixedTopLeftX + dx) -
315  (*movingImageIt)(movingTopLeftY + dy, movingTopLeftX + dx);
316  value += diff * diff;
317  }
318 }
319 
ValueType ShiftLeft()
Same moving image, (x-1,y) on both fixed and moving images.
Definition: SSD.h:228
bool operator<(const SSD &other) const
Definition: SSD.h:198
ValueType ShiftRight()
Same moving image, (x+1,y) on both fixed and moving images.
Definition: SSD.h:205
ValueType ShiftUp()
Same moving image, (x,y-1) on both fixed and moving images.
Definition: SSD.h:251
ValueType ShiftImage(const ImgType *img)
Same (x,y) coordinates on image img.
Definition: SSD.h:297
ValueType GetValue() const
Definition: SSD.h:184
Class responsible for SSD calculation between patches of images.
Definition: SSD.h:31
ValueType ShiftDown()
Same moving image, (x,y+1) on both fixed and moving images.
Definition: SSD.h:274