blob: 83593e7f0db0239eb94c71c66ff97729aba1e71c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
John Richardson8de92612018-02-22 14:09:31 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/NEON/kernels/NELKTrackerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35#include <arm_neon.h>
36#include <cmath>
37
38using namespace arm_compute;
39
40/** Constants used for Lucas-Kanade Algorithm */
41constexpr int W_BITS = 14;
42constexpr float D0 = 1 << W_BITS;
43constexpr float DETERMINANT_THRESHOLD = 1.0e-07f; // Threshold for the determinant. Used for lost tracking criteria
44constexpr float EIGENVALUE_THRESHOLD = 1.0e-04f; // Thresholds for minimum eigenvalue. Used for lost tracking criteria
45constexpr float FLT_SCALE = 1.0f / (1 << 20);
46
47namespace
48{
49enum class BilinearInterpolation
50{
51 BILINEAR_OLD_NEW,
52 BILINEAR_SCHARR
53};
54
55template <typename T>
56constexpr int INT_ROUND(T x, int n)
57{
58 return (x + (1 << (n - 1))) >> n;
59}
60
61template <typename T>
62inline int get_pixel(const ITensor *tensor, int xi, int yi, int iw00, int iw01, int iw10, int iw11, int scale)
63{
64 const auto px00 = *reinterpret_cast<const T *>(tensor->buffer() + tensor->info()->offset_element_in_bytes(Coordinates(xi, yi)));
65 const auto px01 = *reinterpret_cast<const T *>(tensor->buffer() + tensor->info()->offset_element_in_bytes(Coordinates(xi + 1, yi)));
66 const auto px10 = *reinterpret_cast<const T *>(tensor->buffer() + tensor->info()->offset_element_in_bytes(Coordinates(xi, yi + 1)));
67 const auto px11 = *reinterpret_cast<const T *>(tensor->buffer() + tensor->info()->offset_element_in_bytes(Coordinates(xi + 1, yi + 1)));
68
69 return INT_ROUND(px00 * iw00 + px01 * iw01 + px10 * iw10 + px11 * iw11, scale);
70}
71
72inline int32x4_t compute_bilinear_interpolation(int16x8_t top_row, int16x8_t bottom_row, int16x4_t w00, int16x4_t w01, int16x4_t w10, int16x4_t w11, int32x4_t shift)
73{
74 // Get the left column of upper row
75 const int16x4_t px00 = vget_low_s16(top_row);
76
77 // Get the right column of upper row
78 const int16x4_t px01 = vext_s16(px00, vget_high_s16(top_row), 1);
79
80 // Get the left column of lower row
81 const int16x4_t px10 = vget_low_s16(bottom_row);
82
83 // Get the right column of right row
84 const int16x4_t px11 = vext_s16(px10, vget_high_s16(bottom_row), 1);
85
86 // Apply the bilinear filter
87 return vqrshlq_s32(vmull_s16(px00, w00) + vmull_s16(px01, w01) + vmull_s16(px10, w10) + vmull_s16(px11, w11), shift);
88}
89} // namespace
90
91void NELKTrackerKernel::init_keypoints(int start, int end)
92{
93 if(_level == _num_levels - 1)
94 {
95 const float level_scale = pow(_pyramid_scale, _level);
96
97 for(int i = start; i < end; ++i)
98 {
99 _old_points_internal->at(i).x = _old_points->at(i).x * level_scale;
100 _old_points_internal->at(i).y = _old_points->at(i).y * level_scale;
101 _old_points_internal->at(i).tracking_status = true;
102
103 NELKInternalKeypoint keypoint_to_track;
104
105 if(_use_initial_estimate)
106 {
107 keypoint_to_track.x = _new_points_estimates->at(i).x * level_scale;
108 keypoint_to_track.y = _new_points_estimates->at(i).y * level_scale;
109 keypoint_to_track.tracking_status = (_new_points_estimates->at(i).tracking_status == 1);
110 }
111 else
112 {
113 keypoint_to_track.x = _old_points_internal->at(i).x;
114 keypoint_to_track.y = _old_points_internal->at(i).y;
115 keypoint_to_track.tracking_status = true;
116 }
117
118 _new_points_internal->at(i) = keypoint_to_track;
119 }
120 }
121 else
122 {
123 for(int i = start; i < end; ++i)
124 {
125 _old_points_internal->at(i).x /= _pyramid_scale;
126 _old_points_internal->at(i).y /= _pyramid_scale;
127 _new_points_internal->at(i).x /= _pyramid_scale;
128 _new_points_internal->at(i).y /= _pyramid_scale;
129 }
130 }
131}
132
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100133std::tuple<int, int, int> NELKTrackerKernel::compute_spatial_gradient_matrix(const NELKInternalKeypoint &keypoint, int32_t *bilinear_ix, int32_t *bilinear_iy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134{
135 int iA11 = 0;
136 int iA12 = 0;
137 int iA22 = 0;
138
139 int32x4_t nA11 = vdupq_n_s32(0);
140 int32x4_t nA12 = vdupq_n_s32(0);
141 int32x4_t nA22 = vdupq_n_s32(0);
142
143 float keypoint_int_x = 0;
144 float keypoint_int_y = 0;
145
146 const float wx = std::modf(keypoint.x, &keypoint_int_x);
147 const float wy = std::modf(keypoint.y, &keypoint_int_y);
148
149 const int iw00 = roundf((1.0f - wx) * (1.0f - wy) * D0);
150 const int iw01 = roundf(wx * (1.0f - wy) * D0);
151 const int iw10 = roundf((1.0f - wx) * wy * D0);
152 const int iw11 = D0 - iw00 - iw01 - iw10;
153
154 const int16x4_t nw00 = vdup_n_s16(iw00);
155 const int16x4_t nw01 = vdup_n_s16(iw01);
156 const int16x4_t nw10 = vdup_n_s16(iw10);
157 const int16x4_t nw11 = vdup_n_s16(iw11);
158
159 // Convert stride from uint_t* to int16_t*
160 const size_t row_stride = _old_scharr_gx->info()->strides_in_bytes()[1] / 2;
161 const Coordinates top_left_window_corner(static_cast<int>(keypoint_int_x) - _window_dimension / 2, static_cast<int>(keypoint_int_y) - _window_dimension / 2);
162 auto idx = reinterpret_cast<const int16_t *>(_old_scharr_gx->buffer() + _old_scharr_gx->info()->offset_element_in_bytes(top_left_window_corner));
163 auto idy = reinterpret_cast<const int16_t *>(_old_scharr_gy->buffer() + _old_scharr_gy->info()->offset_element_in_bytes(top_left_window_corner));
164 static const int32x4_t nshifter_scharr = vdupq_n_s32(-W_BITS);
165
166 for(int ky = 0; ky < _window_dimension; ++ky, idx += row_stride, idy += row_stride)
167 {
168 int kx = 0;
169
170 // Calculate elements in blocks of four as long as possible
171 for(; kx <= _window_dimension - 4; kx += 4)
172 {
173 // Interpolation X
174 const int16x8_t ndx_row1 = vld1q_s16(idx + kx);
175 const int16x8_t ndx_row2 = vld1q_s16(idx + kx + row_stride);
176
177 const int32x4_t nxval = compute_bilinear_interpolation(ndx_row1, ndx_row2, nw00, nw01, nw10, nw11, nshifter_scharr);
178
179 // Interpolation Y
180 const int16x8_t ndy_row1 = vld1q_s16(idy + kx);
181 const int16x8_t ndy_row2 = vld1q_s16(idy + kx + row_stride);
182
183 const int32x4_t nyval = compute_bilinear_interpolation(ndy_row1, ndy_row2, nw00, nw01, nw10, nw11, nshifter_scharr);
184
185 // Store the intermediate data so that we don't need to recalculate them in later stage
186 vst1q_s32(bilinear_ix + kx + ky * _window_dimension, nxval);
187 vst1q_s32(bilinear_iy + kx + ky * _window_dimension, nyval);
188
189 // Accumulate Ix^2
190 nA11 = vmlaq_s32(nA11, nxval, nxval);
191 // Accumulate Ix * Iy
192 nA12 = vmlaq_s32(nA12, nxval, nyval);
193 // Accumulate Iy^2
194 nA22 = vmlaq_s32(nA22, nyval, nyval);
195 }
196
197 // Calculate the leftover elements
198 for(; kx < _window_dimension; ++kx)
199 {
200 const int32_t ixval = get_pixel<int16_t>(_old_scharr_gx, top_left_window_corner.x() + kx, top_left_window_corner.y() + ky,
201 iw00, iw01, iw10, iw11, W_BITS);
202 const int32_t iyval = get_pixel<int16_t>(_old_scharr_gy, top_left_window_corner.x() + kx, top_left_window_corner.y() + ky,
203 iw00, iw01, iw10, iw11, W_BITS);
204
205 iA11 += ixval * ixval;
206 iA12 += ixval * iyval;
207 iA22 += iyval * iyval;
208
209 bilinear_ix[kx + ky * _window_dimension] = ixval;
210 bilinear_iy[kx + ky * _window_dimension] = iyval;
211 }
212 }
213
214 iA11 += vgetq_lane_s32(nA11, 0) + vgetq_lane_s32(nA11, 1) + vgetq_lane_s32(nA11, 2) + vgetq_lane_s32(nA11, 3);
215 iA12 += vgetq_lane_s32(nA12, 0) + vgetq_lane_s32(nA12, 1) + vgetq_lane_s32(nA12, 2) + vgetq_lane_s32(nA12, 3);
216 iA22 += vgetq_lane_s32(nA22, 0) + vgetq_lane_s32(nA22, 1) + vgetq_lane_s32(nA22, 2) + vgetq_lane_s32(nA22, 3);
217
218 return std::make_tuple(iA11, iA12, iA22);
219}
220
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100221std::pair<int, int> NELKTrackerKernel::compute_image_mismatch_vector(const NELKInternalKeypoint &old_keypoint, const NELKInternalKeypoint &new_keypoint, const int32_t *bilinear_ix,
222 const int32_t *bilinear_iy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223{
224 int ib1 = 0;
225 int ib2 = 0;
226
227 int32x4_t nb1 = vdupq_n_s32(0);
228 int32x4_t nb2 = vdupq_n_s32(0);
229
230 // Compute weights for the old keypoint
231 float old_keypoint_int_x = 0;
232 float old_keypoint_int_y = 0;
233
234 const float old_wx = std::modf(old_keypoint.x, &old_keypoint_int_x);
235 const float old_wy = std::modf(old_keypoint.y, &old_keypoint_int_y);
236
237 const int iw00_old = roundf((1.0f - old_wx) * (1.0f - old_wy) * D0);
238 const int iw01_old = roundf(old_wx * (1.0f - old_wy) * D0);
239 const int iw10_old = roundf((1.0f - old_wx) * old_wy * D0);
240 const int iw11_old = D0 - iw00_old - iw01_old - iw10_old;
241
242 const int16x4_t nw00_old = vdup_n_s16(iw00_old);
243 const int16x4_t nw01_old = vdup_n_s16(iw01_old);
244 const int16x4_t nw10_old = vdup_n_s16(iw10_old);
245 const int16x4_t nw11_old = vdup_n_s16(iw11_old);
246
247 // Compute weights for the new keypoint
248 float new_keypoint_int_x = 0;
249 float new_keypoint_int_y = 0;
250
251 const float new_wx = std::modf(new_keypoint.x, &new_keypoint_int_x);
252 const float new_wy = std::modf(new_keypoint.y, &new_keypoint_int_y);
253
254 const int iw00_new = roundf((1.0f - new_wx) * (1.0f - new_wy) * D0);
255 const int iw01_new = roundf(new_wx * (1.0f - new_wy) * D0);
256 const int iw10_new = roundf((1.0f - new_wx) * new_wy * D0);
257 const int iw11_new = D0 - iw00_new - iw01_new - iw10_new;
258
259 const int16x4_t nw00_new = vdup_n_s16(iw00_new);
260 const int16x4_t nw01_new = vdup_n_s16(iw01_new);
261 const int16x4_t nw10_new = vdup_n_s16(iw10_new);
262 const int16x4_t nw11_new = vdup_n_s16(iw11_new);
263
264 const int row_stride = _input_new->info()->strides_in_bytes()[1];
265 const Coordinates top_left_window_corner_old(static_cast<int>(old_keypoint_int_x) - _window_dimension / 2, static_cast<int>(old_keypoint_int_y) - _window_dimension / 2);
266 const Coordinates top_left_window_corner_new(static_cast<int>(new_keypoint_int_x) - _window_dimension / 2, static_cast<int>(new_keypoint_int_y) - _window_dimension / 2);
267 const uint8_t *old_ptr = _input_old->buffer() + _input_old->info()->offset_element_in_bytes(top_left_window_corner_old);
268 const uint8_t *new_ptr = _input_new->buffer() + _input_new->info()->offset_element_in_bytes(top_left_window_corner_new);
269 static const int32x4_t nshifter_tensor = vdupq_n_s32(-(W_BITS - 5));
270
271 for(int ky = 0; ky < _window_dimension; ++ky, new_ptr += row_stride, old_ptr += row_stride)
272 {
273 int kx = 0;
274
275 // Calculate elements in blocks of four as long as possible
276 for(; kx <= _window_dimension - 4; kx += 4)
277 {
278 // Interpolation old tensor
279 const int16x8_t nold_row1 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(old_ptr + kx)));
280 const int16x8_t nold_row2 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(old_ptr + kx + row_stride)));
281
282 const int32x4_t noldval = compute_bilinear_interpolation(nold_row1, nold_row2, nw00_old, nw01_old, nw10_old, nw11_old, nshifter_tensor);
283
284 // Interpolation new tensor
285 const int16x8_t nnew_row1 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(new_ptr + kx)));
286 const int16x8_t nnew_row2 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(new_ptr + kx + row_stride)));
287
288 const int32x4_t nnewval = compute_bilinear_interpolation(nnew_row1, nnew_row2, nw00_new, nw01_new, nw10_new, nw11_new, nshifter_tensor);
289
290 // Calculate It gradient, i.e. pixelwise difference between old and new tensor
291 const int32x4_t diff = vsubq_s32(nnewval, noldval);
292
293 // Load the Ix and Iy gradient computed in the previous stage
294 const int32x4_t nxval = vld1q_s32(bilinear_ix + kx + ky * _window_dimension);
295 const int32x4_t nyval = vld1q_s32(bilinear_iy + kx + ky * _window_dimension);
296
297 // Caculate Ix * It and Iy * It, and accumulate the results
298 nb1 = vmlaq_s32(nb1, diff, nxval);
299 nb2 = vmlaq_s32(nb2, diff, nyval);
300 }
301
302 // Calculate the leftover elements
303 for(; kx < _window_dimension; ++kx)
304 {
305 const int32_t ival = get_pixel<uint8_t>(_input_old, top_left_window_corner_old.x() + kx, top_left_window_corner_old.y() + ky,
306 iw00_old, iw01_old, iw10_old, iw11_old, W_BITS - 5);
307 const int32_t jval = get_pixel<uint8_t>(_input_new, top_left_window_corner_new.x() + kx, top_left_window_corner_new.y() + ky,
308 iw00_new, iw01_new, iw10_new, iw11_new, W_BITS - 5);
309
310 const int32_t diff = jval - ival;
311
312 ib1 += diff * bilinear_ix[kx + ky * _window_dimension];
313 ib2 += diff * bilinear_iy[kx + ky * _window_dimension];
314 }
315 }
316
317 ib1 += vgetq_lane_s32(nb1, 0) + vgetq_lane_s32(nb1, 1) + vgetq_lane_s32(nb1, 2) + vgetq_lane_s32(nb1, 3);
318 ib2 += vgetq_lane_s32(nb2, 0) + vgetq_lane_s32(nb2, 1) + vgetq_lane_s32(nb2, 2) + vgetq_lane_s32(nb2, 3);
319
320 return std::make_pair(ib1, ib2);
321}
322
323NELKTrackerKernel::NELKTrackerKernel()
324 : _input_old(nullptr), _input_new(nullptr), _old_scharr_gx(nullptr), _old_scharr_gy(nullptr), _new_points(nullptr), _new_points_estimates(nullptr), _old_points(nullptr), _old_points_internal(),
325 _new_points_internal(), _termination(Termination::TERM_CRITERIA_EPSILON), _use_initial_estimate(false), _pyramid_scale(0.0f), _epsilon(0.0f), _num_iterations(0), _window_dimension(0), _level(0),
326 _num_levels(0), _valid_region()
327{
328}
329
330BorderSize NELKTrackerKernel::border_size() const
331{
332 return BorderSize(1);
333}
334
335void NELKTrackerKernel::configure(const ITensor *input_old, const ITensor *input_new, const ITensor *old_scharr_gx, const ITensor *old_scharr_gy,
336 const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates, IKeyPointArray *new_points,
337 INELKInternalKeypointArray *old_points_internal, INELKInternalKeypointArray *new_points_internal,
338 Termination termination, bool use_initial_estimate, float epsilon, unsigned int num_iterations, size_t window_dimension,
339 size_t level, size_t num_levels, float pyramid_scale)
340
341{
342 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_old, 1, DataType::U8);
343 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_new, 1, DataType::U8);
344 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(old_scharr_gx, 1, DataType::S16);
345 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(old_scharr_gy, 1, DataType::S16);
346
347 _input_old = input_old;
348 _input_new = input_new;
349 _old_scharr_gx = old_scharr_gx;
350 _old_scharr_gy = old_scharr_gy;
351 _old_points = old_points;
352 _new_points_estimates = new_points_estimates;
353 _new_points = new_points;
354 _old_points_internal = old_points_internal;
355 _new_points_internal = new_points_internal;
356 _termination = termination;
357 _use_initial_estimate = use_initial_estimate;
358 _epsilon = epsilon;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359 _window_dimension = window_dimension;
360 _level = level;
361 _num_levels = num_levels;
362 _pyramid_scale = pyramid_scale;
363 _num_levels = num_levels;
364
John Richardson8de92612018-02-22 14:09:31 +0000365 // Set maximum number of iterations used for convergence
366 const size_t max_iterations = 1000;
367 _num_iterations = (termination == Termination::TERM_CRITERIA_EPSILON) ? max_iterations : num_iterations;
368
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369 Window window;
370 window.set(Window::DimX, Window::Dimension(0, old_points->num_values()));
371 window.set(Window::DimY, Window::Dimension(0, 1));
372
373 _valid_region = intersect_valid_regions(
374 input_old->info()->valid_region(),
375 input_new->info()->valid_region(),
376 old_scharr_gx->info()->valid_region(),
377 old_scharr_gy->info()->valid_region());
378
379 update_window_and_padding(window,
380 AccessWindowStatic(input_old->info(), _valid_region.start(0), _valid_region.start(1),
381 _valid_region.end(0), _valid_region.end(1)),
382 AccessWindowStatic(input_new->info(), _valid_region.start(0), _valid_region.start(1),
383 _valid_region.end(0), _valid_region.end(1)),
384 AccessWindowStatic(old_scharr_gx->info(), _valid_region.start(0), _valid_region.start(1),
385 _valid_region.end(0), _valid_region.end(1)),
386 AccessWindowStatic(old_scharr_gy->info(), _valid_region.start(0), _valid_region.start(1),
387 _valid_region.end(0), _valid_region.end(1)));
388
389 INEKernel::configure(window);
390}
391
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100392void NELKTrackerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100393{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100394 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
396 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
397
398 ARM_COMPUTE_ERROR_ON(_input_old->buffer() == nullptr);
399 ARM_COMPUTE_ERROR_ON(_input_new->buffer() == nullptr);
400 ARM_COMPUTE_ERROR_ON(_old_scharr_gx->buffer() == nullptr);
401 ARM_COMPUTE_ERROR_ON(_old_scharr_gy->buffer() == nullptr);
402
403 const int list_end = window.x().end();
404 const int list_start = window.x().start();
405
406 init_keypoints(list_start, list_end);
407
408 const int buffer_size = _window_dimension * _window_dimension;
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100409 int32_t bilinear_ix[buffer_size];
410 int32_t bilinear_iy[buffer_size];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411
412 const int half_window = _window_dimension / 2;
413
414 auto is_invalid_keypoint = [&](const NELKInternalKeypoint & keypoint)
415 {
416 const int x = std::floor(keypoint.x);
417 const int y = std::floor(keypoint.y);
418
419 return (x - half_window < _valid_region.start(0)) || (x + half_window >= _valid_region.end(0) - 1) || (y - half_window < _valid_region.start(1)) || (y + half_window >= _valid_region.end(1) - 1);
420 };
421
422 for(int list_indx = list_start; list_indx < list_end; ++list_indx)
423 {
424 NELKInternalKeypoint &old_keypoint = _old_points_internal->at(list_indx);
425 NELKInternalKeypoint &new_keypoint = _new_points_internal->at(list_indx);
426
427 if(!old_keypoint.tracking_status)
428 {
429 continue;
430 }
431
432 if(is_invalid_keypoint(old_keypoint))
433 {
434 if(_level == 0)
435 {
436 new_keypoint.tracking_status = false;
437 }
438
439 continue;
440 }
441
442 // Compute spatial gradient matrix
443 int iA11 = 0;
444 int iA12 = 0;
445 int iA22 = 0;
446
447 std::tie(iA11, iA12, iA22) = compute_spatial_gradient_matrix(old_keypoint, bilinear_ix, bilinear_iy);
448
449 const float A11 = iA11 * FLT_SCALE;
450 const float A12 = iA12 * FLT_SCALE;
451 const float A22 = iA22 * FLT_SCALE;
452
453 // Calculate minimum eigenvalue
454 const float sum_A11_A22 = A11 + A22;
455 const float discriminant = sum_A11_A22 * sum_A11_A22 - 4.0f * (A11 * A22 - A12 * A12);
456 // Divide by _window_dimension^2 to reduce the floating point accummulation error
457 const float minimum_eigenvalue = (sum_A11_A22 - std::sqrt(discriminant)) / (2.0f * _window_dimension * _window_dimension);
458
459 // Determinant
460 const double D = A11 * A22 - A12 * A12;
461
462 // Check if it is a good point to track
463 if(minimum_eigenvalue < EIGENVALUE_THRESHOLD || D < DETERMINANT_THRESHOLD)
464 {
465 // Invalidate tracked point
466 if(_level == 0)
467 {
468 new_keypoint.tracking_status = false;
469 }
470
471 continue;
472 }
473
474 float prev_delta_x = 0.0f;
475 float prev_delta_y = 0.0f;
476
John Richardson8de92612018-02-22 14:09:31 +0000477 for(unsigned int j = 0; j < _num_iterations; ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100478 {
479 if(is_invalid_keypoint(new_keypoint))
480 {
481 if(_level == 0)
482 {
483 new_keypoint.tracking_status = false;
484 }
485
486 break;
487 }
488
489 // Compute image mismatch vector
490 int ib1 = 0;
491 int ib2 = 0;
492
493 std::tie(ib1, ib2) = compute_image_mismatch_vector(old_keypoint, new_keypoint, bilinear_ix, bilinear_iy);
494
495 double b1 = ib1 * FLT_SCALE;
496 double b2 = ib2 * FLT_SCALE;
497
498 // Compute motion vector -> A^-1 * -b
499 const float delta_x = (A12 * b2 - A22 * b1) / D;
500 const float delta_y = (A12 * b1 - A11 * b2) / D;
501
502 // Update the new position
503 new_keypoint.x += delta_x;
504 new_keypoint.y += delta_y;
505
506 const float mag2 = delta_x * delta_x + delta_y * delta_y;
507
508 // Check if termination criteria is EPSILON and if it is satisfied
509 if(mag2 <= _epsilon && (_termination == Termination::TERM_CRITERIA_EPSILON || _termination == Termination::TERM_CRITERIA_BOTH))
510 {
511 break;
512 }
513
514 // Check convergence analyzing the previous delta
515 if(j > 0 && std::fabs(delta_x + prev_delta_x) < 0.01f && std::fabs(delta_y + prev_delta_y) < 0.01f)
516 {
517 new_keypoint.x -= delta_x * _pyramid_scale;
518 new_keypoint.y -= delta_y * _pyramid_scale;
519 break;
520 }
521
522 prev_delta_x = delta_x;
523 prev_delta_y = delta_y;
524 }
525 }
526
527 if(_level == 0)
528 {
529 for(int list_indx = list_start; list_indx < list_end; ++list_indx)
530 {
531 const NELKInternalKeypoint &new_keypoint = _new_points_internal->at(list_indx);
532
533 _new_points->at(list_indx).x = roundf(new_keypoint.x);
534 _new_points->at(list_indx).y = roundf(new_keypoint.y);
535 _new_points->at(list_indx).tracking_status = new_keypoint.tracking_status ? 1 : 0;
536 }
537 }
538}