forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsift.cpp
More file actions
170 lines (153 loc) · 5.79 KB
/
sift.cpp
File metadata and controls
170 lines (153 loc) · 5.79 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
/*******************************************************
* Copyright (c) 2015, 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 <features.hpp>
#include <handle.hpp>
#include <sift.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/features.h>
#include <af/vision.h>
using af::dim4;
using detail::Array;
using detail::createEmptyArray;
template<typename T, typename convAccT>
static void sift(af_features& feat_, af_array& descriptors, const af_array& in,
const unsigned n_layers, const float contrast_thr,
const float edge_thr, const float init_sigma,
const bool double_input, const float img_scale,
const float feature_ratio, const bool compute_GLOH) {
Array<float> x = createEmptyArray<float>(dim4());
Array<float> y = createEmptyArray<float>(dim4());
Array<float> score = createEmptyArray<float>(dim4());
Array<float> ori = createEmptyArray<float>(dim4());
Array<float> size = createEmptyArray<float>(dim4());
Array<float> desc = createEmptyArray<float>(dim4());
af_features_t feat;
feat.n =
sift<T, convAccT>(x, y, score, ori, size, desc, getArray<T>(in),
n_layers, contrast_thr, edge_thr, init_sigma,
double_input, img_scale, feature_ratio, compute_GLOH);
feat.x = getHandle(x);
feat.y = getHandle(y);
feat.score = getHandle(score);
feat.orientation = getHandle(ori);
feat.size = getHandle(size);
feat_ = getFeaturesHandle(feat);
descriptors = getHandle<float>(desc);
}
af_err af_sift(af_features* feat, af_array* desc, const af_array in,
const unsigned n_layers, const float contrast_thr,
const float edge_thr, const float init_sigma,
const bool double_input, const float img_scale,
const float feature_ratio) {
try {
#ifdef AF_WITH_NONFREE_SIFT
const ArrayInfo& info = getInfo(in);
af::dim4 dims = info.dims();
ARG_ASSERT(2, (dims[0] >= 15 && dims[1] >= 15 && dims[2] == 1 &&
dims[3] == 1));
ARG_ASSERT(3, n_layers > 0);
ARG_ASSERT(4, contrast_thr > 0.0f);
ARG_ASSERT(5, edge_thr >= 1.0f);
ARG_ASSERT(6, init_sigma > 0.5f);
ARG_ASSERT(8, img_scale > 0.0f);
ARG_ASSERT(9, feature_ratio > 0.0f);
dim_t in_ndims = dims.ndims();
DIM_ASSERT(1, (in_ndims <= 3 && in_ndims >= 2));
af_array tmp_desc;
af_dtype type = info.getType();
switch (type) {
case f32:
sift<float, float>(*feat, tmp_desc, in, n_layers, contrast_thr,
edge_thr, init_sigma, double_input,
img_scale, feature_ratio, false);
break;
case f64:
sift<double, double>(
*feat, tmp_desc, in, n_layers, contrast_thr, edge_thr,
init_sigma, double_input, img_scale, feature_ratio, false);
break;
default: TYPE_ERROR(1, type);
}
std::swap(*desc, tmp_desc);
#else
UNUSED(feat);
UNUSED(desc);
UNUSED(in);
UNUSED(n_layers);
UNUSED(contrast_thr);
UNUSED(edge_thr);
UNUSED(init_sigma);
UNUSED(double_input);
UNUSED(img_scale);
UNUSED(feature_ratio);
AF_ERROR(
"ArrayFire was not built with nonfree support, SIFT disabled\n",
AF_ERR_NONFREE);
#endif
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_gloh(af_features* feat, af_array* desc, const af_array in,
const unsigned n_layers, const float contrast_thr,
const float edge_thr, const float init_sigma,
const bool double_input, const float img_scale,
const float feature_ratio) {
try {
#ifdef AF_WITH_NONFREE_SIFT
const ArrayInfo& info = getInfo(in);
af::dim4 dims = info.dims();
ARG_ASSERT(2, (dims[0] >= 15 && dims[1] >= 15 && dims[2] == 1 &&
dims[3] == 1));
ARG_ASSERT(3, n_layers > 0);
ARG_ASSERT(4, contrast_thr > 0.0f);
ARG_ASSERT(5, edge_thr >= 1.0f);
ARG_ASSERT(6, init_sigma > 0.5f);
ARG_ASSERT(8, img_scale > 0.0f);
ARG_ASSERT(9, feature_ratio > 0.0f);
dim_t in_ndims = dims.ndims();
DIM_ASSERT(1, (in_ndims == 2));
af_array tmp_desc;
af_dtype type = info.getType();
switch (type) {
case f32:
sift<float, float>(*feat, tmp_desc, in, n_layers, contrast_thr,
edge_thr, init_sigma, double_input,
img_scale, feature_ratio, true);
break;
case f64:
sift<double, double>(
*feat, tmp_desc, in, n_layers, contrast_thr, edge_thr,
init_sigma, double_input, img_scale, feature_ratio, true);
break;
default: TYPE_ERROR(1, type);
}
std::swap(*desc, tmp_desc);
#else
UNUSED(feat);
UNUSED(desc);
UNUSED(in);
UNUSED(n_layers);
UNUSED(contrast_thr);
UNUSED(edge_thr);
UNUSED(init_sigma);
UNUSED(double_input);
UNUSED(img_scale);
UNUSED(feature_ratio);
AF_ERROR(
"ArrayFire was not built with nonfree support, GLOH disabled\n",
AF_ERR_NONFREE);
#endif
}
CATCHALL;
return AF_SUCCESS;
}