SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
image_interpolate.h
Go to the documentation of this file.
1 #ifndef CVD_IMAGE_INTERPOLATE_H
2 #define CVD_IMAGE_INTERPOLATE_H
3 
4 #include "TooN.h"
5 #include "pixel_operations.h"
6 #include "rgb_components.h"
7 #include "vector_image_ref.h"
8 #include <math.h>
9 
10 namespace CVD
11 {
15  namespace Interpolate
16  {
24 
42  class Bilinear{};
43 
56  class Bicubic{};
57  };
58 
59  #ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
60  template<class I, class P> class image_interpolate
66  {
67  public:
71 
74  bool in_image(const TooN::Vector<2>& pos);
75 
79  float_type operator[](const TooN::Vector<2>& pos);
80 
82  TooN::Vector<2> min();
84  TooN::Vector<2> max();
85  };
86  #endif
87 
88 
89 
90 
91  template<class I, class C> class image_interpolate;
92 
93  //Zero order (nearest neighbour)
94 
95  template<class C> class image_interpolate<Interpolate::NearestNeighbour, C>
96  {
97  private:
98  const BasicImage<C>* im;
99 
100  int round(double d)
101  {
102  if(d < 0)
103  return (int)ceil(d - .5);
104  else
105  return (int)floor(d + .5);
106  }
107 
109  {
110  return ImageRef(round(v[0]), round(v[1]));
111  }
112 
114 
115  public:
117  :im(&i)
118  {}
119 
120  bool in_image(const TooN::Vector<2>& pos)
121  {
122  return im->in_image(to_ir(pos));
123  }
124 
126  {
127  return (*im)[to_ir(pos)];
128  }
129 
131  {
132  return TooN::makeVector( 0, 0);
133  }
134 
136  {
137  return vec(im->size());
138  }
139 
140 
141  };
142 
143  template<class T> class image_interpolate<Interpolate::Bilinear, T>
144  {
145  private:
147 
149  {
150  return TooN::makeVector( ::floor(v[0]), ::floor(v[1]));
151  }
152 
154  {
155  return TooN::makeVector( ::ceil(v[0]), ::ceil(v[1]));
156  }
157 
159 
160  public:
162  :im(&i)
163  {}
164 
165  bool in_image(const TooN::Vector<2>& pos)
166  {
167  return im->in_image(ir(floor(pos))) && im->in_image(ir(ceil(pos)));
168  }
169 
171  {
172  TooN::Vector<2> delta = pos - floor(pos);
173 
174  ImageRef p = ir(floor(pos));
175 
176  double x = delta[0];
177  double y = delta[1];
178 
179  FT ret;
180 
181  for(unsigned int i=0; i < Pixel::Component<T>::count; i++)
182  {
183  float a, b=0, c=0, d=0;
184 
185  a = Pixel::Component<T>::get((*im)[p + ImageRef(0,0)], i) * (1-x) * (1-y);
186 
187  if(x != 0)
188  b = Pixel::Component<T>::get((*im)[p + ImageRef(1,0)], i) * x * (1-y);
189 
190  if(y != 0)
191  c = Pixel::Component<T>::get((*im)[p + ImageRef(0,1)], i) * (1-x) * y;
192 
193  if(x !=0 && y != 0)
194  d = Pixel::Component<T>::get((*im)[p + ImageRef(1,1)], i) * x * y;
195 
196  Pixel::Component<FT>::get(ret, i) = a + b + c + d;
197  }
198 
199  return ret;
200  }
201 
203  {
204  return TooN::makeVector( 0, 0);
205  }
206 
208  {
209  return vec(im->size());
210  }
211 
212  };
213 
214 
215  template<class T> class image_interpolate<Interpolate::Bicubic, T>
216  {
217  private:
219 
220  float p(float f)
221  {
222  return f <0 ? 0 : f;
223  }
224 
225  float r(float x)
226  {
227  return ( pow(p(x+2), 3) - 4 * pow(p(x+1),3) + 6 * pow(p(x), 3) - 4* pow(p(x-1),3))/6;
228  }
229 
231 
232  public:
234  :im(&i)
235  {}
236 
237  bool in_image(const TooN::Vector<2>& pos)
238  {
239  return pos[0] >= 1 && pos[1] >=1 && pos[0] < im->size().x-2 && pos[1] < im->size().y - 2;
240  }
241 
243  {
244  int x = (int)floor(pos[0]);
245  int y = (int)floor(pos[1]);
246  float dx = pos[0] - x;
247  float dy = pos[1] - y;
248 
249  //Algorithm as described in http://astronomy.swin.edu.au/~pbourke/colour/bicubic/
250  FT ret;
251  for(unsigned int i=0; i < Pixel::Component<T>::count; i++)
252  {
253  float s=0;
254 
255  for(int m = -1; m < 3; m++)
256  for(int n = -1; n < 3; n++)
257  s += Pixel::Component<T>::get((*im)[y+n][x+m], i) * r(m - dx) * r(dy-n);
258 
259  Pixel::Component<FT>::get(ret, i)= s;
260  }
261 
262  return ret;
263  }
264 
266  {
267  return TooN::makeVector( 1, 1);
268  }
269 
271  {
272  return TooN::makeVector( im->size().x - 2, im->size().y - 2);
273  }
274 
275  };
276 
277 }
278 
279 #endif
static const P & get(const P &pixel, size_t)
TooN::Vector< 2 > vec(const ImageRef &ir)
Definition: abs.h:24
TooN::Vector< 2 > ceil(const TooN::Vector< 2 > &v)
ImageRef size() const
What is the size of this image?
Definition: image.h:370
ImageRef ir(const TooN::Vector< 2 > &v)
Vector< 1 > makeVector(double x1)
Definition: make_vector.hh:4
TooN::Vector< 2 > floor(const TooN::Vector< 2 > &v)
bool in_image(const TooN::Vector< 2 > &pos)
bool in_image(const ImageRef &ir) const
Definition: image.h:263