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.
utility.h
Go to the documentation of this file.
1 #ifndef CVD_UTILITY_H
2 #define CVD_UTILITY_H
3 
4 #include "image.h"
5 #include "is_pod.h"
6 #include "pixel_traits.h"
7 #include "convert_pixel_types.h"
8 
9 namespace CVD { //begin namespace
10 
26  template<class S, class T> void copy(const BasicImage<S>& in, BasicImage<T>& out, ImageRef size=ImageRef(-1,-1), ImageRef begin = ImageRef(), ImageRef dst = ImageRef())
27  {
28  if (size.x == -1 && size.y == -1)
29  size = in.size();
30  // FIXME: This should be an exception, but which one do I use?
31  // I refuse to define another "ImageRefNotInImage" in this namespace.
32  if (!(in.in_image(begin) && out.in_image(dst) && in.in_image(begin+size - ImageRef(1,1)) && out.in_image(dst+size - ImageRef(1,1)))){
33  std::cerr << "bad copy: " << in.size() << " " << out.size() << " " << size << " " << begin << " " << dst << std::endl;
34  int *p = 0;
35  *p = 1;
36  }
37  if (in.size() == out.size() && size == in.size() && begin == ImageRef() && dst == ImageRef()) {
39  return;
40  }
41 
42  const S* from = &in[begin];
43  T* to = &out[dst];
44  int i = 0;
45  while (i++<size.y) {
46  Pixel::ConvertPixels<S,T>::convert(from, to, size.x);
47  from += in.size().x;
48  to += out.size().x;
49  }
50  }
51 
52  template <class T, bool pod = Internal::is_POD<T>::is_pod> struct ZeroPixel {
53  static void zero(T& t) {
54  for (unsigned int c=0; c<Pixel::Component<T>::count; c++)
55  Pixel::Component<T>::get(t,c) = 0;
56  }
57  };
58 
59  template <class T> struct ZeroPixel<T,true> {
60  static void zero(T& t) { memset(&t,0,sizeof(T)); }
61  };
62 
63  template <class T, bool pod = Internal::is_POD<T>::is_pod> struct ZeroPixels {
64  static void zero(T* pixels, int count) {
65  if (count) {
66  ZeroPixel<T>::zero(*pixels);
67  std::fill(pixels+1, pixels+count, *pixels);
68  }
69  }
70  };
71 
72  template <class T> struct ZeroPixels<T,true> {
73  static void zero(T* pixels, int count) {
74  memset(pixels, 0, sizeof(T)*count);
75  }
76  };
77 
78 
81  template <class T> inline void zeroPixel(T& pixel) { ZeroPixel<T>::zero(pixel); }
82 
85  template <class T> inline void zeroPixels(T* pixels, int count) { ZeroPixels<T>::zero(pixels, count); }
86 
88  template <class T> void zeroBorders(BasicImage<T>& I)
89  {
90  if (I.size().y == 0)
91  return;
92  zeroPixels(I[0], I.size().x);
93  for (int r=0;r<I.size().y-1; r++)
94  zeroPixels(I[r]+I.size().x-1,2);
95  zeroPixels(I[I.size().y-1], I.size().x);
96  }
97 
103  template<class T> void fillBorders(SubImage<T>& im, const T pix, int w=1)
104  {
105  //Fill the top and bottom
106  for(int n=0; n < w; n++)
107  for(int x=0; x < im.size().x; x++)
108  {
109  im[n][x] = pix;
110  im[im.size().y-1-n][x] = pix;
111  }
112 
113  for(int y=w; y < im.size().y - w; y++)
114  for(int n=0; n < w; n++)
115  {
116  im[y][n] = pix;
117  im[y][im.size().x - 1 - n] = pix;
118  }
119 
120  }
121 
122 
123 
124 
128  template <class A, class B> inline void differences(const A* a, const A* b, B* diff, size_t count)
129  {
130  while (count--)
131  *(diff++) = (B)*(a++) - (B)*(b++);
132  }
133 
137  template <class A, class B, class C> inline void add_multiple_of_sum(const A* a, const A* b, const C& c, B* out, size_t count)
138  {
139  while (count--)
140  *(out++) += (*(a++) + *(b++)) * c;
141  }
142 
146  template <class A, class B, class C> inline void assign_multiple(const A* a, const B& c, C* out, size_t count)
147  {
148  while (count--)
149  *(out++) = static_cast<C>(*(a++) * c);
150  }
151 
155  template <class T> double inner_product(const T* a, const T* b, size_t count) {
156  double dot = 0;
157  while (count--)
158  dot += *(a++) * *(b++);
159  return dot;
160  }
161 
162  template <class R, class D, class T> struct SumSquaredDifferences {
163  static inline R sum_squared_differences(const T* a, const T* b, size_t count) {
164  R ssd = 0;
165  while (count--) {
166  D d = *a++ - *b++;
167  ssd += d*d;
168  }
169  return ssd;
170  }
171  };
172 
173  template <class T1, class T2> inline void square(const T1* in, T2* out, size_t count)
174  {
175  while (count--) {
176  *(out++) = static_cast<T2>(*in * *in);
177  ++in;
178  }
179  }
180 
181  template <class T1, class T2> inline void subtract_square(const T1* in, T2* out, size_t count)
182  {
183  while (count--) {
184  *(out++) -= static_cast<T2>(*in * *in);
185  ++in;
186  }
187  }
188 
192  template <class T> inline double sum_squared_differences(const T* a, const T* b, size_t count) {
194  }
195 
197  template<int bytes> bool is_aligned(const void* ptr);
198  template<> inline bool is_aligned<8>(const void* ptr) { return ((reinterpret_cast<size_t>(ptr)) & 0x7) == 0; }
199  template<> inline bool is_aligned<16>(const void* ptr) { return ((reinterpret_cast<size_t>(ptr)) & 0xF) == 0; }
200 
202  template<int A, class T> inline size_t steps_to_align(const T* ptr)
203  {
204  return is_aligned<A>(ptr) ? 0 : (A-((reinterpret_cast<size_t>(ptr)) & (A-1)))/sizeof(T);
205  }
206 
207  void differences(const byte* a, const byte* b, short* diff, unsigned int size);
208  void differences(const short* a, const short* b, short* diff, unsigned int size);
209 
210 
211  void differences(const float* a, const float* b, float* diff, size_t size);
212  void add_multiple_of_sum(const float* a, const float* b, const float& c, float* out, size_t count);
213  void assign_multiple(const float* a, const float& c, float* out, size_t count);
214  double inner_product(const float* a, const float* b, size_t count);
215  double sum_squared_differences(const float* a, const float* b, size_t count);
216  void square(const float* in, float* out, size_t count);
217  void subtract_square(const float* in, float* out, size_t count);
218 
219  void differences(const int32_t* a, const int32_t* b, int32_t* diff, size_t size);
220  void differences(const double* a, const double* b, double* diff, size_t size);
221  void add_multiple_of_sum(const double* a, const double* b, const double& c, double* out, size_t count);
222  void assign_multiple(const double* a, const double& c, double* out, size_t count);
223  double inner_product(const double* a, const double* b, size_t count);
224  double sum_squared_differences(const double* a, const double* b, size_t count);
225  long long sum_squared_differences(const byte* a, const byte* b, size_t count);
226 
227 
228 }
229 
230 #endif
int totalsize() const
What is the total number of elements in the image (i.e. size().x * size().y), including padding...
Definition: image.h:382
int y
The y co-ordinate.
Definition: image_ref.h:180
int x
The x co-ordinate.
Definition: image_ref.h:179
bool is_aligned< 16 >(const void *ptr)
Definition: utility.h:199
static R sum_squared_differences(const T *a, const T *b, size_t count)
Definition: utility.h:163
static void zero(T &t)
Definition: utility.h:60
static void zero(T &t)
Definition: utility.h:53
bool is_aligned< 8 >(const void *ptr)
Definition: utility.h:198
bool is_aligned(const void *ptr)
Check if the pointer is aligned to the specified byte granularity.
double sum_squared_differences(const T *a, const T *b, size_t count)
Definition: utility.h:192
Definition: abs.h:24
void copy(const BasicImage< S > &in, BasicImage< T > &out, ImageRef size=ImageRef(-1,-1), ImageRef begin=ImageRef(), ImageRef dst=ImageRef())
Definition: utility.h:26
void zeroPixel(T &pixel)
Definition: utility.h:81
size_t steps_to_align(const T *ptr)
Compute the number of pointer increments necessary to yield alignment of A bytes. ...
Definition: utility.h:202
void zeroPixels(T *pixels, int count)
Definition: utility.h:85
void square(const T1 *in, T2 *out, size_t count)
Definition: utility.h:173
void zeroBorders(BasicImage< T > &I)
Set the one-pixel border (top, bottom, sides) of an image to zero values.
Definition: utility.h:88
ImageRef size() const
What is the size of this image?
Definition: image.h:370
const T * data() const
Returns the raw image data.
Definition: image.h:326
double inner_product(const T *a, const T *b, size_t count)
Definition: utility.h:155
unsigned char byte
Definition: byte.h:28
void add_multiple_of_sum(const A *a, const A *b, const C &c, B *out, size_t count)
Definition: utility.h:137
void assign_multiple(const A *a, const B &c, C *out, size_t count)
Definition: utility.h:146
static void zero(T *pixels, int count)
Definition: utility.h:73
void differences(const A *a, const A *b, B *diff, size_t count)
Definition: utility.h:128
void fillBorders(SubImage< T > &im, const T pix, int w=1)
Definition: utility.h:103
bool in_image(const ImageRef &ir) const
Definition: image.h:263
static void zero(T *pixels, int count)
Definition: utility.h:64
void subtract_square(const T1 *in, T2 *out, size_t count)
Definition: utility.h:181
static void convert(const From *from, To *to, size_t count)