aimstil  5.0.5
convert.h
Go to the documentation of this file.
1 #ifndef TIL_CONVERT_H
2 #define TIL_CONVERT_H
3 
4 // includes from STL
5 #include <limits>
6 
7 // includes from TIL library
8 #include "til/til_common.h"
9 #include "til/imageBasicStats.h"
10 #include "til/miscTools.h"
11 
12 // Namespace
13 namespace til
14 {
15 
21  // NB: The fact that out has to be allocated is not necessary. We
22  // could check that out is allocated, and allocate it if needed.
23  // However, as a general philosophy, I think it is better that
24  // no allocation is done on images without the user knowing it,
25  // because memory size can be huge, and the user has to be in
26  // control and aware of everything on this level.
27  template < class TImageIn, class TImageOut >
28  void convertScale
29  (
30  TImageIn const & in,
31  TImageOut & out
32  )
33  {
34  typedef typename TImageIn::value_type TPixelIn;
35  typedef typename TImageOut::value_type TPixelOut;
36 
37  const TPixelIn EPSILON = 32 * std::numeric_limits<TPixelIn>::epsilon();
38 
39  // Check that images have same size
40  similarityCheck(in, out);
41 
42  // Test if in has too little variation to be rescaled
43  // If so, output image is set to zero
44  double maxIm = (double) max(in);
45  double minIm = (double) min(in);
46  if (std::abs(maxIm - minIm) <= EPSILON)
47  {
48  out.reset();
49  return;
50  }
51 
52  // Compute the parameters of the affine rescaling of the intensity
53  // TODO: choose automatically type of coefficients a and b
54  // using traits (e.g. double is not enough is TIn is of type long double
55  TPixelIn t1max = std::numeric_limits<TPixelIn>::max();
56  TPixelIn t1min = std::numeric_limits<TPixelIn>::min();
57 
58  double a = (t1max - t1min) / (maxIm - minIm);
59  double b = (t1min * maxIm - t1max*minIm) / (maxIm - minIm);
60 
61  // Apply the intensity affine transform from in to out
62  typename Iterator<TImageIn>::ConstLinear iIn(in);
63  typename Iterator<TImageOut>::Linear iOut(out);
64  do
65  {
66  *iOut = castValue<TPixelIn,TPixelOut>(*iIn*a + b);
67  }
68  while (iIn.next() && iOut.next());
69  }
70 
71 
72 
75  // TODO: I renammed this into convert_im because of a conflict with a general convert,
76  // that will eventually take over when the Grand Plan is finished.
77  template < class TImageIn, class TImageOut >
78  void convert_im
79  (
80  TImageIn const & in,
81  TImageOut & out
82  )
83  {
84  typedef typename TImageIn::value_type TPixelIn;
85  typedef typename TImageOut::value_type TPixelOut;
86 
87  // Check that image have same size
88  similarityCheck(in, out);
89 
90  // If the maximum value possible of new type is less than the
91  // maximum value of old type...
93  {
94  // ...check whether maximum input image value is greater than new type max
96  {
97  // Throw an exception: The user will have to think of an
98  // alternative
99  throw std::overflow_error("Output type cannot handle input image range");
100  }
101  }
102 
103  // Same thing with minimums:
104  // If the minimum value possible of new type is greater than the
105  // minimum value of old type...
106  if (type_min<TPixelOut>() > type_min<TPixelIn>())
107  {
108  // ...check whether minimum image value is lesser than new type min
109  if (min(in) < type_min<TPixelOut>())
110  {
111  // Throw an exception: The user will have to think of an
112  // alternative
113  throw std::overflow_error("Output type cannot handle input image range");
114  }
115  }
116 
117  // Do the conversion
118  typename Iterator<TImageIn>::ConstLinear iIn(in);
119  typename Iterator<TImageOut>::Linear iOut(out);
120  for (; !iIn.isAtEnd(); ++iIn, ++iOut)
121  {
122  *iOut = castValue<TPixelIn,TPixelOut>(*iIn);
123  }
124  }
125 
126 } // namespace
127 
128 
129 #endif
130 
A trait class to assign iterators to image types.
boost::enable_if< is_Image< TImage >, typename TImage::value_type >::type min(const TImage &im)
void similarityCheck(const TImage1 &im1, const TImage2 &im2)
Check whether both images are allocated and have the same size and voxel size.
Definition: imageTools.h:75
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
General macros, definitions and functions.
numeric_array< T, D > abs(const numeric_array< T, D > &a)
Absolute value, element-wise.
TImage::value_type max(const TImage &im)
Returns the maximum intensity of the input image.
void convertScale(TImageIn const &in, TImageOut &out)
Convert an image into the type of the other image while scaling its intensity range to span through t...
Definition: convert.h:29
void convert_im(TImageIn const &in, TImageOut &out)
Convert an image into the type of the other image.
Definition: convert.h:79