forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph.cpp
More file actions
120 lines (105 loc) · 3.81 KB
/
morph.cpp
File metadata and controls
120 lines (105 loc) · 3.81 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
/*******************************************************
* 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 <af/dim4.hpp>
#include <af/defines.h>
#include <af/image.h>
#include <handle.hpp>
#include <err_common.hpp>
#include <backend.hpp>
#include <morph.hpp>
using af::dim4;
using namespace detail;
template<typename T, bool isDilation>
static inline af_array morph(const af_array &in, const af_array &mask)
{
const Array<T> &input = getArray<T>(in);
const Array<T> &filter = getArray<T>(mask);
Array<T> out = morph<T, isDilation>(input, filter);
return getHandle(out);
}
template<typename T, bool isDilation>
static inline af_array morph3d(const af_array &in, const af_array &mask)
{
const Array<T> &input = getArray<T>(in);
const Array<T> &filter = getArray<T>(mask);
Array<T> out = morph3d<T, isDilation>(input, filter);
return getHandle(out);
}
template<bool isDilation>
static af_err morph(af_array *out, const af_array &in, const af_array &mask)
{
try {
ArrayInfo info = getInfo(in);
ArrayInfo mInfo= getInfo(mask);
af::dim4 dims = info.dims();
af::dim4 mdims = mInfo.dims();
dim_type in_ndims = dims.ndims();
dim_type mask_ndims = mdims.ndims();
DIM_ASSERT(1, (in_ndims <= 3 && in_ndims >= 2));
DIM_ASSERT(2, (mask_ndims == 2));
af_array output;
af_dtype type = info.getType();
switch(type) {
case f32: output = morph<float , isDilation>(in, mask); break;
case f64: output = morph<double, isDilation>(in, mask); break;
case b8 : output = morph<char , isDilation>(in, mask); break;
case s32: output = morph<int , isDilation>(in, mask); break;
case u32: output = morph<uint , isDilation>(in, mask); break;
case u8 : output = morph<uchar , isDilation>(in, mask); break;
default : TYPE_ERROR(1, type);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}
template<bool isDilation>
static af_err morph3d(af_array *out, const af_array &in, const af_array &mask)
{
try {
ArrayInfo info = getInfo(in);
ArrayInfo mInfo= getInfo(mask);
af::dim4 dims = info.dims();
af::dim4 mdims = mInfo.dims();
dim_type in_ndims = dims.ndims();
dim_type mask_ndims = mdims.ndims();
DIM_ASSERT(1, (in_ndims == 3));
DIM_ASSERT(2, (mask_ndims == 3));
af_array output;
af_dtype type = info.getType();
switch(type) {
case f32: output = morph3d<float , isDilation>(in, mask); break;
case f64: output = morph3d<double, isDilation>(in, mask); break;
case b8 : output = morph3d<char , isDilation>(in, mask); break;
case s32: output = morph3d<int , isDilation>(in, mask); break;
case u32: output = morph3d<uint , isDilation>(in, mask); break;
case u8 : output = morph3d<uchar , isDilation>(in, mask); break;
default : TYPE_ERROR(1, type);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_dilate(af_array *out, const af_array in, const af_array mask)
{
return morph<true>(out,in,mask);
}
af_err af_erode(af_array *out, const af_array in, const af_array mask)
{
return morph<false>(out,in,mask);
}
af_err af_dilate3d(af_array *out, const af_array in, const af_array mask)
{
return morph3d<true>(out,in,mask);
}
af_err af_erode3d(af_array *out, const af_array in, const af_array mask)
{
return morph3d<false>(out,in,mask);
}