aimsalgo  5.1.2
Neuroimaging image processing
convol.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_SIGNALFILTER_CONVOL_H
36 #define AIMS_SIGNALFILTER_CONVOL_H
37 
40 #include <cartobase/type/limits.h>
41 #include <float.h>
42 
43 
51 template <class T>
53 {
54 public:
55 
56  AimsConvolution(bool safe=true) : _safe(safe) {
57  set_safe_status(safe);
58  }
59  virtual ~AimsConvolution() { }
60 
63 
64  void set_safe_status(bool safe=true) {
65  if (safe)
67  else
69  _safe = safe;
70  }
71 
72 protected:
76  int x, int y, int z, int t );
80  int x, int y, int z, int t);
84  int x, int y, int z, int t);
85  T min() const;
86  T max() const;
87 
90  carto::rc_ptr<carto::Volume< T > >& kernel, int x, int y, int z, int t);
91 
92  bool _safe;
93 };
94 
102 template <class T>
104 {
105 public:
106 
108  short sources=0, bool safe=true)
109  : AimsConvolution< T >(safe),
110  _sources(sources),
111  _mask(mask)
112  { }
117  int x, int y, int z, int t);
121  int x, int y, int z, int t);
122 
123 protected:
124  short _sources;
126 };
127 
128 
129 template< class T > inline
133 {
134  ASSERT( kernel->getSizeT() == 1 );
135  if (_safe == false)
136  ASSERT( img->getBorders()[0] >= 1 );
137  else
138  if( img->getBorders()[0] >= 1 )
139  set_safe_status(false);
140 
141  int x, y, z, t;
142 
143  int dx = img->getSizeX();
144  int dy = img->getSizeY();
145  int dz = img->getSizeZ();
146  int dt = img->getSizeT();
147 
148  carto::VolumeRef< T > res( dx, dy, dz, dt );
149  res.setVoxelSize( img->getVoxelSize() );
150 
151  for ( t=0; t<dt; t++ )
152  for ( z=0; z<dz; z++ )
153  for ( y=0; y<dy; y++ )
154  for ( x=0; x<dx; x++ )
155  {
156  T val = doit_voxel(img, kernel, x, y, z, t);
157  if ( val > max() )
158  val = max();
159  else if ( val < min() )
160  val = min();
161  res( x, y, z, t ) = val;
162  }
163 
164  return res;
165 }
166 
167 template< class T > inline
170  int x, int y, int z, int t)
171 {
172  return (this->*_doit_voxel_method)(img, kernel, x, y, z, t);
173 };
174 
175 template< class T > inline
178  carto::rc_ptr<carto::Volume< T > >& kernel, int x, int y, int z, int t)
179 {
180  int i, j, k, idx, idy, idz;
181 
182  int mx = kernel->getSizeX();
183  int my = kernel->getSizeY();
184  int mz = kernel->getSizeZ();
185 
186  int mx2 = mx / 2;
187  int my2 = my / 2;
188  int mz2 = mz / 2;
189 
190  int dx = img->getSizeX();
191  int dy = img->getSizeY();
192  int dz = img->getSizeZ();
193 
194  T val = (T)0;
195 
196  for ( k=0; k<mz; k++ )
197  for ( j=0; j<my; j++ )
198  for ( i=0; i<mx; i++ )
199  {
200  idx = x - i + mx2;
201  idy = y - j + my2;
202  idz = z - k + mz2;
203 
204  if ( idx >= 0 && idy >= 0 && idz >= 0 &&
205  idx < dx && idy < dy && idz < dz )
206  val += kernel->at( i, j, k ) * img->at( idx, idy, idz, t );
207  }
208 
209  return val;
210 }
211 
212 template< class T > inline
215  carto::rc_ptr<carto::Volume< T > >& kernel, int x, int y, int z, int t)
216 {
217  int i, j, k, idx, idy, idz;
218 
219  int mx = kernel->getSizeX();
220  int my = kernel->getSizeY();
221  int mz = kernel->getSizeZ();
222 
223  int mx2 = mx / 2;
224  int my2 = my / 2;
225  int mz2 = mz / 2;
226 
227  T val = (T)0;
228 
229  for ( k=0; k<mz; k++ )
230  for ( j=0; j<my; j++ )
231  for ( i=0; i<mx; i++ )
232  {
233  idx = x - i + mx2;
234  idy = y - j + my2;
235  idz = z - k + mz2;
236 
237  val += kernel->at( i, j, k ) * img->at( idx, idy, idz, t );
238  }
239 
240  return val;
241 }
242 
243 template< class T > inline
246  carto::rc_ptr<carto::Volume< T > >& kernel, int x, int y, int z, int t)
247 {
248  if (_mask(x, y, z, t) == _sources)
249  return img->at(x, y, z, t);
250  return AimsConvolution<T>::doit_voxel_safe(img, kernel, x, y, z, t);
251 }
252 
253 template< class T > inline
256  carto::rc_ptr<carto::Volume< T > >& kernel, int x, int y, int z, int t )
257 {
258  if (_mask(x, y, z, t) == _sources)
259  return img->at(x, y, z, t);
260  return AimsConvolution<T>::doit_voxel_unsafe(img, kernel, x, y, z, t);
261 }
262 
263 template <>
264 inline
266 {
268 }
269 
270 
271 template <>
272 inline
274 {
276 }
277 
278 
279 template <>
280 inline
282 {
283  return 0;
284 }
285 
286 
287 template <>
288 inline
290 {
292 }
293 
294 
295 template <>
296 inline
298 {
300 }
301 
302 
303 template <>
304 inline
306 {
308 }
309 
310 
311 template <>
312 inline
314 {
315  return 0;
316 }
317 
318 
319 template <>
320 inline
322 {
324 }
325 
326 
327 template <>
328 inline
330 {
332 }
333 
334 
335 template <>
336 inline
338 {
340 }
341 
342 
343 template <>
344 inline
346 {
347  return 0;
348 }
349 
350 
351 template <>
352 inline
354 {
356 }
357 
358 
359 template <>
360 inline
362 {
363  return (-FLT_MAX);
364 }
365 
366 
367 template <>
368 inline
370 {
371  return FLT_MAX;
372 }
373 
374 
375 template <>
376 inline
378 {
379  return DBL_MIN;
380 }
381 
382 
383 template <>
384 inline
386 {
387  return DBL_MAX;
388 }
389 
390 
391 // ### remove after everything has been moved to intN_t/uintN_t
392 #include <limits.h>
393 #if !defined(__sun__) || !defined(_CHAR_IS_SIGNED)
394 template<>
395 inline
397 {
399 }
400 
401 
402 template <>
403 inline
405 {
407 }
408 #endif
409 
410 
411 template <>
412 inline
414 {
415  return LONG_MIN;
416 }
417 
418 
419 template <>
420 inline
422 {
423  return LONG_MAX;
424 }
425 
426 
427 template <>
428 inline
430 {
431  return 0;
432 }
433 
434 
435 template <>
436 inline
438 {
439  return ULONG_MAX;
440 }
441 
442 
443 #endif
#define ASSERT(EX)
The template class to make convolutions.
Definition: convol.h:53
T(AimsConvolution::* _doit_voxel_method)(carto::rc_ptr< carto::Volume< T > > &img, carto::rc_ptr< carto::Volume< T > > &kernel, int x, int y, int z, int t)
Definition: convol.h:88
AimsConvolution(bool safe=true)
Definition: convol.h:56
virtual T doit_voxel_unsafe(carto::rc_ptr< carto::Volume< T > > &img, carto::rc_ptr< carto::Volume< T > > &kernel, int x, int y, int z, int t)
called for each voxel (unsafe version)
Definition: convol.h:213
void set_safe_status(bool safe=true)
Definition: convol.h:64
T doit_voxel(carto::rc_ptr< carto::Volume< T > > &img, carto::rc_ptr< carto::Volume< T > > &kernel, int x, int y, int z, int t)
called for each voxel (user selected function)
Definition: convol.h:168
carto::VolumeRef< T > doit(carto::rc_ptr< carto::Volume< T > > &, carto::rc_ptr< carto::Volume< T > > &)
Definition: convol.h:131
bool _safe
Definition: convol.h:92
virtual ~AimsConvolution()
Definition: convol.h:59
virtual T doit_voxel_safe(carto::rc_ptr< carto::Volume< T > > &img, carto::rc_ptr< carto::Volume< T > > &kernel, int x, int y, int z, int t)
called for each voxel (safe version)
Definition: convol.h:176
Make convolution only on a specified mask.
Definition: convol.h:104
virtual ~AimsMaskedConvolution()
Definition: convol.h:113
AimsMaskedConvolution(const carto::rc_ptr< carto::Volume< short > > &mask, short sources=0, bool safe=true)
Definition: convol.h:107
virtual T doit_voxel_safe(carto::rc_ptr< carto::Volume< T > > &img, carto::rc_ptr< carto::Volume< T > > &kernel, int x, int y, int z, int t)
called for each voxel (safe version)
Definition: convol.h:244
const carto::VolumeRef< short > _mask
Definition: convol.h:125
virtual T doit_voxel_unsafe(carto::rc_ptr< carto::Volume< T > > &img, carto::rc_ptr< carto::Volume< T > > &kernel, int x, int y, int z, int t)
called for each voxel (unsafe version)
Definition: convol.h:254
void setVoxelSize(float vx, float vy=1., float vz=1., float vt=1.)
float min(float x, float y)
Definition: thickness.h:106
float max(float x, float y)
Definition: thickness.h:98
BucketMap< Void > * mask(const BucketMap< Void > &src, const BucketMap< Void > &m, bool intersect=true)
static _Tp min()
static _Tp max()
unsigned long ulong