aimsalgo  5.1.2
Neuroimaging image processing
majoritylabelresampler_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_MAJORITYLABELRESAMPLER_D_H
36 #define AIMS_RESAMPLING_MAJORITYLABELRESAMPLER_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 101;
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 
124  if ( dimz == 1 )
125  {
126 
127  //counting contributions
128  pj = i;
129  pi = pj + (size_t)(foldY0);
130  if( weightY0 != 0 )
131  {
132  if( weightX0 != 0 )
133  values[*( pi + (size_t)(foldX0) )]++;
134  if( weightX1 != 0 )
135  values[*( pi + size_t(foldX1) )]++;
136  }
137  if( weightY1 != 0 )
138  {
139  pi = pj + foldY1;
140  if( weightX0 != 0 && weightY1 != 0 )
141  values[*( pi + (size_t)(foldX0) )]++;
142  if( weightX1 != 0 && weightY1 != 0 )
143  values[*( pi + (size_t)(foldX1) )]++;
144  }
145 
146  }
147  else
148  {
149 
150  // first z contribution
151  int z = static_cast<long>(floor(normalizedInLocation[2]));
152  if( getBSplineWeight( z, normalizedInLocation[2] ) != 0 )
153  {
154  pj = i + (size_t)(this->getFold( z, dimz )) * dimx * dimy;
155  pi = pj + (size_t)(foldY0);
156 
157  if( weightY0 != 0 )
158  {
159  if( weightX0 != 0 )
160  values[*( pi + (size_t)(foldX0) )]++;
161  if( weightX1 != 0 )
162  values[*( pi + size_t(foldX1) )]++;
163  }
164  if( weightY1 != 0 )
165  {
166  pi = pj + foldY1;
167  if( weightX0 != 0 && weightY1 != 0 )
168  values[*( pi + (size_t)(foldX0) )]++;
169  if( weightX1 != 0 && weightY1 != 0 )
170  values[*( pi + (size_t)(foldX1) )]++;
171  }
172 
173  }
174  // second z contribution
175  ++ z;
176  if( getBSplineWeight( z, normalizedInLocation[2] ) != 0 )
177  {
178  pj = i + (size_t)(this->getFold( z, dimz )) * dimx *
179  dimy;
180  pi = pj + (size_t)(foldY0);
181 
182  if( weightY0 != 0 )
183  {
184  if( weightX0 != 0 )
185  values[*( pi + (size_t)(foldX0) )]++;
186  if( weightX1 != 0 )
187  values[*( pi + size_t(foldX1) )]++;
188  }
189  if( weightY1 != 0 )
190  {
191  pi = pj + foldY1;
192  if( weightX0 != 0 && weightY1 != 0 )
193  values[*( pi + (size_t)(foldX0) )]++;
194  if( weightX1 != 0 && weightY1 != 0 )
195  values[*( pi + (size_t)(foldX1) )]++;
196  }
197  }
198  }
199 
200  unsigned long c = 0;
201  ChannelType maxv = 0;
202  typename std::map<ChannelType, unsigned long>::const_iterator
203  iv, ev = values.end();
204 
205  for( iv=values.begin(); iv!=ev; ++iv )
206  if( iv->second > c )
207  {
208  maxv = iv->first;
209  c = iv->second;
210  }
211  if( c != 0 )
212  intensity = maxv;
213  else
214  intensity = outBackground;
215 
216  carto::RawConverter<double, ChannelType>().convert(intensity, outValue);
217 
218  }
219  else
220  {
221 
222  outValue = outBackground;
223 
224  }
225 
226 }
227 
228 template < class T >
229 double MajorityLabelResampler< T >::getBSplineWeight( int i, double x ) const
230 {
231 
232  x = fabs( x - ( double )i );
233  return ( x > 1.0 ) ? 0.0 : 1.0 - x;
234 
235 }
236 
237 } // namespace aims
238 
239 #endif
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
double getBSplineWeight(int i, double x) const CARTO_OVERRIDE
Returns .
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