-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorb
165 lines (150 loc) · 13.1 KB
/
orb
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_VISION_FEATURE_DESCRIPTOR_ORB_HPP
#define GTL_VISION_FEATURE_DESCRIPTOR_ORB_HPP
// Summary: Implemetation of feature description from the paper "ORB: An efficient alternative to SIFT or SURF". IEEE International Conference on Computer Vision (2011). [wip]
#if defined(_MSC_VER)
#pragma warning(push, 0)
#endif
#include <cmath>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#include <vision/feature/binary_descriptor>
namespace gtl {
void orb(
const unsigned char* __restrict const data,
const int stride,
const float angle_degrees,
gtl::binary_descriptor<32>& descriptor
);
void orb(
const unsigned char* __restrict const data,
const int stride,
const float angle_degrees,
gtl::binary_descriptor<32>& descriptor
) {
constexpr static const int pattern_size = 256;
constexpr static const auto round = [](float value) -> int {
return (value > 0.0f) ? static_cast<int>(value + 0.5f) : static_cast<int>(value - 0.5f);
};
// This is the standard set of pattern sample locations for a patch size of 31.
constexpr static const int pattern[pattern_size][2][2] = {
{{ 8, -3}, { 9, 5}}, {{ 4, 2}, { 7,-12}}, {{-11, 9}, { -8, 2}}, {{ 7,-12}, { 12,-13}},
{{ 2,-13}, { 2, 12}}, {{ 1, -7}, { 1, 6}}, {{ -2,-10}, { -2, -4}}, {{-13,-13}, {-11, -8}},
{{-13, -3}, {-12, -9}}, {{ 10, 4}, { 11, 9}}, {{-13, -8}, { -8, -9}}, {{-11, 7}, { -9, 12}},
{{ 7, 7}, { 12, 6}}, {{ -4, -5}, { -3, 0}}, {{-13, 2}, {-12, -3}}, {{ -9, 0}, { -7, 5}},
{{ 12, -6}, { 12, -1}}, {{ -3, 6}, { -2, 12}}, {{ -6,-13}, { -4, -8}}, {{ 11,-13}, { 12, -8}},
{{ 4, 7}, { 5, 1}}, {{ 5, -3}, { 10, -3}}, {{ 3, -7}, { 6, 12}}, {{ -8, -7}, { -6, -2}},
{{ -2, 11}, { -1,-10}}, {{-13, 12}, { -8, 10}}, {{ -7, 3}, { -5, -3}}, {{ -4, 2}, { -3, 7}},
{{-10,-12}, { -6, 11}}, {{ 5,-12}, { 6, -7}}, {{ 5, -6}, { 7, -1}}, {{ 1, 0}, { 4, -5}},
{{ 9, 11}, { 11,-13}}, {{ 4, 7}, { 4, 12}}, {{ 2, -1}, { 4, 4}}, {{ -4,-12}, { -2, 7}},
{{ -8, -5}, { -7,-10}}, {{ 4, 11}, { 9, 12}}, {{ 0, -8}, { 1,-13}}, {{-13, -2}, { -8, 2}},
{{ -3, -2}, { -2, 3}}, {{ -6, 9}, { -4, -9}}, {{ 8, 12}, { 10, 7}}, {{ 0, 9}, { 1, 3}},
{{ 7, -5}, { 11,-10}}, {{-13, -6}, {-11, 0}}, {{ 10, 7}, { 12, 1}}, {{ -6, -3}, { -6, 12}},
{{ 10, -9}, { 12, -4}}, {{-13, 8}, { -8,-12}}, {{-13, 0}, { -8, -4}}, {{ 3, 3}, { 7, 8}},
{{ 5, 7}, { 10, -7}}, {{ -1, 7}, { 1,-12}}, {{ 3,-10}, { 5, 6}}, {{ 2, -4}, { 3,-10}},
{{-13, 0}, {-13, 5}}, {{-13, -7}, {-12, 12}}, {{-13, 3}, {-11, 8}}, {{ -7, 12}, { -4, 7}},
{{ 6,-10}, { 12, 8}}, {{ -9, -1}, { -7, -6}}, {{ -2, -5}, { 0, 12}}, {{-12, 5}, { -7, 5}},
{{ 3,-10}, { 8,-13}}, {{ -7, -7}, { -4, 5}}, {{ -3, -2}, { -1, -7}}, {{ 2, 9}, { 5,-11}},
{{-11,-13}, { -5,-13}}, {{ -1, 6}, { 0, -1}}, {{ 5, -3}, { 5, 2}}, {{ -4,-13}, { -4, 12}},
{{ -9, -6}, { -9, 6}}, {{-12,-10}, { -8, -4}}, {{ 10, 2}, { 12, -3}}, {{ 7, 12}, { 12, 12}},
{{ -7,-13}, { -6, 5}}, {{ -4, 9}, { -3, 4}}, {{ 7, -1}, { 12, 2}}, {{ -7, 6}, { -5, 1}},
{{-13, 11}, {-12, 5}}, {{ -3, 7}, { -2, -6}}, {{ 7, -8}, { 12, -7}}, {{-13, -7}, {-11,-12}},
{{ 1, -3}, { 12, 12}}, {{ 2, -6}, { 3, 0}}, {{ -4, 3}, { -2,-13}}, {{ -1,-13}, { 1, 9}},
{{ 7, 1}, { 8, -6}}, {{ 1, -1}, { 3, 12}}, {{ 9, 1}, { 12, 6}}, {{ -1, -9}, { -1, 3}},
{{-13,-13}, {-10, 5}}, {{ 7, 7}, { 10, 12}}, {{ 12, -5}, { 12, 9}}, {{ 6, 3}, { 7, 11}},
{{ 5,-13}, { 6, 10}}, {{ 2,-12}, { 2, 3}}, {{ 3, 8}, { 4, -6}}, {{ 2, 6}, { 12,-13}},
{{ 9,-12}, { 10, 3}}, {{ -8, 4}, { -7, 9}}, {{-11, 12}, { -4, -6}}, {{ 1, 12}, { 2, -8}},
{{ 6, -9}, { 7, -4}}, {{ 2, 3}, { 3, -2}}, {{ 6, 3}, { 11, 0}}, {{ 3, -3}, { 8, -8}},
{{ 7, 8}, { 9, 3}}, {{-11, -5}, { -6, -4}}, {{-10, 11}, { -5, 10}}, {{ -5, -8}, { -3, 12}},
{{-10, 5}, { -9, 0}}, {{ 8, -1}, { 12, -6}}, {{ 4, -6}, { 6,-11}}, {{-10, 12}, { -8, 7}},
{{ 4, -2}, { 6, 7}}, {{ -2, 0}, { -2, 12}}, {{ -5, -8}, { -5, 2}}, {{ 7, -6}, { 10, 12}},
{{ -9,-13}, { -8, -8}}, {{ -5,-13}, { -5, -2}}, {{ 8, -8}, { 9,-13}}, {{ -9,-11}, { -9, 0}},
{{ 1, -8}, { 1, -2}}, {{ 7, -4}, { 9, 1}}, {{ -2, 1}, { -1, -4}}, {{ 11, -6}, { 12,-11}},
{{-12, -9}, { -6, 4}}, {{ 3, 7}, { 7, 12}}, {{ 5, 5}, { 10, 8}}, {{ 0, -4}, { 2, 8}},
{{ -9, 12}, { -5,-13}}, {{ 0, 7}, { 2, 12}}, {{ -1, 2}, { 1, 7}}, {{ 5, 11}, { 7, -9}},
{{ 3, 5}, { 6, -8}}, {{-13, -4}, { -8, 9}}, {{ -5, 9}, { -3, -3}}, {{ -4, -7}, { -3,-12}},
{{ 6, 5}, { 8, 0}}, {{ -7, 6}, { -6, 12}}, {{-13, 6}, { -5, -2}}, {{ 1,-10}, { 3, 10}},
{{ 4, 1}, { 8, -4}}, {{ -2, -2}, { 2,-13}}, {{ 2,-12}, { 12, 12}}, {{ -2,-13}, { 0, -6}},
{{ 4, 1}, { 9, 3}}, {{ -6,-10}, { -3, -5}}, {{ -3,-13}, { -1, 1}}, {{ 7, 5}, { 12,-11}},
{{ 4, -2}, { 5, -7}}, {{-13, 9}, { -9, -5}}, {{ 7, 1}, { 8, 6}}, {{ 7, -8}, { 7, 6}},
{{ -7, -4}, { -7, 1}}, {{ -8, 11}, { -7, -8}}, {{-13, 6}, {-12, -8}}, {{ 2, 4}, { 3, 9}},
{{ 10, -5}, { 12, 3}}, {{ -6, -5}, { -6, 7}}, {{ 8, -3}, { 9, -8}}, {{ 2,-12}, { 2, 8}},
{{-11, -2}, {-10, 3}}, {{-12,-13}, { -7, -9}}, {{-11, 0}, {-10, -5}}, {{ 5, -3}, { 11, 8}},
{{ -2,-13}, { -1, 12}}, {{ -1, -8}, { 0, 9}}, {{-13,-11}, {-12, -5}}, {{-10, -2}, {-10, 11}},
{{ -3, 9}, { -2,-13}}, {{ 2, -3}, { 3, 2}}, {{ -9,-13}, { -4, 0}}, {{ -4, 6}, { -3,-10}},
{{ -4, 12}, { -2, -7}}, {{ -6,-11}, { -4, 9}}, {{ 6, -3}, { 6, 11}}, {{-13, 11}, { -5, 5}},
{{ 11, 11}, { 12, 6}}, {{ 7, -5}, { 12, -2}}, {{ -1, 12}, { 0, 7}}, {{ -4, -8}, { -3, -2}},
{{ -7, 1}, { -6, 7}}, {{-13,-12}, { -8,-13}}, {{ -7, -2}, { -6, -8}}, {{ -8, 5}, { -6, -9}},
{{ -5, -1}, { -4, 5}}, {{-13, 7}, { -8, 10}}, {{ 1, 5}, { 5,-13}}, {{ 1, 0}, { 10,-13}},
{{ 9, 12}, { 10, -1}}, {{ 5, -8}, { 10, -9}}, {{ -1, 11}, { 1,-13}}, {{ -9, -3}, { -6, 2}},
{{ -1,-10}, { 1, 12}}, {{-13, 1}, { -8,-10}}, {{ 8,-11}, { 10, -6}}, {{ 2,-13}, { 3, -6}},
{{ 7,-13}, { 12, -9}}, {{-10,-10}, { -5, -7}}, {{-10, -8}, { -8,-13}}, {{ 4, -6}, { 8, 5}},
{{ 3, 12}, { 8,-13}}, {{ -4, 2}, { -3, -3}}, {{ 5,-13}, { 10,-12}}, {{ 4,-13}, { 5, -1}},
{{ -9, 9}, { -4, 3}}, {{ 0, 3}, { 3, -9}}, {{-12, 1}, { -6, 1}}, {{ 3, 2}, { 4, -8}},
{{-10,-10}, {-10, 9}}, {{ 8,-13}, { 12, 12}}, {{ -8,-12}, { -6, -5}}, {{ 2, 2}, { 3, 7}},
{{ 10, 6}, { 11, -8}}, {{ 6, 8}, { 8,-12}}, {{ -7, 10}, { -6, 5}}, {{ -3, -9}, { -3, 9}},
{{ -1,-13}, { -1, 5}}, {{ -3, -7}, { -3, 4}}, {{ -8, -2}, { -8, 3}}, {{ 4, 2}, { 12, 12}},
{{ 2, -5}, { 3, 11}}, {{ 6, -9}, { 11,-13}}, {{ 3, -1}, { 7, 12}}, {{ 11, -1}, { 12, 4}},
{{ -3, 0}, { -3, 6}}, {{ 4,-11}, { 4, 12}}, {{ 2, -4}, { 2, 1}}, {{-10, -6}, { -8, 1}},
{{-13, 7}, {-11, 1}}, {{-13, 12}, {-11,-13}}, {{ 6, 0}, { 11,-13}}, {{ 0, -1}, { 1, 4}},
{{-13, 3}, { -9, -2}}, {{ -9, 8}, { -6, -3}}, {{-13, -6}, { -8, -2}}, {{ 5, -9}, { 8, 10}},
{{ 2, 7}, { 3, -9}}, {{ -1, -6}, { -1, -1}}, {{ 9, 5}, { 11, -2}}, {{ 11, -3}, { 12, -8}},
{{ 3, 0}, { 3, 5}}, {{ -1, 4}, { 0, 10}}, {{ 3, -6}, { 4, 5}}, {{-13, 0}, {-10, 5}},
{{ 5, 8}, { 12, 11}}, {{ 8, 9}, { 9, -6}}, {{ 7, -4}, { 8,-12}}, {{-10, 4}, {-10, 9}},
{{ 7, 3}, { 12, 4}}, {{ 9, -7}, { 10, -2}}, {{ 7, 0}, { 12, -2}}, {{ -1, -6}, { 0,-11}}
};
// Compute sine and cosine of angle.
constexpr static const float degrees_to_radians = (static_cast<float>(M_PI) / 180.0f);
const float angle_radians = angle_degrees * degrees_to_radians;
const float angle_sin = std::sin(angle_radians);
const float angle_cos = std::cos(angle_radians);
// The descriptor is comprised of 32 sets of 8 comparisons on the input image data.
// Note: The input data should be a gaussian blurred version of the original image for orb calculation.
for (int index = 0; index < pattern_size; index += 8) {
const unsigned char pixels_lhs[8] = {
data[(round(pattern[index + 0][0][0] * angle_sin + pattern[index + 0][0][1] * angle_cos)) * stride + (round(pattern[index + 0][0][0] * angle_cos - pattern[index + 0][0][1] * angle_sin))],
data[(round(pattern[index + 1][0][0] * angle_sin + pattern[index + 1][0][1] * angle_cos)) * stride + (round(pattern[index + 1][0][0] * angle_cos - pattern[index + 1][0][1] * angle_sin))],
data[(round(pattern[index + 2][0][0] * angle_sin + pattern[index + 2][0][1] * angle_cos)) * stride + (round(pattern[index + 2][0][0] * angle_cos - pattern[index + 2][0][1] * angle_sin))],
data[(round(pattern[index + 3][0][0] * angle_sin + pattern[index + 3][0][1] * angle_cos)) * stride + (round(pattern[index + 3][0][0] * angle_cos - pattern[index + 3][0][1] * angle_sin))],
data[(round(pattern[index + 4][0][0] * angle_sin + pattern[index + 4][0][1] * angle_cos)) * stride + (round(pattern[index + 4][0][0] * angle_cos - pattern[index + 4][0][1] * angle_sin))],
data[(round(pattern[index + 5][0][0] * angle_sin + pattern[index + 5][0][1] * angle_cos)) * stride + (round(pattern[index + 5][0][0] * angle_cos - pattern[index + 5][0][1] * angle_sin))],
data[(round(pattern[index + 6][0][0] * angle_sin + pattern[index + 6][0][1] * angle_cos)) * stride + (round(pattern[index + 6][0][0] * angle_cos - pattern[index + 6][0][1] * angle_sin))],
data[(round(pattern[index + 7][0][0] * angle_sin + pattern[index + 7][0][1] * angle_cos)) * stride + (round(pattern[index + 7][0][0] * angle_cos - pattern[index + 7][0][1] * angle_sin))],
};
const unsigned char pixels_rhs[8] = {
data[(round(pattern[index + 0][1][0] * angle_sin + pattern[index + 0][1][1] * angle_cos)) * stride + (round(pattern[index + 0][1][0] * angle_cos - pattern[index + 0][1][1] * angle_sin))],
data[(round(pattern[index + 1][1][0] * angle_sin + pattern[index + 1][1][1] * angle_cos)) * stride + (round(pattern[index + 1][1][0] * angle_cos - pattern[index + 1][1][1] * angle_sin))],
data[(round(pattern[index + 2][1][0] * angle_sin + pattern[index + 2][1][1] * angle_cos)) * stride + (round(pattern[index + 2][1][0] * angle_cos - pattern[index + 2][1][1] * angle_sin))],
data[(round(pattern[index + 3][1][0] * angle_sin + pattern[index + 3][1][1] * angle_cos)) * stride + (round(pattern[index + 3][1][0] * angle_cos - pattern[index + 3][1][1] * angle_sin))],
data[(round(pattern[index + 4][1][0] * angle_sin + pattern[index + 4][1][1] * angle_cos)) * stride + (round(pattern[index + 4][1][0] * angle_cos - pattern[index + 4][1][1] * angle_sin))],
data[(round(pattern[index + 5][1][0] * angle_sin + pattern[index + 5][1][1] * angle_cos)) * stride + (round(pattern[index + 5][1][0] * angle_cos - pattern[index + 5][1][1] * angle_sin))],
data[(round(pattern[index + 6][1][0] * angle_sin + pattern[index + 6][1][1] * angle_cos)) * stride + (round(pattern[index + 6][1][0] * angle_cos - pattern[index + 6][1][1] * angle_sin))],
data[(round(pattern[index + 7][1][0] * angle_sin + pattern[index + 7][1][1] * angle_cos)) * stride + (round(pattern[index + 7][1][0] * angle_cos - pattern[index + 7][1][1] * angle_sin))],
};
descriptor[index / 8] = static_cast<unsigned char>(
((pixels_lhs[0] < pixels_rhs[0]) << 0) |
((pixels_lhs[1] < pixels_rhs[1]) << 1) |
((pixels_lhs[2] < pixels_rhs[2]) << 2) |
((pixels_lhs[3] < pixels_rhs[3]) << 3) |
((pixels_lhs[4] < pixels_rhs[4]) << 4) |
((pixels_lhs[5] < pixels_rhs[5]) << 5) |
((pixels_lhs[6] < pixels_rhs[6]) << 6) |
((pixels_lhs[7] < pixels_rhs[7]) << 7)
);
}
}
}
#endif // GTL_VISION_FEATURE_DESCRIPTOR_ORB_HPP