-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhaar_like_feature.jl
executable file
·261 lines (240 loc) · 7.35 KB
/
haar_like_feature.jl
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const FEATURE_TYPES = (
two_vertical = (1, 2),
two_horizontal = (2, 1),
three_horizontal = (3, 1),
three_vertical = (1, 3),
four = (2, 2),
)
abstract type AbstractHaarFeature end
"""
mutable struct HaarLikeObject{I <: Integer, F <: AbstractFloat}
Struct representing a Haar-like feature.
feature_type::Tuple{I, I}
position::Tuple{I, I}
top_left::Tuple{I, I}
bottom_right::Tuple{I, I}
width::I
height::I
threshold::I
polarity::I
weight::F
"""
mutable struct HaarLikeObject{I <: Integer, F <: AbstractFloat} <: AbstractHaarFeature
#parametric struct to store the ints and floats efficiently
feature_type::Tuple{I, I}
position::Tuple{I, I}
top_left::Tuple{I, I}
bottom_right::Tuple{I, I}
width::I
height::I
threshold::I
polarity::I
weight::F
end # end structure
"""
HaarLikeObject(
feature_type::Tuple{Integer, Integer},
position::Tuple{Integer, Integer},
width::Integer,
height::Integer,
threshold::Integer,
polarity::Integer
) -> HaarLikeObject
"""
function HaarLikeObject(
feature_type::Tuple{Integer, Integer},
position::Tuple{Integer, Integer},
width::Integer,
height::Integer,
threshold::Integer,
polarity::Integer,
)
# make sure that everything is of the same size
p₁, p₂ = position
f₁, f₂ = feature_type
p₁, p₂, f₁, f₂, width, height, threshold, polarity = promote(
p₁, p₂, f₁, f₂, width, height, threshold, polarity
)
position = (p₁, p₂)
feature_type = (f₁, f₂)
top_left = position
bottom_right = (first(position) + width, last(position) + height)
weight = float(one(p₁)) #to make a float of the same size
return HaarLikeObject(
feature_type,
position,
top_left,
bottom_right,
width,
height,
threshold,
polarity,
weight,
)
end
"""
get_score(feature::HaarLikeObject, int_img::AbstractArray) -> Tuple{Number, Number}
Get score for given integral image array. This is the feature cascade.
# Arguments
- `feature::HaarLikeObject`: given Haar-like feature (parameterised replacement of Python's `self`)
- `int_img::AbstractArray`: Integral image array
# Returns
- `score::Number`: Score for given feature
"""
function get_score(
feature::HaarLikeObject{I, F}, int_img::IntegralArray{T, N}
) where {I, F, T, N}
score = zero(I)
_2f = F(2)
_3f = F(3)
_half = F(0.5)
_one_third = F(1.0 / 3.0)
if feature.feature_type == FEATURE_TYPES.two_vertical
_first = sum_region(
int_img,
feature.top_left,
(
first(feature.top_left) + feature.width,
round(I, last(feature.top_left) + feature.height / 2),
),
)
second = sum_region(
int_img,
(
first(feature.top_left),
round(I, last(feature.top_left) + feature.height / 2),
),
feature.bottom_right,
)
score = _first - second
elseif feature.feature_type == FEATURE_TYPES.two_horizontal
_first = sum_region(
int_img,
feature.top_left,
(
round(I, first(feature.top_left) + feature.width / 2),
last(feature.top_left) + feature.height,
),
)
second = sum_region(
int_img,
(round(I, first(feature.top_left) + feature.width / 2), last(feature.top_left)),
feature.bottom_right,
)
score = _first - second
elseif feature.feature_type == FEATURE_TYPES.three_horizontal
_first = sum_region(
int_img,
feature.top_left,
(
round(I, first(feature.top_left) + feature.width / 3),
last(feature.top_left) + feature.height,
),
)
second = sum_region(
int_img,
(round(I, first(feature.top_left) + feature.width / 3), last(feature.top_left)),
(
round(I, first(feature.top_left) + 2 * feature.width / 3),
last(feature.top_left) + feature.height,
),
)
third = sum_region(
int_img,
(
round(I, first(feature.top_left) + 2 * feature.width / 3),
last(feature.top_left),
),
feature.bottom_right,
)
score = _first - second + third
elseif feature.feature_type == FEATURE_TYPES.three_vertical
_first = sum_region(
int_img,
feature.top_left,
(
first(feature.bottom_right),
round(I, last(feature.top_left) + feature.height / 3),
),
)
second = sum_region(
int_img,
(
first(feature.top_left),
round(I, last(feature.top_left) + feature.height / 3),
),
(
first(feature.bottom_right),
round(I, last(feature.top_left) + 2 * feature.height / 3),
),
)
third = sum_region(
int_img,
(
first(feature.top_left),
round(I, last(feature.top_left) + 2 * feature.height / 3),
),
feature.bottom_right,
)
score = _first - second + third
elseif feature.feature_type == FEATURE_TYPES.four
# top left area
_first = sum_region(
int_img,
feature.top_left,
(
round(I, first(feature.top_left) + feature.width / 2),
round(I, last(feature.top_left) + feature.height / 2),
),
)
# top right area
second = sum_region(
int_img,
(round(I, first(feature.top_left) + feature.width / 2), last(feature.top_left)),
(
first(feature.bottom_right),
round(I, last(feature.top_left) + feature.height / 2),
),
)
# bottom left area
third = sum_region(
int_img,
(
first(feature.top_left),
round(I, last(feature.top_left) + feature.height / 2),
),
(
round(I, first(feature.top_left) + feature.width / 2),
last(feature.bottom_right),
),
)
# bottom right area
fourth = sum_region(
int_img,
(
round(I, first(feature.top_left) + feature.width / 2),
round(I, last(feature.top_left) + feature.height / 2),
),
feature.bottom_right,
)
score = _first - second - third + fourth
end
return score
end
"""
get_vote(feature::HaarLikeObject, int_img::IntegralArray) -> Integer
Get vote of this feature for given integral image.
# Arguments
- `feature::HaarLikeObject`: given Haar-like feature
- `int_img::IntegralArray`: Integral image array
# Returns
- `vote::Integer`:
1 ⟺ this feature votes positively
-1 otherwise
"""
function get_vote(
feature::HaarLikeObject{I, F}, int_img::IntegralArray{T, N}
) where {I, F, T, N}
score = get_score(feature, int_img)
return score < feature.polarity * feature.threshold ? feature.weight : -feature.weight
end