[go: up one dir, main page]

Menu

[r999]: / libmona / libmona / costFkt.hh  Maximize  Restore  History

Download this file

322 lines (241 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// $Id: costFkt.hh 887 2006-03-01 12:22:14Z write1 $
/*! \brief Base class for different cost functions
This contains basic cost functions to image registrtion.
\todo move test- funtions to ../test
\file costFkt.hh
\author Gert Wollny <wollny@cbs.mpg.de>
*/
#ifndef __MONA_COSTFKT_HH
#define __MONA_COSTFKT_HH 1
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
//#pragma interface
#include <vector>
#include <libmona/monaUtils.hh>
#include <libmona/3DImage.hh>
namespace mona {
class CCostFunction {
public:
/** Construct the cost function object
*/
CCostFunction();
/// ensure a virtual destructor
virtual ~CCostFunction();
/** \returns current value of cost function
\remark this is a pure virtual function
*/
double get_cost() const;
private:
virtual double evaluate_cost() const = 0;
mutable TCachedValue<double> value;
};
/** An class to offer a runtime polymorphism to the cost function evaluation */
class TCostEvaluator {
public:
/** makes sure the destructor is virtual */
virtual ~TCostEvaluator();
/** updates toe cost based on given intensity values (pure virtual)
\param src_val intensity value of source image
\param ref_val intensity value of reference image
*/
virtual void accumulate(double src_val, double ref_val) = 0;
/** \returns the accumulated value of the cost function */
virtual double get_value() const = 0;
virtual void clear() = 0;
};
/** a class for the Sum of Squared Differences (SSD) cost function */
class CSSDCostEvaluator: public TCostEvaluator {
public:
/// constructor initializes the cost with zero
CSSDCostEvaluator();
/** updates the cost based on given intensity values
\param src_val intensity value of source image
\param ref_val intensity value of reference image
*/
virtual void accumulate(double src_val, double ref_val);
/** \returns the accumulated value of the cost function */
virtual double get_value() const;
virtual void clear();
#ifndef NDEBUG
static bool test();
#endif
private:
double _M_cost;
};
/** Class of a correlation coefficient (CC) based cost function evaluator */
class CCCCostEvaluator: public TCostEvaluator {
public:
/** Initializes the evaluator
*/
CCCCostEvaluator();
/** updates the cost based on given intensity values
\param src_val intensity value of source image
\param ref_val intensity value of reference image
*/
virtual void accumulate(double src_val, double ref_val);
/** \returns the accumulated value of the cost function */
virtual double get_value() const;
virtual void clear();
#ifndef NDEBUG
static bool test();
#endif
private:
double _M_sumS;
double _M_sumR;
double _M_sumS2;
double _M_sumR2;
double _M_sumDP;
int _M_n;
};
class CNMICostEvaluator: public TCostEvaluator {
public:
/** Initializes the evaluator
*/
CNMICostEvaluator(double maxi, int hsize);
/** updates the cost based on given intensity values
\param src_val intensity value of source image
\param ref_val intensity value of reference image
*/
virtual void accumulate(double src_val, double ref_val);
/** \returns the accumulated value of the cost function */
virtual double get_value() const;
virtual void clear();
#ifndef NDEBUG
static bool test();
#endif
private:
CNMICostEvaluator(const CNMICostEvaluator& org);
unsigned int _M_n;
unsigned int _M_hsize;
double _M_factor;
typedef std::vector<unsigned long> CHistogram;
CHistogram _M_shisto;
CHistogram _M_rhisto;
CHistogram _M_xhisto;
};
/** Base class for 3D image registration cost functions */
template <class Transformation, class Image>
class CImageRegCostFunction: public CCostFunction {
public:
/** constructor
\param src the source image
\param ref the refercnec image
\param t the initial transformation
\retval cev provides the value of the cost function
\param mask provides a mask of the relevant image data
*/
CImageRegCostFunction(const Image& src, const Image& ref,
const Transformation& t, TCostEvaluator& cev,
const C3DUBImage *mask = NULL);
/** set a new transformation
\param t the new transformation
*/
void set_transformation(const Transformation& t);
private:
virtual double evaluate_cost() const;
class TTransformationWrap {
friend class CImageRegCostFunction;
const Transformation& _M_t;
TTransformationWrap(const Transformation& t):
_M_t(t)
{
}
typedef typename Transformation::const_iterator const_iterator;
const_iterator begin()const {
return _M_t.begin();
}
};
const Image& _M_src;
const Image& _M_ref;
TCostEvaluator& _M_cev;
auto_ptr<TTransformationWrap> _M_trans;
const C3DUBImage *_M_mask;
};
// implementation of templates
template <class Transformation, class Image>
CImageRegCostFunction<Transformation,Image>::CImageRegCostFunction(const Image& src, const Image& ref,
const Transformation& t,
TCostEvaluator& cev,
const C3DUBImage *mask):
_M_src(src),
_M_ref(ref),
_M_cev(cev),
_M_trans(new TTransformationWrap(src,t)),
_M_mask(mask)
{
assert(_M_src.get_size() == _M_ref.get_size());
if (_M_mask) {
assert(_M_src.get_size() == this->_M_mask.get_size());
}
}
template <class Transformation, class Image>
double CImageRegCostFunction<Transformation,Image>::evaluate_cost() const
{
C3DBounds l;
_M_cev.clear();
typename Image::const_iterator i_ref = _M_ref.begin();
typename Transformation::const_iterator i_trans = _M_trans.begin();
if (_M_mask) {
C3DUBImage::const_iterator i_mask = this->_M_mask.begin();
for(l.z = 0; l.z < this->_M_mask.get_size().z; ++l.z)
for(l.y = 0; l.y < this->_M_mask.get_size().y; ++l.y)
for(l.x = 0; l.x < this->_M_mask.get_size().x; ++l.x, ++i_ref, ++i_mask, ++_M_trans)
if (*i_mask)
_M_cev.accumulate((*_M_src)(*i_trans),*i_ref);
} else {
for(l.z = 0; l.z < _M_ref.get_size().z; ++l.z)
for(l.y = 0; l.y < _M_ref.get_size().y; ++l.y)
for(l.x = 0; l.x < _M_ref.get_size().x; ++l.x, ++i_ref,++_M_trans)
_M_cev.accumulate((*_M_src)(*i_trans),*i_ref);
}
return _M_cev.get_value();
}
template <class Transformation, class Image>
void CImageRegCostFunction<Transformation,Image>::set_transformation(const Transformation& t)
{
_M_trans.reset(new TTransformationWrap(t));
value.invalidate();
}
} // namespace mona
#endif
/* CVS LOG
$Log$
Revision 1.7 2005/06/29 13:22:23 wollny
switch to version 0.7
Revision 1.1.1.1 2005/03/17 13:44:20 gerddie
initial import
Revision 1.6 2004/07/13 09:16:16 wollny
g++ 3.4 compile
Revision 1.5 2004/06/03 09:57:32 wollny
Changed (hopefully) all instancable class names to Cxxxxx
Revision 1.4 2004/03/16 18:56:37 tittge
making release 0.3.1 (starting to divide algorithms from data classifiers)
Revision 1.3 2004/03/05 10:30:38 tittge
more plugin functionality
Revision 1.2 2004/02/20 08:57:44 tittge
add version tracking, improve iterator performance
Revision 1.1 2004/02/13 08:01:13 tittge
set up some test
*/