GuidedFilter  1.2.0
 Hosted by GitHub
helper_funcs.hpp
Go to the documentation of this file.
1 
29 #ifndef GF_HELPERFUNCS_HPP
30 #define GF_HELPERFUNCS_HPP
31 
32 #include <cassert>
33 
34 #if defined(__APPLE__) || defined(__MACOSX)
35 #include <OpenCL/cl.hpp>
36 #else
37 #include <CL/cl.hpp>
38 #endif
39 
40 
41 namespace GF
42 {
43 
45  bool setProfilingFlag (int argc, char **argv);
46 
47 
53  template <typename T>
54  uint64_t nextPow2 (T num)
55  {
56  assert (num >= 0);
57 
58  uint64_t pow;
59  for (pow = 1; pow < (uint64_t) num; pow <<= 1) ;
60 
61  return pow;
62  }
63 
64 
73  template <typename T>
74  void printBuffer (const char *title, T *ptr, uint32_t width, uint32_t height)
75  {
76  std::cout << title << std::endl;
77 
78  for (int row = 0; row < height; ++row)
79  {
80  for (int col = 0; col < width; ++col)
81  {
82  std::cout << std::setw (3 * sizeof (T)) << +ptr[row * width + col] << " ";
83  }
84  std::cout << std::endl;
85  }
86 
87  std::cout << std::endl;
88  }
89 
90 
100  template <typename T>
101  void printBufferF (const char *title, T *ptr, uint32_t width, uint32_t height, uint32_t prec)
102  {
103  std::ios::fmtflags f (std::cout.flags ());
104  std::cout << title << std::endl;
105  std::cout << std::fixed << std::setprecision (prec);
106 
107  for (int row = 0; row < height; ++row)
108  {
109  for (int col = 0; col < width; ++col)
110  {
111  std::cout << std::setw (5 + prec) << ptr[row * width + col] << " ";
112  }
113  std::cout << std::endl;
114  }
115 
116  std::cout << std::endl;
117  std::cout.flags (f);
118  }
119 
120 
129  template <typename T1, typename T2>
130  void cpuTranspose (T1 *in, T2 *out, uint32_t width, uint32_t height)
131  {
132  for (uint row = 0; row < height; ++row)
133  for (uint col = 0; col < width; ++col)
134  out[col * height + row] = in[row * width + col];
135  }
136 
137 
147  template <typename T>
148  void cpuSeparateRGB (T *in, T *r, T *g, T *b, uint32_t pixels)
149  {
150  for (uint i = 0; i < pixels; ++i)
151  {
152  r[i] = in[i * 3];
153  g[i] = in[i * 3 + 1];
154  b[i] = in[i * 3 + 2];
155  }
156  }
157 
158 
168  template <typename T2>
169  void cpuSeparateRGB_N_Norm (unsigned char *in, T2 *r, T2 *g, T2 *b, uint32_t pixels)
170  {
171  for (uint i = 0; i < pixels; ++i)
172  {
173  r[i] = (T2) in[i * 3] / 255.f;
174  g[i] = (T2) in[i * 3 + 1] / 255.f;
175  b[i] = (T2) in[i * 3 + 2] / 255.f;
176  }
177  }
178 
179 
189  template <typename T>
190  void cpuCombineRGB (T *r, T *g, T *b, T *out, uint32_t pixels)
191  {
192  for (uint i = 0; i < pixels; ++i)
193  {
194  out[i * 3] = r[i];
195  out[i * 3 + 1] = g[i];
196  out[i * 3 + 2] = b[i];
197  }
198  }
199 
200 
210  template <typename T1>
211  void cpuTranspose_N_Scale (T1 *r, T1 *g, T1 *b, unsigned char *out, uint32_t pixels)
212  {
213  for (uint i = 0; i < pixels; ++i)
214  {
215  out[i * 3] = (unsigned char) (r[i] * 255.f);
216  out[i * 3 + 1] = (unsigned char) (g[i] * 255.f);
217  out[i * 3 + 2] = (unsigned char) (b[i] * 255.f);
218  }
219  }
220 
221 
231  template <typename T>
232  void cpuDepthTo3D (T *depth, cl_float4 *pCloud, uint32_t width, uint32_t height, float f)
233  {
234  for (uint row = 0; row < height; ++row)
235  {
236  for (uint col = 0; col < width; ++col)
237  {
238  T d = depth[row * width + col];
239  pCloud[row * width + col] = { (col - (width - 1) / 2.f) * (float) d / f,
240  (row - (height - 1) / 2.f) * (float) d / f,
241  (float) d, 1.f };
242  }
243  }
244  }
245 
246 
260  template <typename T>
261  void cpuRGBDTo8D (T *D, T *R, T *G, T *B, cl_float8 *points, uint32_t width, uint32_t height, float f, bool rgbNorm)
262  {
263  for (uint row = 0; row < height; ++row)
264  {
265  for (uint col = 0; col < width; ++col)
266  {
267  int i = row * width + col;
268 
269  T d = D[i];
270  T r = R[i];
271  T g = G[i];
272  T b = B[i];
273 
274  if (rgbNorm)
275  {
276  T sum_ = r + g + b;
277  T factor = (sum_ == 0.0) ? 0.0 : 1.0 / sum_;
278  r *= factor;
279  g *= factor;
280  b *= factor;
281  }
282 
283  points[i] = { (col - (width - 1) / 2.f) * (float) d / f,
284  (row - (height - 1) / 2.f) * (float) d / f,
285  (float) d, 1.f, r, g, b, 1.f };
286  }
287  }
288  }
289 
290 
299  template <typename T>
300  void cpuSplitPC8D (T *pc8d, T *pc4d, T *rgba, uint32_t n)
301  {
302  for (uint k = 0; k < n; ++k)
303  {
304  cl_float *point = pc8d + (k << 3);
305 
306  for (uint j = 0; j < 4; ++j)
307  {
308  pc4d[(k << 2) + j] = point[j];
309  rgba[(k << 2) + j] = point[4 + j];
310  }
311  }
312  }
313 
314 
324  template <typename T>
325  void cpuRGBNorm (T *in, T *out, uint32_t width, uint32_t height)
326  {
327  for (uint row = 0; row < height; ++row)
328  {
329  for (uint col = 0; col < width; ++col)
330  {
331  uint rank = (row * width + col) * 3;
332  T sum_ = in[rank] + in[rank+1] + in[rank+2];
333  T factor = (sum_ == 0) ? 0.0 : 1.0 / sum_;
334  out[rank] = (T) (factor * in[rank]);
335  out[rank+1] = (T) (factor * in[rank+1]);
336  out[rank+2] = (T) (factor * in[rank+2]);
337  }
338  }
339  }
340 
341 
351  template <typename T>
352  void cpuScan (T *in, T *out, uint32_t width, uint32_t height)
353  {
354  // Initialize the first element of each row
355  for (uint32_t row = 0; row < height; ++row)
356  out[row * width] = in[row * width];
357  // Perform the scan
358  for (uint32_t row = 0; row < height; ++row)
359  for (uint32_t col = 1; col < width; ++col)
360  out[row * width + col] = out[row * width + col - 1] + in[row * width + col];
361  }
362 
363 
373  template <typename T>
374  void cpuSAT (T *in, T *out, uint32_t width, uint32_t height)
375  {
376  // Initialize the first element of each row
377  for (uint32_t row = 0; row < height; ++row)
378  out[row * width] = in[row * width];
379  // Perform a scan on the rows
380  for (uint32_t row = 0; row < height; ++row)
381  for (uint32_t col = 1; col < width; ++col)
382  out[row * width + col] = out[row * width + col - 1] + in[row * width + col];
383  // Perform a scan on the columns
384  for (uint32_t col = 0; col < width; ++col)
385  for (uint32_t row = 1; row < height; ++row)
386  out[row * width + col] = out[(row - 1) * width + col] + out[row * width + col];
387  }
388 
389 
399  template <typename T>
400  void cpuBoxFilter (T *in, T *out, int width, int height, int radius)
401  {
402  for (int row = 0; row < height; ++row)
403  {
404  for (int col = 0; col < width; ++col)
405  {
406  T sum = 0.f;
407  int n = 0;
408  int ix, iy;
409 
410  for (int fRow = -radius; fRow <= radius; ++fRow)
411  {
412  for (int fCol = -radius; fCol <= radius; ++fCol)
413  {
414  ix = col + fCol;
415  iy = row + fRow;
416 
417  if ((ix >= 0) && (iy >= 0) && (ix < width) && (iy < height))
418  {
419  sum += in[iy * width + ix];
420  n++;
421  }
422  }
423  }
424 
425  out[row * width + col] = sum / (T) n;
426  }
427  }
428  }
429 
430 
440  template <typename T>
441  void cpuMult (T *a, T *b, T *out, int width, int height)
442  {
443  for (int row = 0; row < height; ++row)
444  {
445  for (int col = 0; col < width; ++col)
446  {
447  int idx = row * width + col;
448  out[idx] = a[idx] * b[idx];
449  }
450  }
451  }
452 
453 
463  template <typename T>
464  void cpuPown (T *in, T *out, int width, int height, int n)
465  {
466  for (int row = 0; row < height; ++row)
467  {
468  for (int col = 0; col < width; ++col)
469  {
470  int idx = row * width + col;
471  out[idx] = pow (in[idx], n);
472  }
473  }
474  }
475 
476 
487  template <typename T>
488  void cpuGuidedFilter (T *p, T *q, int width, int height, int radius, float eps)
489  {
490  int pixels = width * height;
491  T *mean_p = new T[pixels];
492  T *p2 = new T[pixels];
493  T *mean_p2 = new T[pixels];
494  T *a = new T[pixels];
495  T *b = new T[pixels];
496  T *mean_a = new T[pixels];
497  T *mean_b = new T[pixels];
498 
499  cpuBoxFilter (p, mean_p, width, height, radius);
500  cpuPown (p, p2, width, height, 2);
501  cpuBoxFilter (p2, mean_p2, width, height, radius);
502 
503  for (int row = 0; row < height; ++row)
504  {
505  for (int col = 0; col < width; ++col)
506  {
507  int idx = row * width + col;
508  T var = mean_p2[idx] - mean_p[idx] * mean_p[idx];
509  a[idx] = var / (var + eps);
510  b[idx] = (1 - a[idx]) * mean_p[idx];
511  }
512  }
513 
514  cpuBoxFilter (a, mean_a, width, height, radius);
515  cpuBoxFilter (b, mean_b, width, height, radius);
516 
517  for (int row = 0; row < height; ++row)
518  {
519  for (int col = 0; col < width; ++col)
520  {
521  int idx = row * width + col;
522  q[idx] = mean_a[idx] * p[idx] + mean_b[idx];
523  }
524  }
525  }
526 
527 }
528 
529 #endif // GF_HELPERFUNCS_HPP
void cpuScan(T *in, T *out, uint32_t width, uint32_t height)
Performs a scan operation.
Definition: helper_funcs.hpp:352
void cpuSeparateRGB_N_Norm(unsigned char *in, T2 *r, T2 *g, T2 *b, uint32_t pixels)
Performs a matrix transposition.
Definition: helper_funcs.hpp:169
void cpuRGBNorm(T *in, T *out, uint32_t width, uint32_t height)
Performs RGB color normalization.
Definition: helper_funcs.hpp:325
void cpuGuidedFilter(T *p, T *q, int width, int height, int radius, float eps)
Performs edge preserving smoothing on an array.
Definition: helper_funcs.hpp:488
void cpuSplitPC8D(T *pc8d, T *pc4d, T *rgba, uint32_t n)
Splits an 8-D point cloud into 4-D geometry points and RGBA color points.
Definition: helper_funcs.hpp:300
void cpuSAT(T *in, T *out, uint32_t width, uint32_t height)
Constructs a Summed Area Table (SAT).
Definition: helper_funcs.hpp:374
void cpuBoxFilter(T *in, T *out, int width, int height, int radius)
Performs a blurring effect (mean filtering) on an array.
Definition: helper_funcs.hpp:400
void cpuRGBDTo8D(T *D, T *R, T *G, T *B, cl_float8 *points, uint32_t width, uint32_t height, float f, bool rgbNorm)
Fuses geometry and color values into 8D feature points.
Definition: helper_funcs.hpp:261
void cpuPown(T *in, T *out, int width, int height, int n)
Performs a raise to an integer power.
Definition: helper_funcs.hpp:464
void cpuMult(T *a, T *b, T *out, int width, int height)
Performs an element-wise array multiplication.
Definition: helper_funcs.hpp:441
uint64_t nextPow2(T num)
Returns the first power of 2 greater than or equal to the input.
Definition: helper_funcs.hpp:54
void cpuDepthTo3D(T *depth, cl_float4 *pCloud, uint32_t width, uint32_t height, float f)
Transforms a depth image to a point cloud.
Definition: helper_funcs.hpp:232
void cpuCombineRGB(T *r, T *g, T *b, T *out, uint32_t pixels)
Performs a matrix transposition.
Definition: helper_funcs.hpp:190
void printBuffer(const char *title, T *ptr, uint32_t width, uint32_t height)
Prints an array of an integer type to standard output.
Definition: helper_funcs.hpp:74
kernel void rgbNorm(global float *in, global float *out)
Performs RGB color normalization.
Definition: imageSupport_kernels.cl:287
void printBufferF(const char *title, T *ptr, uint32_t width, uint32_t height, uint32_t prec)
Prints an array of floating-point type to standard output.
Definition: helper_funcs.hpp:101
Definition: helper_funcs.hpp:41
void cpuSeparateRGB(T *in, T *r, T *g, T *b, uint32_t pixels)
Performs a matrix transposition.
Definition: helper_funcs.hpp:148
void cpuTranspose_N_Scale(T1 *r, T1 *g, T1 *b, unsigned char *out, uint32_t pixels)
Performs a matrix transpose.
Definition: helper_funcs.hpp:211
bool setProfilingFlag(int argc, char **argv)
Checks the command line arguments for the profiling flag, --profiling.
Definition: helper_funcs.cpp:66
void cpuTranspose(T1 *in, T2 *out, uint32_t width, uint32_t height)
Performs a matrix transposition.
Definition: helper_funcs.hpp:130