-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathanisotropic_diffusion.cpp
More file actions
104 lines (86 loc) · 3.13 KB
/
anisotropic_diffusion.cpp
File metadata and controls
104 lines (86 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************
* Copyright (c) 2017, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <anisotropic_diffusion.hpp>
#include <arith.hpp>
#include <backend.hpp>
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <copy.hpp>
#include <gradient.hpp>
#include <handle.hpp>
#include <reduce.hpp>
#include <af/dim4.hpp>
#include <af/image.h>
#include <type_traits>
using af::dim4;
using arrayfire::common::cast;
using detail::arithOp;
using detail::Array;
using detail::createEmptyArray;
using detail::getScalar;
using detail::gradient;
using detail::reduce_all;
template<typename T>
af_array diffusion(const Array<float>& in, const float dt, const float K,
const unsigned iterations, const af_flux_function fftype,
const af::diffusionEq eq) {
auto out = copyArray(in);
auto dims = out.dims();
auto g0 = createEmptyArray<float>(dims);
auto g1 = createEmptyArray<float>(dims);
float cnst =
-2.0f * K * K / dims.elements(); // NOLINT(readability-magic-numbers)
for (unsigned i = 0; i < iterations; ++i) {
gradient<float>(g0, g1, out);
auto g0Sqr = arithOp<float, af_mul_t>(g0, g0, dims);
auto g1Sqr = arithOp<float, af_mul_t>(g1, g1, dims);
auto sumd = arithOp<float, af_add_t>(g0Sqr, g1Sqr, dims);
float avg =
getScalar<float>(reduce_all<af_add_t, float, float>(sumd, true, 0));
anisotropicDiffusion(out, dt, 1.0f / (cnst * avg), fftype, eq);
}
return getHandle(cast<T, float>(out));
}
af_err af_anisotropic_diffusion(af_array* out, const af_array in,
const float dt, const float K,
const unsigned iterations,
const af_flux_function fftype,
const af_diffusion_eq eq) {
try {
const ArrayInfo& info = getInfo(in);
const af::dim4& inputDimensions = info.dims();
const af_dtype inputType = info.getType();
const unsigned inputNumDims = inputDimensions.ndims();
DIM_ASSERT(1, (inputNumDims >= 2));
ARG_ASSERT(3, (K > 0 || K < 0));
ARG_ASSERT(4, (iterations > 0));
const af_flux_function F =
(fftype == AF_FLUX_DEFAULT ? AF_FLUX_EXPONENTIAL : fftype);
auto input = castArray<float>(in);
af_array output = nullptr;
switch (inputType) {
case f64:
output = diffusion<double>(input, dt, K, iterations, F, eq);
break;
case f32:
case s32:
case u32:
case s16:
case u16:
case s8:
case u8:
output = diffusion<float>(input, dt, K, iterations, F, eq);
break;
default: TYPE_ERROR(1, inputType);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}