aimsalgo  5.1.2
Neuroimaging image processing
medianresampler_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 
35 #ifndef AIMS_RESAMPLING_MEDIANRESAMPLER_D_H
36 #define AIMS_RESAMPLING_MEDIANRESAMPLER_D_H
37 
39 
41 
42 #include <cmath>
43 
44 namespace aims
45 {
46 
47 template < class T >
49  : SplineResampler< T >()
50 {
51 }
52 
53 
54 template < class T >
56 {
57 }
58 
59 
60 template < class T >
62 {
63 
64  return 201;
65 
66 }
67 
68 
69 template < class T >
72  const soma::Transformation3d& invTransform3d,
73  const ChannelType& outBackground,
74  const Point3df& outLocation,
75  ChannelType& outValue, int t ) const
76 {
77 
78  const ChannelType *i = &inVolume.at( 0, 0, 0, t );
79  const ChannelType *pi, *pj;
80 
81  Point3df normalizedInLocation;
82  normalizedInLocation = invTransform3d.transform( outLocation );
83 
84  float xf = round(normalizedInLocation[0]);
85  float yf = round(normalizedInLocation[1]);
86  float zf = round(normalizedInLocation[2]);
87 
88  std::vector<int> dims = inVolume.getSize();
89  int dimx = dims[0], dimy = dims[1], dimz = dims[2];
90 
91  // The test is done using floating-point so that NaN values are excluded (the
92  // background value is returned if the transformation yields NaN)
93  if ( ( xf >= 0 ) && ( xf < dimx ) &&
94  ( yf >= 0 ) && ( yf < dimy ) &&
95  ( zf >= 0 ) && ( zf < dimz ) )
96  {
97 
98  double weightX0, weightY0, weightX1, weightY1;
99  long foldX0, foldY0, foldX1, foldY1;
100  double intensity, qi, qj;
101 
102  // first y contribution
103  int y = static_cast<long>(floor(normalizedInLocation[1]));
104  weightY0 = getBSplineWeight( y, normalizedInLocation[1] );
105  foldY0 = (long)this->getFold( y, dims[1] ) * dims[0];
106 
107  // second y contribution
108  ++ y;
109  weightY1 = getBSplineWeight( y, normalizedInLocation[1] );
110  foldY1 = (long)this->getFold( y, dimy ) * dimx;
111 
112  // first x contribution
113  int x = static_cast<long>(floor(normalizedInLocation[0]));
114  weightX0 = getBSplineWeight( x, normalizedInLocation[0] );
115  foldX0 = (long)this->getFold( x, dimx );
116 
117  // second x contribution
118  ++ x;
119  weightX1 = getBSplineWeight( x, normalizedInLocation[0] );
120  foldX1 = (long)this->getFold( x, dimx );
121 
122  std::map<ChannelType, unsigned long> values;
123  unsigned long nv = 0;
124 
125  if ( dimz == 1 )
126  {
127 
128  //counting contributions
129  pj = i;
130  pi = pj + (size_t)(foldY0);
131  if( weightY0 != 0 )
132  {
133  if( weightX0 != 0 )
134  {
135  values[*( pi + (size_t)(foldX0) )]++;
136  ++nv;
137  }
138  if( weightX1 != 0 )
139  {
140  values[*( pi + size_t(foldX1) )]++;
141  ++nv;
142  }
143  }
144  if( weightY1 != 0 )
145  {
146  pi = pj + foldY1;
147  if( weightX0 != 0 && weightY1 != 0 )
148  {
149  values[*( pi + (size_t)(foldX0) )]++;
150  ++nv;
151  }
152  if( weightX1 != 0 && weightY1 != 0 )
153  {
154  values[*( pi + (size_t)(foldX1) )]++;
155  ++nv;
156  }
157  }
158 
159  }
160  else
161  {
162 
163  // first z contribution
164  int z = static_cast<long>(floor(normalizedInLocation[2]));
165  if( getBSplineWeight( z, normalizedInLocation[2] ) != 0 )
166  {
167  pj = i + (size_t)(this->getFold( z, dimz )) * dimx * dimy;
168  pi = pj + (size_t)(foldY0);
169 
170  if( weightY0 != 0 )
171  {
172  if( weightX0 != 0 )
173  {
174  values[*( pi + (size_t)(foldX0) )]++;
175  ++nv;
176  }
177  if( weightX1 != 0 )
178  {
179  values[*( pi + size_t(foldX1) )]++;
180  ++nv;
181  }
182  }
183  if( weightY1 != 0 )
184  {
185  pi = pj + foldY1;
186  if( weightX0 != 0 && weightY1 != 0 )
187  {
188  values[*( pi + (size_t)(foldX0) )]++;
189  ++nv;
190  }
191  if( weightX1 != 0 && weightY1 != 0 )
192  {
193  values[*( pi + (size_t)(foldX1) )]++;
194  ++nv;
195  }
196  }
197 
198  }
199  // second z contribution
200  ++ z;
201  if( getBSplineWeight( z, normalizedInLocation[2] ) != 0 )
202  {
203  pj = i + (size_t)(this->getFold( z, dimz )) * dimx *
204  dimy;
205  pi = pj + (size_t)(foldY0);
206 
207  if( weightY0 != 0 )
208  {
209  if( weightX0 != 0 )
210  {
211  values[*( pi + (size_t)(foldX0) )]++;
212  ++nv;
213  }
214  if( weightX1 != 0 )
215  {
216  values[*( pi + size_t(foldX1) )]++;
217  ++nv;
218  }
219  }
220  if( weightY1 != 0 )
221  {
222  pi = pj + foldY1;
223  if( weightX0 != 0 && weightY1 != 0 )
224  {
225  values[*( pi + (size_t)(foldX0) )]++;
226  ++nv;
227  }
228  if( weightX1 != 0 && weightY1 != 0 )
229  {
230  values[*( pi + (size_t)(foldX1) )]++;
231  ++nv;
232  }
233  }
234  }
235  }
236 
237  unsigned long c = 0, nv2 = (unsigned long) ::ceil( nv / 2. );
238  ChannelType medv = 0;
239  typename std::map<ChannelType, unsigned long>::const_iterator
240  iv, ev = values.end();
241 
242  for( iv=values.begin(); iv!=ev; ++iv )
243  {
244  c += iv->second;
245  medv = iv->first;
246  if( c >= nv2 )
247  break;
248  }
249  if( c != 0 )
250  intensity = medv;
251  else
252  intensity = outBackground;
253 
254  carto::RawConverter<double, ChannelType>().convert(intensity, outValue);
255 
256  }
257  else
258  {
259 
260  outValue = outBackground;
261 
262  }
263 
264 }
265 
266 template < class T >
267 double MedianResampler< T >::getBSplineWeight( int i, double x ) const
268 {
269 
270  x = fabs( x - ( double )i );
271  return ( x > 1.0 ) ? 0.0 : 1.0 - x;
272 
273 }
274 
275 } // namespace aims
276 
277 #endif
double getBSplineWeight(int i, double x) const CARTO_OVERRIDE
Returns .
int getOrder() const CARTO_OVERRIDE
Spline order (1 to 7)
void doResampleChannel(const carto::Volume< ChannelType > &inVolume, const soma::Transformation3d &transform3d, const ChannelType &outBackground, const Point3df &outLocation, ChannelType &outValue, int t) const CARTO_OVERRIDE
B-Spline-based resampling.
void convert(const INP &in, OUTP &out) const
std::vector< int > getSize() const
const T & at(long x, long y=0, long z=0, long t=0) const
Point3dd transform(double x, double y, double z) const