29 #ifndef GF_HELPERFUNCS_HPP
30 #define GF_HELPERFUNCS_HPP
34 #if defined(__APPLE__) || defined(__MACOSX)
35 #include <OpenCL/cl.hpp>
59 for (pow = 1; pow < (uint64_t) num; pow <<= 1) ;
74 void printBuffer (
const char *title, T *ptr, uint32_t width, uint32_t height)
76 std::cout << title << std::endl;
78 for (
int row = 0; row < height; ++row)
80 for (
int col = 0; col < width; ++col)
82 std::cout << std::setw (3 *
sizeof (T)) << +ptr[row * width + col] <<
" ";
84 std::cout << std::endl;
87 std::cout << std::endl;
100 template <
typename T>
101 void printBufferF (
const char *title, T *ptr, uint32_t width, uint32_t height, uint32_t prec)
103 std::ios::fmtflags f (std::cout.flags ());
104 std::cout << title << std::endl;
105 std::cout << std::fixed << std::setprecision (prec);
107 for (
int row = 0; row < height; ++row)
109 for (
int col = 0; col < width; ++col)
111 std::cout << std::setw (5 + prec) << ptr[row * width + col] <<
" ";
113 std::cout << std::endl;
116 std::cout << std::endl;
129 template <
typename T1,
typename T2>
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];
147 template <
typename T>
150 for (uint i = 0; i < pixels; ++i)
153 g[i] = in[i * 3 + 1];
154 b[i] = in[i * 3 + 2];
168 template <
typename T2>
171 for (uint i = 0; i < pixels; ++i)
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;
189 template <
typename T>
192 for (uint i = 0; i < pixels; ++i)
195 out[i * 3 + 1] = g[i];
196 out[i * 3 + 2] = b[i];
210 template <
typename T1>
213 for (uint i = 0; i < pixels; ++i)
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);
231 template <
typename T>
232 void cpuDepthTo3D (T *depth, cl_float4 *pCloud, uint32_t width, uint32_t height,
float f)
234 for (uint row = 0; row < height; ++row)
236 for (uint col = 0; col < width; ++col)
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,
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)
263 for (uint row = 0; row < height; ++row)
265 for (uint col = 0; col < width; ++col)
267 int i = row * width + col;
277 T factor = (sum_ == 0.0) ? 0.0 : 1.0 / sum_;
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 };
299 template <
typename T>
302 for (uint k = 0; k < n; ++k)
304 cl_float *point = pc8d + (k << 3);
306 for (uint j = 0; j < 4; ++j)
308 pc4d[(k << 2) + j] = point[j];
309 rgba[(k << 2) + j] = point[4 + j];
324 template <
typename T>
325 void cpuRGBNorm (T *in, T *out, uint32_t width, uint32_t height)
327 for (uint row = 0; row < height; ++row)
329 for (uint col = 0; col < width; ++col)
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]);
351 template <
typename T>
352 void cpuScan (T *in, T *out, uint32_t width, uint32_t height)
355 for (uint32_t row = 0; row < height; ++row)
356 out[row * width] = in[row * width];
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];
373 template <
typename T>
374 void cpuSAT (T *in, T *out, uint32_t width, uint32_t height)
377 for (uint32_t row = 0; row < height; ++row)
378 out[row * width] = in[row * width];
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];
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];
399 template <
typename T>
402 for (
int row = 0; row < height; ++row)
404 for (
int col = 0; col < width; ++col)
410 for (
int fRow = -radius; fRow <= radius; ++fRow)
412 for (
int fCol = -radius; fCol <= radius; ++fCol)
417 if ((ix >= 0) && (iy >= 0) && (ix < width) && (iy < height))
419 sum += in[iy * width + ix];
425 out[row * width + col] = sum / (T) n;
440 template <
typename T>
441 void cpuMult (T *a, T *b, T *out,
int width,
int height)
443 for (
int row = 0; row < height; ++row)
445 for (
int col = 0; col < width; ++col)
447 int idx = row * width + col;
448 out[idx] = a[idx] * b[idx];
463 template <
typename T>
464 void cpuPown (T *in, T *out,
int width,
int height,
int n)
466 for (
int row = 0; row < height; ++row)
468 for (
int col = 0; col < width; ++col)
470 int idx = row * width + col;
471 out[idx] = pow (in[idx], n);
487 template <
typename T>
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];
500 cpuPown (p, p2, width, height, 2);
503 for (
int row = 0; row < height; ++row)
505 for (
int col = 0; col < width; ++col)
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];
517 for (
int row = 0; row < height; ++row)
519 for (
int col = 0; col < width; ++col)
521 int idx = row * width + col;
522 q[idx] = mean_a[idx] * p[idx] + mean_b[idx];
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