forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharris.cpp
More file actions
150 lines (124 loc) · 5.75 KB
/
harris.cpp
File metadata and controls
150 lines (124 loc) · 5.75 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
/*******************************************************
* 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 <Array.hpp>
#include <convolve.hpp>
#include <gradient.hpp>
#include <harris.hpp>
#include <kernel/harris.hpp>
#include <math.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <sort_index.hpp>
#include <af/dim4.hpp>
#include <cstring>
using af::dim4;
namespace cpu {
template<typename T, typename convAccT>
unsigned harris(Array<float> &x_out, Array<float> &y_out,
Array<float> &resp_out, const Array<T> &in,
const unsigned max_corners, const float min_response,
const float sigma, const unsigned filter_len,
const float k_thr) {
dim4 idims = in.dims();
// Window filter
auto h_filter = memAlloc<convAccT>(filter_len);
// Decide between rectangular or circular filter
if (sigma < 0.5f) {
for (unsigned i = 0; i < filter_len; i++) {
h_filter[i] = static_cast<T>(1) / (filter_len);
}
} else {
gaussian1D<convAccT>(h_filter.get(), static_cast<int>(filter_len),
sigma);
}
Array<convAccT> filter =
createDeviceDataArray<convAccT>(dim4(filter_len), h_filter.release());
unsigned border_len = filter_len / 2 + 1;
Array<T> ix = createEmptyArray<T>(idims);
Array<T> iy = createEmptyArray<T>(idims);
// Compute first order derivatives
gradient<T>(iy, ix, in);
Array<T> ixx = createEmptyArray<T>(idims);
Array<T> ixy = createEmptyArray<T>(idims);
Array<T> iyy = createEmptyArray<T>(idims);
// Compute second-order derivatives
getQueue().enqueue(kernel::second_order_deriv<T>, ixx, ixy, iyy,
in.elements(), ix, iy);
// Convolve second-order derivatives with proper window filter
ixx = convolve2<T, convAccT>(ixx, filter, filter, false);
ixy = convolve2<T, convAccT>(ixy, filter, filter, false);
iyy = convolve2<T, convAccT>(iyy, filter, filter, false);
const unsigned corner_lim = in.elements() * 0.2f;
Array<T> responses = createEmptyArray<T>(dim4(in.elements()));
getQueue().enqueue(kernel::harris_responses<T>, responses, idims[0],
idims[1], ixx, ixy, iyy, k_thr, border_len);
Array<float> xCorners = createEmptyArray<float>(dim4(corner_lim));
Array<float> yCorners = createEmptyArray<float>(dim4(corner_lim));
Array<float> respCorners = createEmptyArray<float>(dim4(corner_lim));
const unsigned min_r =
(max_corners > 0) ? 0U : static_cast<unsigned>(min_response);
// Performs non-maximal suppression
getQueue().sync();
unsigned corners_found = 0;
kernel::non_maximal<T>(xCorners, yCorners, respCorners, &corners_found,
idims[0], idims[1], responses, min_r, border_len,
corner_lim);
const unsigned corners_out =
min(corners_found, (max_corners > 0) ? max_corners : corner_lim);
if (corners_out == 0) { return 0; }
if (max_corners > 0 && corners_found > corners_out) {
respCorners.resetDims(dim4(corners_found));
Array<float> harris_sorted =
createEmptyArray<float>(dim4(corners_found));
Array<unsigned> harris_idx =
createEmptyArray<unsigned>(dim4(corners_found));
// Sort Harris responses
sort_index<float>(harris_sorted, harris_idx, respCorners, 0, false);
x_out = createEmptyArray<float>(dim4(corners_out));
y_out = createEmptyArray<float>(dim4(corners_out));
resp_out = createEmptyArray<float>(dim4(corners_out));
// Keep only the corners with higher Harris responses
getQueue().enqueue(kernel::keep_corners, x_out, y_out, resp_out,
xCorners, yCorners, harris_sorted, harris_idx,
corners_out);
} else if (max_corners == 0 && corners_found < corner_lim) {
x_out = createEmptyArray<float>(dim4(corners_out));
y_out = createEmptyArray<float>(dim4(corners_out));
resp_out = createEmptyArray<float>(dim4(corners_out));
auto copyFunc =
[=](Param<float> x_out, Param<float> y_out,
Param<float> outResponses, const CParam<float> &x_crnrs,
const CParam<float> &y_crnrs, const CParam<float> &inResponses,
const unsigned corners_out) {
memcpy(x_out.get(), x_crnrs.get(), corners_out * sizeof(float));
memcpy(y_out.get(), y_crnrs.get(), corners_out * sizeof(float));
memcpy(outResponses.get(), inResponses.get(),
corners_out * sizeof(float));
};
getQueue().enqueue(copyFunc, x_out, y_out, resp_out, xCorners, yCorners,
respCorners, corners_out);
} else {
x_out = xCorners;
y_out = yCorners;
resp_out = respCorners;
x_out.resetDims(dim4(corners_out));
y_out.resetDims(dim4(corners_out));
resp_out.resetDims(dim4(corners_out));
}
return corners_out;
}
#define INSTANTIATE(T, convAccT) \
template unsigned harris<T, convAccT>( \
Array<float> & x_out, Array<float> & y_out, Array<float> & score_out, \
const Array<T> &in, const unsigned max_corners, \
const float min_response, const float sigma, \
const unsigned block_size, const float k_thr);
INSTANTIATE(double, double)
INSTANTIATE(float, float)
} // namespace cpu