forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.cpp
More file actions
196 lines (166 loc) · 6.34 KB
/
filters.cpp
File metadata and controls
196 lines (166 loc) · 6.34 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************
* Copyright (c) 2014, 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 <backend.hpp>
#include <common/err_common.hpp>
#include <handle.hpp>
#include <medfilt.hpp>
#include <af/data.h>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/image.h>
#include <af/signal.h>
using af::dim4;
using detail::uchar;
using detail::uint;
using detail::ushort;
af_err af_medfilt(af_array *out, const af_array in, const dim_t wind_length,
const dim_t wind_width, const af_border_type edge_pad) {
return af_medfilt2(out, in, wind_length, wind_width, edge_pad);
}
template<typename T>
static af_array medfilt1(af_array const &in, dim_t w_wid,
af_border_type edge_pad) {
return getHandle<T>(
medfilt1<T>(getArray<T>(in), static_cast<int>(w_wid), edge_pad));
}
af_err af_medfilt1(af_array *out, const af_array in, const dim_t wind_width,
const af_border_type edge_pad) {
try {
ARG_ASSERT(2, (wind_width > 0));
ARG_ASSERT(4, (edge_pad >= AF_PAD_ZERO && edge_pad <= AF_PAD_SYM));
const ArrayInfo &info = getInfo(in);
af::dim4 dims = info.dims();
dim_t input_ndims = dims.ndims();
DIM_ASSERT(1, (input_ndims >= 1));
if (wind_width == 1) {
*out = retain(in);
return AF_SUCCESS;
}
af_array output = nullptr;
af_dtype type = info.getType();
switch (type) {
case f32: output = medfilt1<float>(in, wind_width, edge_pad); break;
case f64:
output = medfilt1<double>(in, wind_width, edge_pad);
break;
case b8: output = medfilt1<char>(in, wind_width, edge_pad); break;
case s32: output = medfilt1<int>(in, wind_width, edge_pad); break;
case u32: output = medfilt1<uint>(in, wind_width, edge_pad); break;
case s16: output = medfilt1<short>(in, wind_width, edge_pad); break;
case u16:
output = medfilt1<ushort>(in, wind_width, edge_pad);
break;
case u8: output = medfilt1<uchar>(in, wind_width, edge_pad); break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}
template<typename T>
inline af_array medfilt2(af_array const &in, dim_t w_len, dim_t w_wid,
af_border_type edge_pad) {
return getHandle(medfilt2<T>(getArray<T>(in), static_cast<int>(w_len),
static_cast<int>(w_wid), edge_pad));
}
af_err af_medfilt2(af_array *out, const af_array in, const dim_t wind_length,
const dim_t wind_width, const af_border_type edge_pad) {
try {
ARG_ASSERT(2, (wind_length == wind_width));
ARG_ASSERT(2, (wind_length > 0));
ARG_ASSERT(3, (wind_width > 0));
ARG_ASSERT(4, (edge_pad >= AF_PAD_ZERO && edge_pad <= AF_PAD_SYM));
const ArrayInfo &info = getInfo(in);
af::dim4 dims = info.dims();
if (info.isColumn()) {
return af_medfilt1(out, in, wind_width, edge_pad);
}
dim_t input_ndims = dims.ndims();
DIM_ASSERT(1, (input_ndims >= 2));
if (wind_length == 1) {
*out = retain(in);
return AF_SUCCESS;
}
af_array output = nullptr;
af_dtype type = info.getType();
switch (type) {
case f32:
output = medfilt2<float>(in, wind_length, wind_width, edge_pad);
break;
case f64:
output =
medfilt2<double>(in, wind_length, wind_width, edge_pad);
break;
case b8:
output = medfilt2<char>(in, wind_length, wind_width, edge_pad);
break;
case s32:
output = medfilt2<int>(in, wind_length, wind_width, edge_pad);
break;
case u32:
output = medfilt2<uint>(in, wind_length, wind_width, edge_pad);
break;
case s16:
output = medfilt2<short>(in, wind_length, wind_width, edge_pad);
break;
case u16:
output =
medfilt2<ushort>(in, wind_length, wind_width, edge_pad);
break;
case u8:
output = medfilt2<uchar>(in, wind_length, wind_width, edge_pad);
break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_minfilt(af_array *out, const af_array in, const dim_t wind_length,
const dim_t wind_width, const af_border_type edge_pad) {
try {
ARG_ASSERT(2, (wind_length == wind_width));
ARG_ASSERT(2, (wind_length > 0));
ARG_ASSERT(3, (wind_width > 0));
ARG_ASSERT(4, (edge_pad == AF_PAD_ZERO));
const ArrayInfo &info = getInfo(in);
af::dim4 dims = info.dims();
dim_t input_ndims = dims.ndims();
DIM_ASSERT(1, (input_ndims >= 2));
af_array mask;
dim_t wdims[] = {wind_length, wind_width};
AF_CHECK(af_constant(&mask, 1, 2, wdims, info.getType()));
AF_CHECK(af_erode(out, in, mask));
AF_CHECK(af_release_array(mask));
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_maxfilt(af_array *out, const af_array in, const dim_t wind_length,
const dim_t wind_width, const af_border_type edge_pad) {
try {
ARG_ASSERT(2, (wind_length == wind_width));
ARG_ASSERT(2, (wind_length > 0));
ARG_ASSERT(3, (wind_width > 0));
ARG_ASSERT(4, (edge_pad == AF_PAD_ZERO));
const ArrayInfo &info = getInfo(in);
af::dim4 dims = info.dims();
dim_t input_ndims = dims.ndims();
DIM_ASSERT(1, (input_ndims >= 2));
af_array mask;
dim_t wdims[] = {wind_length, wind_width};
AF_CHECK(af_constant(&mask, 1, 2, wdims, info.getType()));
AF_CHECK(af_dilate(out, in, mask));
AF_CHECK(af_release_array(mask));
}
CATCHALL;
return AF_SUCCESS;
}