blob: 20474f00955498cd4cc5a4ccbf5dcddaf77dfcf6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-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 "helpers.h"
25#include "types.h"
26
27/*
28 *The criteria for lost tracking is that the spatial gradient matrix has:
29 * - Determinant less than DETERMINANT_THR
30 * - or minimum eigenvalue is smaller then EIGENVALUE_THR
31 *
32 * The thresholds for the determinant and the minimum eigenvalue is
33 * defined by the OpenVX spec
34 *
35 * Note: Also lost tracking happens when the point tracked coordinate is outside
36 * the image coordinates
37 *
38 * https://www.khronos.org/registry/vx/specs/1.0/html/d0/d0c/group__group__vision__function__opticalflowpyrlk.html
39 */
40
41/* Internal Lucas-Kanade Keypoint struct */
42typedef struct InternalKeypoint
43{
44 float x; /**< The x coordinate. */
45 float y; /**< The y coordinate. */
46 float tracking_status; /**< A zero indicates a lost point. Initialized to 1 by corner detectors. */
Alex Gildayc357c472018-03-21 13:54:09 +000047 float dummy; /**< Dummy member for alignment. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048} InternalKeypoint;
49
50/** Threshold for the determinant. Used for lost tracking criteria */
51#define DETERMINANT_THR 1.0e-07f
52
53/** Thresholds for minimum eigenvalue. Used for lost tracking criteria */
54#define EIGENVALUE_THR 1.0e-04f
55
56/** Constants used for Lucas-Kanade Algorithm */
57#define W_BITS (14)
58#define FLT_SCALE (1.0f / (float)(1 << 20))
59#define D0 ((float)(1 << W_BITS))
60#define D1 (1.0f / (float)(1 << (W_BITS - 5)))
61
62/** Initializes the internal new points array when the level of pyramid is NOT equal to max.
63 *
64 * @param[in,out] old_points_internal An array of internal key points that are defined at the old_images high resolution pyramid.
65 * @param[in,out] new_points_internal An array of internal key points that are defined at the new_images high resolution pyramid.
66 * @param[in] scale Scale factor to apply for the new_point coordinates.
67 */
68__kernel void init_level(
69 __global float4 *old_points_internal,
70 __global float4 *new_points_internal,
71 const float scale)
72{
73 int idx = get_global_id(0);
74
75 // Get old and new keypoints
76 float4 old_point = old_points_internal[idx];
77 float4 new_point = new_points_internal[idx];
78
79 // Scale accordingly with the pyramid_scale
80 old_point.xy *= (float2)(2.0f);
81 new_point.xy *= (float2)(2.0f);
82
83 old_points_internal[idx] = old_point;
84 new_points_internal[idx] = new_point;
85}
86
87/** Initializes the internal new points array when the level of pyramid is equal to max.
88 *
89 * @param[in] old_points An array of key points that are defined at the old_images high resolution pyramid.
90 * @param[in,out] old_points_internal An array of internal key points that are defined at the old_images high resolution pyramid.
91 * @param[out] new_points_internal An array of internal key points that are defined at the new_images high resolution pyramid.
92 * @param[in] scale Scale factor to apply for the new_point coordinates.
93 */
94__kernel void init_level_max(
95 __global Keypoint *old_points,
96 __global InternalKeypoint *old_points_internal,
97 __global InternalKeypoint *new_points_internal,
98 const float scale)
99{
100 int idx = get_global_id(0);
101
102 Keypoint old_point = old_points[idx];
103
104 // Get old keypoint to track
105 InternalKeypoint old_point_internal;
106 old_point_internal.x = old_point.x * scale;
107 old_point_internal.y = old_point.y * scale;
108 old_point_internal.tracking_status = 1.f;
109
110 // Store internal keypoints
111 old_points_internal[idx] = old_point_internal;
112 new_points_internal[idx] = old_point_internal;
113}
114
115/** Initializes the new_points array when the level of pyramid is equal to max and if use_initial_estimate = 1.
116 *
117 * @param[in] old_points An array of key points that are defined at the old_images high resolution pyramid.
118 * @param[in] new_points_estimates An array of estimate key points that are defined at the old_images high resolution pyramid.
119 * @param[in,out] old_points_internal An array of internal key points that are defined at the old_images high resolution pyramid.
120 * @param[out] new_points_internal An array of internal key points that are defined at the new_images high resolution pyramid.
121 * @param[in] scale Scale factor to apply for the new_point coordinates.
122 */
123__kernel void init_level_max_initial_estimate(
124 __global Keypoint *old_points,
125 __global Keypoint *new_points_estimates,
126 __global InternalKeypoint *old_points_internal,
127 __global InternalKeypoint *new_points_internal,
128 const float scale)
129{
130 int idx = get_global_id(0);
131
132 Keypoint old_point = old_points[idx];
133 Keypoint new_point_estimate = new_points_estimates[idx];
134 InternalKeypoint old_point_internal;
135 InternalKeypoint new_point_internal;
136
137 // Get old keypoint to track
138 old_point_internal.x = old_point.x * scale;
139 old_point_internal.y = old_point.y * scale;
140 old_point_internal.tracking_status = 1.f;
141
142 // Get new keypoint to track
143 new_point_internal.x = new_point_estimate.x * scale;
144 new_point_internal.y = new_point_estimate.y * scale;
145 new_point_internal.tracking_status = new_point_estimate.tracking_status;
146
147 // Store internal keypoints
148 old_points_internal[idx] = old_point_internal;
149 new_points_internal[idx] = new_point_internal;
150}
151
152/** Truncates the coordinates stored in new_points array
153 *
154 * @param[in] new_points_internal An array of estimate key points that are defined at the new_images high resolution pyramid.
155 * @param[out] new_points An array of internal key points that are defined at the new_images high resolution pyramid.
156 */
157__kernel void finalize(
158 __global InternalKeypoint *new_points_internal,
159 __global Keypoint *new_points)
160{
161 int idx = get_global_id(0);
162
163 // Load internal keypoint
164 InternalKeypoint new_point_internal = new_points_internal[idx];
165
166 // Calculate output point
167 Keypoint new_point;
168 new_point.x = round(new_point_internal.x);
169 new_point.y = round(new_point_internal.y);
170 new_point.tracking_status = new_point_internal.tracking_status;
171
172 // Store new point
173 new_points[idx] = new_point;
174}
175
176/** Computes A11, A12, A22, min_eig, ival, ixval and iyval at level 0th of the pyramid. These values will be used in step 1.
177 *
178 * @param[in] old_image_ptr Pointer to the input old image. Supported data types: U8
179 * @param[in] old_image_stride_x Stride of the input old image in X dimension (in bytes)
180 * @param[in] old_image_step_x old_image_stride_x * number of elements along X processed per workitem(in bytes)
181 * @param[in] old_image_stride_y Stride of the input old image in Y dimension (in bytes)
182 * @param[in] old_image_step_y old_image_stride_y * number of elements along Y processed per workitem(in bytes)
183 * @param[in] old_image_offset_first_element_in_bytes The offset of the first element in the input old image
184 * @param[in] old_scharr_gx_ptr Pointer to the input scharr x image. Supported data types: S16
185 * @param[in] old_scharr_gx_stride_x Stride of the input scharr x image in X dimension (in bytes)
186 * @param[in] old_scharr_gx_step_x old_scharr_gx_stride_x * number of elements along X processed per workitem(in bytes)
187 * @param[in] old_scharr_gx_stride_y Stride of the input scharr x image in Y dimension (in bytes)
188 * @param[in] old_scharr_gx_step_y old_scharr_gx_stride_y * number of elements along Y processed per workitem(in bytes)
189 * @param[in] old_scharr_gx_offset_first_element_in_bytes The offset of the first element in the input scharr x image
190 * @param[in] old_scharr_gy_ptr Pointer to the input scharr y image. Supported data types: S16
191 * @param[in] old_scharr_gy_stride_x Stride of the input scharr y image in X dimension (in bytes)
192 * @param[in] old_scharr_gy_step_x old_scharr_gy_stride_x * number of elements along X processed per workitem(in bytes)
193 * @param[in] old_scharr_gy_stride_y Stride of the input scharr y image in Y dimension (in bytes)
194 * @param[in] old_scharr_gy_step_y old_scharr_gy_stride_y * number of elements along Y processed per workitem(in bytes)
195 * @param[in] old_scharr_gy_offset_first_element_in_bytes The offset of the first element in the input scharr y image
196 * @param[in] old_points An array of key points. Those key points are defined at the old_images high resolution pyramid
197 * @param[in, out] new_points An output array of key points. Those key points are defined at the new_images high resolution pyramid
198 * @param[out] coeff It stores | A11 | A12 | A22 | min_eig | for each keypoint
199 * @param[out] iold_val It stores | ival | ixval | iyval | dummy | for each point in the window centered on old_keypoint
200 * @param[in] window_dimension The size of the window on which to perform the algorithm
201 * @param[in] window_dimension_pow2 The squared size of the window on which to perform the algorithm
202 * @param[in] half_window The half size of the window on which to perform the algorithm
203 * @param[in] border_limits It stores the right border limit (width - window_dimension - 1, height - window_dimension - 1,)
204 * @param[in] eig_const 1.0f / (float)(2.0f * window_dimension * window_dimension)
205 * @param[in] level0 It is set to 1 if level 0 of the pyramid
206 */
207void __kernel lktracker_stage0(
208 IMAGE_DECLARATION(old_image),
209 IMAGE_DECLARATION(old_scharr_gx),
210 IMAGE_DECLARATION(old_scharr_gy),
211 __global float4 *old_points,
212 __global float4 *new_points,
213 __global float4 *coeff,
214 __global short4 *iold_val,
215 const int window_dimension,
216 const int window_dimension_pow2,
217 const int half_window,
218 const float3 border_limits,
219 const float eig_const,
220 const int level0)
221{
222 int idx = get_global_id(0);
223
224 Image old_image = CONVERT_TO_IMAGE_STRUCT_NO_STEP(old_image);
225 Image old_scharr_gx = CONVERT_TO_IMAGE_STRUCT_NO_STEP(old_scharr_gx);
226 Image old_scharr_gy = CONVERT_TO_IMAGE_STRUCT_NO_STEP(old_scharr_gy);
227
228 // Get old keypoint
229 float2 old_keypoint = old_points[idx].xy - (float2)half_window;
230
231 // Get the floor value
232 float2 iold_keypoint = floor(old_keypoint);
233
234 // Check if using the window dimension we can go out of boundary in the following for loops. If so, invalidate the tracked point
235 if(any(iold_keypoint < border_limits.zz) || any(iold_keypoint >= border_limits.xy))
236 {
237 if(level0 == 1)
238 {
239 // Invalidate tracked point as we are at level 0
240 new_points[idx].s2 = 0.0f;
241 }
242
243 // Not valid coordinate. It sets min_eig to 0.0f
244 coeff[idx].s3 = 0.0f;
245
246 return;
247 }
248
249 // Compute weight for the bilinear interpolation
250 float2 ab = old_keypoint - iold_keypoint;
251
252 // Weight used for Bilinear-Interpolation on Scharr images
253 // w_scharr.s0 = (1.0f - ab.x) * (1.0f - ab.y)
254 // w_scharr.s1 = ab.x * (1.0f - ab.y)
255 // w_scharr.s2 = (1.0f - ab.x) * ab.y
256 // w_scharr.s3 = ab.x * ab.y
257
258 float4 w_scharr;
259 w_scharr.s3 = ab.x * ab.y;
260 w_scharr.s0 = w_scharr.s3 + 1.0f - ab.x - ab.y;
261 w_scharr.s12 = ab - (float2)w_scharr.s3;
262
263 // Weight used for Bilinear-Interpolation on Old and New images
264 // w.s0 = round(w_scharr.s0 * D0)
265 // w.s1 = round(w_scharr.s1 * D0)
266 // w.s2 = round(w_scharr.s2 * D0)
267 // w.s3 = w.s3 = D0 - w.s0 - w.s1 - w.s2
268
269 float4 w;
270 w = round(w_scharr * (float4)D0);
271 w.s3 = D0 - w.s0 - w.s1 - w.s2; // Added for matching VX implementation
272
273 // G.s0 = A11, G.s1 = A12, G.s2 = A22, G.s3 = min_eig
274 int4 iG = (int4)0;
275
276 // Window offset
277 int window_offset = idx * window_dimension_pow2;
278
279 // Compute Spatial Gradient Matrix G
280 for(ushort ky = 0; ky < window_dimension; ++ky)
281 {
282 int offset_y = iold_keypoint.y + ky;
283 for(ushort kx = 0; kx < window_dimension; ++kx)
284 {
285 int offset_x = iold_keypoint.x + kx;
286 float4 px;
287
288 // Load values from old_image for computing the bilinear interpolation
289 px = convert_float4((uchar4)(vload2(0, offset(&old_image, offset_x, offset_y)),
290 vload2(0, offset(&old_image, offset_x, offset_y + 1))));
291
292 // old_i.s0 = ival, old_i.s1 = ixval, old_i.s2 = iyval, old_i.s3 = dummy
293 float4 old_i;
294
295 // Compute bilinear interpolation (with D1 scale factor) for ival
296 old_i.s0 = dot(px, w) * D1;
297
298 // Load values from old_scharr_gx for computing the bilinear interpolation
299 px = convert_float4((short4)(vload2(0, (__global short *)offset(&old_scharr_gx, offset_x, offset_y)),
300 vload2(0, (__global short *)offset(&old_scharr_gx, offset_x, offset_y + 1))));
301
302 // Compute bilinear interpolation for ixval
303 old_i.s1 = dot(px, w_scharr);
304
305 // Load values from old_scharr_gy for computing the bilinear interpolation
306 px = convert_float4((short4)(vload2(0, (__global short *)offset(&old_scharr_gy, offset_x, offset_y)),
307 vload2(0, (__global short *)offset(&old_scharr_gy, offset_x, offset_y + 1))));
308
309 // Compute bilinear interpolation for iyval
310 old_i.s2 = dot(px, w_scharr);
311
312 // Rounding (it could be omitted. Used just for matching the VX implementation)
313 int4 iold = convert_int4(round(old_i));
314
315 // Accumulate values in the Spatial Gradient Matrix
316 iG.s0 += (int)(iold.s1 * iold.s1);
317 iG.s1 += (int)(iold.s1 * iold.s2);
318 iG.s2 += (int)(iold.s2 * iold.s2);
319
320 // Store ival, ixval and iyval
321 iold_val[window_offset + kx] = convert_short4(iold);
322 }
323 window_offset += window_dimension;
324 }
325
326 // Scale iA11, iA12 and iA22
327 float4 G = convert_float4(iG) * (float4)FLT_SCALE;
328
329 // Compute minimum eigen value
330 G.s3 = (float)(G.s2 + G.s0 - sqrt(pown(G.s0 - G.s2, 2) + 4.0f * G.s1 * G.s1)) * eig_const;
331
332 // Store A11. A11, A22 and min_eig
333 coeff[idx] = G;
334}
335
336/** Computes the motion vector for a given keypoint
337 *
338 * @param[in] new_image_ptr Pointer to the input new image. Supported data types: U8
339 * @param[in] new_image_stride_x Stride of the input new image in X dimension (in bytes)
340 * @param[in] new_image_step_x new_image_stride_x * number of elements along X processed per workitem(in bytes)
341 * @param[in] new_image_stride_y Stride of the input new image in Y dimension (in bytes)
342 * @param[in] new_image_step_y new_image_stride_y * number of elements along Y processed per workitem(in bytes)
343 * @param[in] new_image_offset_first_element_in_bytes The offset of the first element in the input new image
344 * @param[in, out] new_points An output array of key points. Those key points are defined at the new_images high resolution pyramid
345 * @param[in] coeff The | A11 | A12 | A22 | min_eig | for each keypoint
346 * @param[in] iold_val The | ival | ixval | iyval | dummy | for each point in the window centered on old_keypoint
347 * @param[in] window_dimension The size of the window on which to perform the algorithm
348 * @param[in] window_dimension_pow2 The squared size of the window on which to perform the algorithm
349 * @param[in] half_window The half size of the window on which to perform the algorithm
350 * @param[in] num_iterations The maximum number of iterations
351 * @param[in] epsilon The value for terminating the algorithm.
352 * @param[in] border_limits It stores the right border limit (width - window_dimension - 1, height - window_dimension - 1,)
353 * @param[in] eig_const 1.0f / (float)(2.0f * window_dimension * window_dimension)
354 * @param[in] level0 It is set to 1 if level of pyramid = 0
355 * @param[in] term_iteration It is set to 1 if termination = VX_TERM_CRITERIA_ITERATIONS
356 * @param[in] term_epsilon It is set to 1 if termination = VX_TERM_CRITERIA_EPSILON
357 */
358void __kernel lktracker_stage1(
359 IMAGE_DECLARATION(new_image),
360 __global float4 *new_points,
361 __global float4 *coeff,
362 __global short4 *iold_val,
363 const int window_dimension,
364 const int window_dimension_pow2,
365 const int half_window,
366 const int num_iterations,
367 const float epsilon,
368 const float3 border_limits,
369 const float eig_const,
370 const int level0,
371 const int term_iteration,
372 const int term_epsilon)
373{
374 int idx = get_global_id(0);
375 Image new_image = CONVERT_TO_IMAGE_STRUCT_NO_STEP(new_image);
376
377 // G.s0 = A11, G.s1 = A12, G.s2 = A22, G.s3 = min_eig
378 float4 G = coeff[idx];
379
380 // Determinant
381 float D = G.s0 * G.s2 - G.s1 * G.s1;
382
383 // Check if it is a good point to track
384 if(G.s3 < EIGENVALUE_THR || D < DETERMINANT_THR)
385 {
386 if(level0 == 1)
387 {
388 // Invalidate tracked point as we are at level 0
389 new_points[idx].s2 = 0;
390 }
391
392 return;
393 }
394
395 // Compute inverse
396 //D = native_recip(D);
397 D = 1.0 / D;
398
399 // Get new keypoint
400 float2 new_keypoint = new_points[idx].xy - (float)half_window;
401
402 // Get new point
403 float2 out_new_point = new_points[idx].xy;
404
405 // Keep delta obtained in the previous iteration
406 float2 prev_delta = (float2)0.0f;
407
408 int j = 0;
409 while(j < num_iterations)
410 {
411 // Get the floor value
412 float2 inew_keypoint = floor(new_keypoint);
413
414 // Check if using the window dimension we can go out of boundary in the following for loops. If so, invalidate the tracked point
415 if(any(inew_keypoint < border_limits.zz) || any(inew_keypoint >= border_limits.xy))
416 {
417 if(level0 == 1)
418 {
419 // Invalidate tracked point as we are at level 0
420 new_points[idx].s2 = 0.0f;
421 }
422 else
423 {
424 new_points[idx].xy = out_new_point;
425 }
426
427 return;
428 }
429
430 // Compute weight for the bilinear interpolation
431 float2 ab = new_keypoint - inew_keypoint;
432
433 // Weight used for Bilinear-Interpolation on Old and New images
434 // w.s0 = round((1.0f - ab.x) * (1.0f - ab.y) * D0)
435 // w.s1 = round(ab.x * (1.0f - ab.y) * D0)
436 // w.s2 = round((1.0f - ab.x) * ab.y * D0)
437 // w.s3 = D0 - w.s0 - w.s1 - w.s2
438
439 float4 w;
440 w.s3 = ab.x * ab.y;
441 w.s0 = w.s3 + 1.0f - ab.x - ab.y;
442 w.s12 = ab - (float2)w.s3;
443 w = round(w * (float4)D0);
444 w.s3 = D0 - w.s0 - w.s1 - w.s2;
445
446 // Mismatch vector
447 int2 ib = 0;
448
449 // Old val offset
450 int old_val_offset = idx * window_dimension_pow2;
451
452 for(int ky = 0; ky < window_dimension; ++ky)
453 {
454 for(int kx = 0; kx < window_dimension; ++kx)
455 {
456 // ival, ixval and iyval have been computed in the previous stage
457 int4 old_ival = convert_int4(iold_val[old_val_offset]);
458
459 // Load values from old_image for computing the bilinear interpolation
460 float4 px = convert_float4((uchar4)(vload2(0, offset(&new_image, inew_keypoint.x + kx, inew_keypoint.y + ky)),
461 vload2(0, offset(&new_image, inew_keypoint.x + kx, inew_keypoint.y + ky + 1))));
462
463 // Compute bilinear interpolation on new image
464 int jval = (int)round(dot(px, w) * D1);
465
466 // Compute luminance difference
467 int diff = (int)(jval - old_ival.s0);
468
469 // Accumulate values in mismatch vector
470 ib += (diff * old_ival.s12);
471
472 // Update old val offset
473 old_val_offset++;
474 }
475 }
476
477 float2 b = convert_float2(ib) * (float2)FLT_SCALE;
478
479 // Optical Flow
480 float2 delta;
481
482 delta.x = (float)((G.s1 * b.y - G.s2 * b.x) * D);
483 delta.y = (float)((G.s1 * b.x - G.s0 * b.y) * D);
484
485 // Update new point coordinate
486 new_keypoint += delta;
487
488 out_new_point = new_keypoint + (float2)half_window;
489
490 if(term_epsilon == 1)
491 {
492 float mag2 = dot(delta, delta);
493
494 if(mag2 <= epsilon)
495 {
496 new_points[idx].xy = out_new_point;
497
498 return;
499 }
500 }
501
502 // Check convergence analyzing the previous delta
503 if(j > 0 && all(fabs(delta + prev_delta) < (float2)0.01f))
504 {
505 out_new_point -= delta * (float2)0.5f;
506
507 new_points[idx].xy = out_new_point;
508
509 return;
510 }
511
512 // Update previous delta
513 prev_delta = delta;
514
515 if(term_iteration == 1)
516 {
517 j++;
518 }
519 }
520
521 new_points[idx].xy = out_new_point;
522}