[go: up one dir, main page]

Menu

[r991]: / libmona / libmona / filter.hh  Maximize  Restore  History

Download this file

386 lines (294 with data), 9.8 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* -*- 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: filter.hh 754 2005-11-25 12:09:56Z write1 $
/*! \brief Declaration of a general filter class.
Decalration of a general filter class.
\todo yet only a gaussian convolution is implemnted ... need to be heavily extended!
\par Example:
This example shows the typical use of the class:
\code
TODO: include a code snipped here
\endcode
\file filter.hh
\author Gert Wollny <wollny@cbs.mpg.de>
*/
#ifndef __MONA_FILTER_HH
#define __MONA_FILTER_HH 1
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
# pragma interface
#include <cstdio>
#include <vector>
// MONA specific
#include <libmona/monaConfig.hh>
#include <libmona/RefBase.hh>
namespace mona {
class T1DFilterKernel: public CRefCountedBase {
protected:
typedef std::vector<float> vec_mask;
T1DFilterKernel(int fsize);
public:
typedef vec_mask::const_iterator const_iterator;
/// returns a constant iterator at the begin if the filter kernel
const_iterator begin()const;
/// returns a constant iterator at the end if the filter kernel
const_iterator end()const;
/// returns the filter with definition parameter
const int get_fsize() const;
/// returns the width of the kernel
const int get_size() const;
float& operator[](int i) {
return m_mask[i];
}
const float operator[](int i)const {
return m_mask[i];
}
private:
int m_fsize;
vec_mask m_mask;
};
/** Class for a one-dimensional Gaussian convolution filter kernel. */
class T1DGaussFilterKernel: public T1DFilterKernel {
public:
/** contructor creates a kernel with the width = 2 * fsize +1
\param fsize is the filter with parameter */
T1DGaussFilterKernel(int fsize);
};
/** a class to execute a folding with a given filter kernel */
template <class T>
class T1DConvolutionFilter {
T1DFilterKernel *m_kernel;
public:
/** Conststructor creates a filter object from a given kernel
\param k the kernel to be used
*/
T1DConvolutionFilter(T1DFilterKernel *k);
/**
release the kernel
*/
~T1DConvolutionFilter();
/** filter a one-dimensional field with the filter object in place.
\param data is a vector of the given data row
*/
void filter_inplace(std::vector<T>& data) const;
struct TAccumulate {
typedef double type;
};
};
/**
Filter a one-dimensional data field with a Gaussian filter
\param data input data field, will be overwritten.
\param fsize filter width parameter -> width = 2*fsize +1
*/
template <class T>
void gauss_1d(std::vector<T>& data, int fsize);
/**
Filter a two-dimensional data field with a Gaussian filter
\param Data input data field, will be overwritten.
Data field class must provide \a get_size returning a C2DBounds with the size of the field,
and functions get_data_line_x, get_data_line_y, put_data_line_x, and put_data_line_y.
\param xFilterSize filter width parameter in x-direction
\param yFilterSize filter width parameter in y-direction
*/
template <class T2DDatafield>
void gauss_2d(T2DDatafield *Data,int xFilterSize, int yFilterSize);
/**
Filter a three-dimensional data field with a Gaussian filter
\param Data input data field, will be overwritten.
Data field class must provide \a get_size returning a C3DBounds with the size of the field,
and functions get_data_line_x, get_data_line_y, get_data_line_z,
put_data_line_x, put_data_line_y, and put_data_line_z.
\param xFilterSize filter width parameter in x-direction
\param yFilterSize filter width parameter in y-direction
\param zFilterSize filter width parameter in z-direction
*/
template <class T3DDatafield>
void gauss_3d(T3DDatafield *Data, int xFilterSize,
int yFilterSize, int zFilterSize);
/*
** Filters a 3DDataset with a Gaussian Filter of Size
** (2*xFilterSize+1) x (2*yFilterSize+1) x (2*zFilterSize+1)
**
**
*/
// Implementation part
// should go to lib/filter,cc, but g++ does not understand export
template <class T>
T1DConvolutionFilter<T>::T1DConvolutionFilter(T1DFilterKernel *k):
m_kernel(k)
{
m_kernel->add_ref();
}
template <class T>
T1DConvolutionFilter<T>::~T1DConvolutionFilter()
{
m_kernel->release();
}
template <class T>
void T1DConvolutionFilter<T>::filter_inplace(std::vector<T>& data) const
{
typedef std::vector<T> vec_t;
vec_t tmp(data);
T1DFilterKernel::const_iterator ek = m_kernel->end();
typename vec_t::iterator trgt = data.begin();
int pos = data.size();
// filter at left boundary
for (int i = 0; i < m_kernel->get_fsize(); ++i, --pos, ++trgt) {
typename TAccumulate::type ht = 0.0;
typename TAccumulate::type hw = 0.0;
T1DFilterKernel::const_iterator ik = m_kernel->begin();
typename vec_t::const_iterator src = tmp.begin();
advance(ik, m_kernel->get_fsize() - i);
while (ik != ek){
hw += *ik;
ht += *src++ * *ik++;
}
*trgt = T(ht / hw);
}
// filter inside
typename vec_t::iterator start = tmp.begin();
while (pos-- > m_kernel->get_fsize()) {
T1DFilterKernel::const_iterator ik = m_kernel->begin();
typename vec_t::iterator help = start;
typename TAccumulate::type ht = 0.0;
while (ik != ek){
ht += *ik++ * *help++;
}
*trgt = (T)ht;
++trgt;
++start;
}
// filter at right boundary
for (int i = 0; i < m_kernel->get_fsize(); ++i, --pos, ++trgt) {
T1DFilterKernel::const_iterator ik = m_kernel->begin();
ek = ik;
advance(ek, 2 * m_kernel->get_fsize() - i);
typename vec_t::const_iterator src = start;
typename TAccumulate::type ht = 0.0;
typename TAccumulate::type hw = 0.0;
while (ik != ek){
hw += *ik;
ht += *src++ * *ik++;
}
*trgt = T(ht / hw);
++start;
}
}
template <class T>
void gauss_1d(std::vector<T>& data, int fsize)
{
if (fsize)
T1DConvolutionFilter<T>(new T1DGaussFilterKernel(fsize)).filter_inplace(data);
}
template <class T2DDatafield>
void gauss_2d(T2DDatafield *Data,int xFilterSize, int yFilterSize)
{
typedef typename T2DDatafield::value_type value_type;
typedef std::vector<value_type> invec_t;
int cachXSize = Data->get_size().x;
int cachYSize = Data->get_size().y;
if (xFilterSize) {
T1DConvolutionFilter<value_type> Filter(new T1DGaussFilterKernel(xFilterSize));
invec_t buffer(cachXSize);
for (int y = 0; y < cachYSize; y++) {
Data->get_data_line_x(y,buffer);
Filter.filter_inplace(buffer);
Data->put_data_line_x(y,buffer);
}
}
if (yFilterSize) {
T1DConvolutionFilter<value_type> Filter(new T1DGaussFilterKernel(yFilterSize));
invec_t buffer(cachYSize);
for (int x = 0; x < cachXSize; x++) {
Data->get_data_line_y(x,buffer);
Filter.filter_inplace(buffer);
Data->put_data_line_y(x,buffer);
}
}
}
template <class T3DDatafield>
void gauss_3d(T3DDatafield *Data,int xFilterSize,
int yFilterSize, int zFilterSize)
{
typedef typename T3DDatafield::value_type value_type;
typedef std::vector<value_type> invec_t;
int cachXSize = Data->get_size().x;
int cachYSize = Data->get_size().y;
int cachZSize = Data->get_size().z;
if (xFilterSize > 0) {
T1DConvolutionFilter<value_type> XFilter(new T1DGaussFilterKernel(xFilterSize));
invec_t buffer(cachXSize);
for (int z = 0; z < cachZSize; z++){
for (int y = 0; y < cachYSize; y++) {
Data->get_data_line_x(y,z,buffer);
XFilter.filter_inplace(buffer);
Data->put_data_line_x(y,z,buffer);
}
}
}
if (yFilterSize > 0 ) {
T1DConvolutionFilter<value_type> YFilter(new T1DGaussFilterKernel(yFilterSize));
invec_t buffer(cachYSize);
for (int z = 0; z < cachZSize; z++){
for (int x = 0; x < cachXSize; x++) {
Data->get_data_line_y(x,z,buffer);
YFilter.filter_inplace(buffer);
Data->put_data_line_y(x,z,buffer);
}
}
}
if (zFilterSize > 0) {
T1DConvolutionFilter<value_type> ZFilter(new T1DGaussFilterKernel(zFilterSize));
invec_t buffer(cachZSize);
for (int y = 0; y < cachYSize; y++){
for (int x = 0; x < cachXSize; x++) {
Data->get_data_line_z(x,y,buffer);
ZFilter.filter_inplace(buffer);
Data->put_data_line_z(x,y,buffer);
}
}
}
}
} // namespace mona
#endif
/* CVS LOG
$Log$
Revision 1.8 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.7 2004/08/25 09:08:31 wollny
added an emacs style comment to all source files
Revision 1.6 2004/08/24 15:39:51 wollny
use iterators to get/put lines
Revision 1.5 2004/06/21 13:45:47 wollny
filter with proper non-periodic bounds handling
Revision 1.4 2004/06/09 15:35:58 wollny
minor twaks to remove some warnings
Revision 1.3 2004/06/03 09:57:32 wollny
Changed (hopefully) all instancable class names to Cxxxxx
Revision 1.2 2004/02/12 14:00:25 tittge
Major addaptions from libmia0
Revision 1.1.1.1 2004/02/11 18:18:05 tittge
start project
*/