|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2014, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <af/dim4.hpp> |
| 11 | +#include <af/defines.h> |
| 12 | +#include <af/image.h> |
| 13 | +#include <err_common.hpp> |
| 14 | +#include <backend.hpp> |
| 15 | +#include <handle.hpp> |
| 16 | +#include <reduce.hpp> |
| 17 | +#include <arith.hpp> |
| 18 | +#include <math.hpp> |
| 19 | +#include <unary.hpp> |
| 20 | +#include <iota.hpp> |
| 21 | +#include <reduce.hpp> |
| 22 | +#include <transpose.hpp> |
| 23 | + |
| 24 | +using namespace detail; |
| 25 | + |
| 26 | +template<typename T> |
| 27 | +Array<T> gaussianKernel(const int rows, const int cols, const double sigma_r, const double sigma_c) |
| 28 | +{ |
| 29 | + const dim4 odims = dim4(rows, cols); |
| 30 | + double sigma = 0; |
| 31 | + |
| 32 | + Array<T> tmp = createValueArray<T>(odims, scalar<T>(0)); |
| 33 | + Array<T> half = createValueArray<T>(odims, 0.5); |
| 34 | + Array<T> zero = createValueArray<T>(odims, scalar<T>(0)); |
| 35 | + |
| 36 | + if (cols > 1) { |
| 37 | + Array<T> wt = iota<T>(dim4(cols, rows), 0); |
| 38 | + Array<T> w = transpose<T>(wt, false); |
| 39 | + |
| 40 | + Array<T> c = createValueArray<T>(odims, scalar<T>((double)(cols - 1) / 2.0)); |
| 41 | + w = arithOp<T, af_sub_t>(w, c, odims); |
| 42 | + |
| 43 | + sigma = sigma_c > 0 ? sigma_c : 0.25 * cols; |
| 44 | + Array<T> sig = createValueArray<T>(odims, sigma); |
| 45 | + w = arithOp<T, af_div_t>(w, sig, odims); |
| 46 | + |
| 47 | + w = arithOp<T, af_mul_t>(w, w, odims); |
| 48 | + tmp = arithOp<T, af_add_t>(w, tmp, odims); |
| 49 | + } |
| 50 | + |
| 51 | + if (rows > 1) { |
| 52 | + Array<T> w = iota<T>(dim4(rows, cols), 0); |
| 53 | + |
| 54 | + Array<T> r = createValueArray<T>(odims, scalar<T>((double)(rows - 1) / 2.0)); |
| 55 | + w = arithOp<T, af_sub_t>(w, r, odims); |
| 56 | + |
| 57 | + sigma = sigma_r > 0 ? sigma_r : 0.25 * rows; |
| 58 | + Array<T> sig = createValueArray<T>(odims, sigma); |
| 59 | + |
| 60 | + w = arithOp<T, af_div_t>(w, sig, odims); |
| 61 | + w = arithOp<T, af_mul_t>(w, w, odims); |
| 62 | + tmp = arithOp<T, af_add_t>(w, tmp, odims); |
| 63 | + } |
| 64 | + |
| 65 | + tmp = arithOp<T, af_mul_t>(half, tmp, odims); |
| 66 | + tmp = arithOp<T, af_sub_t>(zero, tmp, odims); |
| 67 | + tmp = unaryOp<T, af_exp_t>(tmp); |
| 68 | + |
| 69 | + // Use this instead of (2 * pi * sig^2); |
| 70 | + // This ensures the window adds up to 1 |
| 71 | + T norm_factor = reduce_all<af_add_t, T, T>(tmp); |
| 72 | + |
| 73 | + Array<T> norm = createValueArray(odims, norm_factor); |
| 74 | + Array<T> res = arithOp<T, af_div_t>(tmp, norm, odims); |
| 75 | + |
| 76 | + return res; |
| 77 | +} |
| 78 | + |
| 79 | +af_err af_gaussian_kernel(af_array *out, |
| 80 | + const int rows, const int cols, |
| 81 | + const double sigma_r, const double sigma_c) |
| 82 | +{ |
| 83 | + af_array res; |
| 84 | + try { |
| 85 | + res = getHandle<float>(gaussianKernel<float>(rows, cols, sigma_r, sigma_c)); |
| 86 | + std::swap(*out, res); |
| 87 | + }CATCHALL; |
| 88 | + return AF_SUCCESS; |
| 89 | +} |
0 commit comments