aimsalgo 6.0.0
Neuroimaging image processing
imagealgorithm.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 AIMSALGO_ALGORITHM_IMAGEALGORITHM_H
35#define AIMSALGO_ALGORITHM_IMAGEALGORITHM_H
36
37//--- aims -------------------------------------------------------------------
38#include <aims/utility/channel.h>
39#include <aims/vector/vector.h>
40//--- cartodata --------------------------------------------------------------
41#include <cartodata/volume/volume.h>
42//--- cartobase --------------------------------------------------------------
43#include <cartobase/config/verbose.h>
44#include <cartobase/type/string_conversion.h>
46#include <cartobase/type/datatypetraits.h>
47#include <cartobase/type/datatypeinfo.h>
48#include <cartobase/smart/rcptr.h>
49//--- std --------------------------------------------------------------------
50#include <iostream>
51//----------------------------------------------------------------------------
52
53namespace aims
54{
55 template <typename T> class ImageAlgorithmInterface;
56 template <typename T> class ImageAlgorithm;
57 template <typename T, bool M> class ImageAlgorithmSwitch;
58
59 //==========================================================================
60 // IMAGE ALGORITHM INTERFACE
61 //==========================================================================
67 template <typename T>
69 public:
70 //----------------------------------------------------------------------
71 // types
72 //----------------------------------------------------------------------
73 typedef T VoxelType;
74
75 //----------------------------------------------------------------------
76 // interface (needs implementation in derived classes)
77 //----------------------------------------------------------------------
84 virtual Point4dl getOutputImageDimensions( const Point4dl & dims ) const
85 {
86 return Point4dl( dims );
87 }
88
95 virtual Point4df getOutputImageVoxelSize( const Point4df & voxelsize ) const
96 {
97 return Point4df( voxelsize );
98 }
99
107 virtual void execute( const carto::VolumeRef<T> & in,
108 carto::VolumeRef<T> & out ) const = 0;
109
110 virtual void setOptions( const carto::Object & /*options*/ ) {};
111 virtual void updateOptions( const carto::Object & /*options*/ ) {};
112
115 virtual ImageAlgorithmInterface *clone() const = 0;
116
117 //----------------------------------------------------------------------
118 // utility method
119 //----------------------------------------------------------------------
121 virtual Point4dl getOutputImageDimensions( const long & dx = 1,
122 const long & dy = 1,
123 const long & dz = 1,
124 const long & dt = 1 ) const
125 {
126 return getOutputImageDimensions( Point4dl( dx, dy, dz, dt ) );
127 }
128
130 virtual Point4df getOutputImageVoxelSize( const float & vx = 1.0,
131 const float & vy = 1.0,
132 const float & vz = 1.0,
133 const float & vt = 1.0 ) const
134 {
135 return getOutputImageVoxelSize( Point4df( vx, vy, vz, vt ) );
136 }
137
140 {
141 Point4dl dims( in.getSizeX(), in.getSizeY(), in.getSizeZ(), in.getSizeT() );
142 std::vector<float> voxelsize(4, 1.);
143
144 in.header().getProperty( "voxel_size", voxelsize );
145 Point4df voxel( voxelsize[0], voxelsize[1], voxelsize[2], voxelsize[3] );
146 dims = getOutputImageDimensions( dims );
147 voxel = getOutputImageVoxelSize( voxel );
148 voxelsize[0] = voxel[0];
149 voxelsize[1] = voxel[1];
150 voxelsize[2] = voxel[2];
151 voxelsize[3] = voxel[3];
152
153 carto::VolumeRef<T> out( dims[0], dims[1], dims[2], dims[3] );
154 out->copyHeaderFrom( in.header() );
155 out.header().setProperty( "voxel_size", voxelsize );
156 execute( in, out );
157
158 return out;
159 }
160
161 //----------------------------------------------------------------------
162 // verbose
163 //----------------------------------------------------------------------
164 public:
165 virtual void setVerbose( int level ) { _verbose = level; }
166 virtual void setQuiet() { setVerbose(0); }
167 protected:
169
170 //----------------------------------------------------------------------
171 // polymorphism
172 //----------------------------------------------------------------------
173 public:
175 protected:
181 {
182 _verbose = other._verbose;
183 }
184 };
185
186
187 //==========================================================================
188 // IMAGE ALGORITHM
189 //==========================================================================
199 template <typename T>
201 {
202 public:
203 //----------------------------------------------------------------------
204 // types
205 //----------------------------------------------------------------------
206 typedef T VoxelType;
208
209 //----------------------------------------------------------------------
210 // interface implementation
211 //----------------------------------------------------------------------
212 virtual void execute( const carto::VolumeRef<T> & in,
213 carto::VolumeRef<T> & out ) const
214 {
216 S::execute( *_algo, in, out, this->_verbose );
217 }
218
219 virtual Point4dl getOutputImageDimensions( const Point4dl & dims ) const {
220 return _algo->getOutputImageDimensions( dims );
221 }
222
223 virtual Point4df getOutputImageVoxelSize( const Point4df & voxelsize ) const {
224 return _algo->getOutputImageVoxelSize( voxelsize );
225 }
226
227 // I need this line to unhide all the "execute" method since the
228 // overloading of one hid all the others.
232
233 virtual ImageAlgorithm<T> *clone() const {
234 return new ImageAlgorithm<T>( *this );
235 }
236
237 virtual void setVerbose( int level )
238 {
240 _algo->setVerbose( level );
241 }
242
243 //----------------------------------------------------------------------
244 // polymorphism
245 //----------------------------------------------------------------------
246 public:
247 virtual ~ImageAlgorithm() {}
248 protected:
256 _algo( (deepcopy ? algo->clone() : algo ) )
257 {}
259 ImageAlgorithmInterface<T>(other),
260 _algo(other._algo->clone())
261 {}
263 {
264 ASSERT( typeid(other) == typeid(*this) );
265 if( &other != this ) {
267 _algo.reset( other._algo->clone() );
268 }
269 return *this;
270 }
271
272 //----------------------------------------------------------------------
273 // member
274 //----------------------------------------------------------------------
275 protected:
277 };
278
279 //==========================================================================
280 // IMAGE ALGORITHM SWITCH
281 //==========================================================================
292 template <typename T, bool M>
294 public:
295 typedef T VoxelType;
297 };
298
299 //--------------------------------------------------------------------------
300 // specialization: single channel
301 //--------------------------------------------------------------------------
310 template <typename T>
311 class ImageAlgorithmSwitch<T,false> {
312 public:
314
320 const carto::VolumeRef<T> & in,
322 int verbose = carto::verbose )
323 {
324 (void)(verbose);
325 return algo.execute( in, out );
326 }
327 };
328
329 //--------------------------------------------------------------------------
330 // specialization: multi channel
331 //--------------------------------------------------------------------------
340 template <typename T>
341 class ImageAlgorithmSwitch<T,true> {
342 public:
343 typedef typename T::ChannelType ChannelType;
345
350 static void execute(
352 const carto::VolumeRef<T> & in,
354 int verbose )
355 {
356 ChannelSelector< carto::VolumeRef<T>,
358 > selector;
359
360 for( uint8_t channel = 0;
361 channel < DataTypeInfo<T>::samples();
362 channel++ )
363 {
364 if( verbose )
365 std::cout << "Processing channel :"
366 << carto::toString(channel)
367 << std::endl;
368
369 // Split the data and process filter on each component
370 const carto::VolumeRef<ChannelType> & inChannel = selector.select( in, channel );
371 const carto::VolumeRef<ChannelType> & outChannel = algo.execute( inChannel );
372 selector.set( out, channel, outChannel );
373 }
374 }
375 };
376
377}
378
379#endif
#define ASSERT(EX)
aims::ImageAlgorithmInterface is the interface for an image processing algorithm.
virtual carto::VolumeRef< T > execute(const carto::VolumeRef< T > &in) const
virtual Point4df getOutputImageVoxelSize(const Point4df &voxelsize) const
Returns the output voxel size of the processed image.
virtual void setVerbose(int level)
virtual Point4dl getOutputImageDimensions(const long &dx=1, const long &dy=1, const long &dz=1, const long &dt=1) const
virtual void updateOptions(const carto::Object &)
virtual Point4df getOutputImageVoxelSize(const float &vx=1.0, const float &vy=1.0, const float &vz=1.0, const float &vt=1.0) const
ImageAlgorithmInterface(const ImageAlgorithmInterface &other)
virtual Point4dl getOutputImageDimensions(const Point4dl &dims) const
Returns the output dimensions of the processed image.
virtual ImageAlgorithmInterface * clone() const =0
ImageAlgorithmInterface<T> Pure virtual method.
virtual void execute(const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out) const =0
ImageAlgorithmInterface<T> Pure virtual method.
virtual void setOptions(const carto::Object &)
ImageAlgorithmInterface & operator=(const ImageAlgorithmInterface &other)
static void execute(ImageAlgorithmInterface< T > &algo, const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out, int verbose=carto::verbose)
Execute ImageAlgorithmInterface<T> on VolumeRef<T>.
static void execute(ImageAlgorithmInterface< ChannelType > &algo, const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out, int verbose)
Execute ImageAlgorithmInterface<T> on VolumeRef<T>.
aims::ImageAlgorithmSwitch switches between multichannel and monochannel ImageAlgorithmInterface
aims::ImageAlgorithm class used to implement image algorithms
ImageAlgorithm(ImageAlgorithmInterface< ChannelType > *algo, bool deepcopy=true)
ImageAlgorithm(const ImageAlgorithm &other)
ImageAlgorithm(const ImageAlgorithmInterface< ChannelType > &algo)
virtual Point4dl getOutputImageDimensions(const Point4dl &dims) const
Returns the output dimensions of the processed image.
virtual void execute(const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out) const
ImageAlgorithmInterface<T> Pure virtual method.
ImageAlgorithm & operator=(const ImageAlgorithm &other)
carto::rc_ptr< ImageAlgorithmInterface< ChannelType > > _algo
virtual Point4df getOutputImageVoxelSize(const Point4df &voxelsize) const
Returns the output voxel size of the processed image.
virtual void setVerbose(int level)
carto::DataTypeTraits< T >::ChannelType ChannelType
virtual ImageAlgorithm< T > * clone() const
ImageAlgorithmInterface<T> Pure virtual method.
void setProperty(const std::string &, const T &)
bool getProperty(const std::string &, T &) const
int getSizeZ() const
virtual void copyHeaderFrom(const PropertySet &other)
int getSizeT() const
int getSizeY() const
const PropertySet & header() const
int getSizeX() const
std::string toString(const T &object)
Volume< T > deepcopy(const Volume< T > &src, const std::vector< int > &size=std::vector< int >())
int verbose
AimsVector< int64_t, 4 > Point4dl
AimsVector< float, 4 > Point4df