aimsalgo  5.1.2
Neuroimaging image processing
subsamplingpyramid_d.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 #ifndef AIMS_PYRAMID_SUBSAMPLINGPYRAMID_D_H
35 #define AIMS_PYRAMID_SUBSAMPLINGPYRAMID_D_H
36 
38 #include <aims/io/writer.h>
40 
41 namespace aims {
42 
43  //--------------------------------------------------------------------------
44  // Constructors / Destructors / Copy
45  //--------------------------------------------------------------------------
46 
47  template <typename S>
49  _subsampler( f ),
50  _factor( 1, Point4du(factor, factor, factor, factor) ),
51  _dir( 4, true ),
52  _verbose( carto::verbose )
53  {
54  _dir[3] = false;
55  }
56 
57  template <typename S>
59  _subsampler( f ),
60  _factor( 1, factor ),
61  _dir( 4, true ),
62  _verbose( carto::verbose )
63  {
64  _dir[3] = false;
65  }
66 
67  template <typename S>
68  SubSamplingPyramidBuilder<S>::SubSamplingPyramidBuilder( const SubSampler & f, const std::vector<Point4du> & factor ):
69  _subsampler( f ),
70  _factor( factor ),
71  _dir( 4, true ),
72  _verbose( carto::verbose )
73  {
74  _dir[3] = false;
75  }
76 
77  template <typename S>
79  _subsampler( other._subsampler ),
80  _factor( other._factor ),
81  _dir( other._dir ),
82  _verbose( other._verbose )
83  {}
84 
85  template <typename S>
87  {}
88 
89  template <typename S>
92  {
93  if( this != &other )
94  {
95  _subsampler = other._subsampler;
96  _factor = other._factor;
97  _dir = other._dir;
98  _verbose = other._verbose;
99  }
100  return *this;
101  }
102 
103 
104  //--------------------------------------------------------------------------
105  // Parameters
106  //--------------------------------------------------------------------------
107 
108  template <typename S>
109  const std::vector<bool> & SubSamplingPyramidBuilder<S>::directions() const
110  {
111  return _dir;
112  }
113 
114  template <typename S>
115  const std::vector<Point4du> & SubSamplingPyramidBuilder<S>::factor() const
116  {
117  return _factor;
118  }
119 
120  template <typename S>
122  {
123  _factor.assign( 1, Point4du( r, r, r, r ) );
124  }
125 
126  template <typename S>
128  {
129  _factor.assign( 1, r );
130  }
131 
132  template <typename S>
133  void SubSamplingPyramidBuilder<S>::setFactor( const std::vector<Point4du> & r )
134  {
135  _factor = r;
136  }
137 
138  template <typename S>
140  {
141  _subsampler = f;
142  }
143 
144  template <typename S>
145  void SubSamplingPyramidBuilder<S>::setDirections( const std::vector<bool> & dir )
146  {
147  _dir.assign( 4, true );
148  _dir[3] = false;
149  for( size_t i = 0; i < dir.size() && i < _dir.size(); ++i )
150  _dir[i] = dir[i];
151  }
152 
153  template <typename S>
154  void SubSamplingPyramidBuilder<S>::setDirections( bool dirx, bool diry, bool dirz, bool dirt )
155  {
156  _dir[0] = dirx;
157  _dir[1] = diry;
158  _dir[2] = dirz;
159  _dir[3] = dirt;
160  }
161 
162  template <typename S>
164  {
165  _verbose = verbose;
166  }
167 
168  template <typename S>
170  {
171  _verbose = 0;
172  }
173 
174  //--------------------------------------------------------------------------
175  // Execution
176  //--------------------------------------------------------------------------
177 
178  template <typename S>
179  template <typename T>
180  std::vector<carto::VolumeRef<T> > SubSamplingPyramidBuilder<S>::execute( const carto::VolumeRef<T> & vol )
181  {
182  std::vector<Point4du> factors = computeFactors( vol.getSize() );
183  std::vector<carto::VolumeRef<T> > pyramid;
184  pyramid.reserve( factors.size() + 1 );
185  _subsampler.setVerbose( _verbose - 1 );
186 
187  // FIRST LEVEL
188  if( _verbose > 0 )
189  std::cout << "Copy first level... " << std::endl;
190  pyramid.push_back( carto::copy(vol) );
191 
192  // OTHER LEVELS
193  std::vector<Point4du>::iterator f;
194  int l = 1;
195  for( f = factors.begin(); f != factors.end(); ++f, ++l )
196  {
197  if( _verbose > 0 )
198  std::cout << "Compute subsampled level " << l << "... " << std::endl;
199 
200  if( _verbose > 1 )
201  std::cout << "Moving average filter/subsampling: " << std::flush;
202  _subsampler.setFactor( *f );
203  _subsampler.setDirections( _dir[0] && (*f)[0] > 1,
204  _dir[1] && (*f)[1] > 1,
205  _dir[2] && (*f)[2] > 1,
206  _dir[3] && (*f)[3] > 1 );
207  pyramid.push_back( _subsampler.template execute<T,T>( pyramid[l-1] ) );
208  }
209 
210  return pyramid;
211  }
212 
213  //--------------------------------------------------------------------------
214  // Helpers
215  //--------------------------------------------------------------------------
216  template <typename S>
217  std::vector<Point4du> SubSamplingPyramidBuilder<S>::computeFactors( const std::vector<int> & size ) const
218  {
219  std::vector<Point4du> factors;
220  if( _factor.size() > 1 )
221  {
222  factors = _factor;
223  }
224  else
225  {
226  unsigned nlevels = 0;
227  Point4dl level_size( size[0], size[1], size[2], size[3] );
228  bool ok = true;
229  while( ok )
230  {
231  ++nlevels;
232  for( int i = 0; i < 4; ++i )
233  {
234  if( _dir[i] ) {
235  level_size[i] /= _factor[0][i];
236  if( level_size[i] == 1 )
237  ok = false;
238  }
239  }
240  }
241  factors.assign( nlevels, _factor[0] );
242  }
243 
244  Point4dl level_size( size[0], size[1], size[2], size[3] );
245  for( std::vector<Point4du>::iterator f = factors.begin();
246  f != factors.end(); ++f )
247  {
248  for( int i = 0; i < 4; ++i )
249  {
250  if( _dir[i] )
251  {
252  if( level_size[i] / (*f)[i] == 0 )
253  (*f)[i] = 1;
254  else
255  level_size[i] /= (*f)[i];
256  }
257  else
258  (*f)[i] = 1;
259  }
260  if( *f == Point4du(1, 1, 1, 1) )
261  {
262  factors.erase( f, factors.end() );
263  break;
264  }
265  }
266 
267  return factors;
268  }
269 
270 } // namespace aims
271 
272 #endif // AIMS_PYRAMID_SUBSAMPLINGPYRAMID_D_H
std::vector< Point4du > _factor
std::vector< carto::VolumeRef< T > > execute(const carto::VolumeRef< T > &vol)
Execution.
std::vector< Point4du > computeFactors(const std::vector< int > &size) const
const std::vector< Point4du > & factor() const
const std::vector< bool > & directions() const
Parameters.
void setSubSampler(const SubSampler &f)
void setDirections(const std::vector< bool > &dir)
SubSamplingPyramidBuilder & operator=(const SubSamplingPyramidBuilder &other)
SubSamplingPyramidBuilder(const SubSampler &f, unsigned factor=2)
Constructors / Destructors / Copy.
std::vector< int > getSize() const
int verbose
Volume< T > copy(const Volume< T > &src)