blob: bc4f6ce29643537271ccef1855cd3a789c3634e1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2016-2021 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_LKTRACKERKERNEL_H
25#define ARM_COMPUTE_LKTRACKERKERNEL_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/IArray.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Types.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010029#include "src/core/NEON/INEKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31#include <cstddef>
32#include <cstdint>
33#include <tuple>
34#include <utility>
35
36namespace arm_compute
37{
38class ITensor;
39
Sheri Zhangac6499a2021-02-10 15:32:38 +000040/** Interface for Neon Array of Internal Key Points. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041using INELKInternalKeypointArray = IArray<NELKInternalKeypoint>;
42
43/** Interface for the Lucas-Kanade tracker kernel */
44class NELKTrackerKernel : public INEKernel
45{
46public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000047 const char *name() const override
48 {
49 return "NELKTrackerKernel";
50 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 /** Default constructor */
52 NELKTrackerKernel();
53 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 NELKTrackerKernel(const NELKTrackerKernel &) = delete;
55 /** Prevent instances of this class from being copied (As this class contains pointers) */
56 NELKTrackerKernel &operator=(const NELKTrackerKernel &) = delete;
57 /** Allow instances of this class to be moved */
58 NELKTrackerKernel(NELKTrackerKernel &&) = default;
59 /** Allow instances of this class to be moved */
60 NELKTrackerKernel &operator=(NELKTrackerKernel &&) = default;
61 /** Default destructor */
62 ~NELKTrackerKernel() = default;
63
64 /** Initialise the kernel input and output
65 *
66 * @param[in] input_old Pointer to the input old tensor. Data type supported: U8
67 * @param[in] input_new Pointer to the input new tensor. Data type supported. U8
68 * @param[in] old_scharr_gx Pointer to the input scharr X tensor. Data type supported: S16
69 * @param[in] old_scharr_gy Pointer to the input scharr Y tensor. Data type supported: S16
70 * @param[in] old_points Pointer to the IKeyPointArray storing old key points
71 * @param[in] new_points_estimates Pointer to the IKeyPointArray storing new estimates key points
72 * @param[out] new_points Pointer to the IKeyPointArray storing new key points
73 * @param[in, out] old_points_internal Pointer to the array of NELKInternalKeypoint for old points
74 * @param[out] new_points_internal Pointer to the array of NELKInternalKeypoint for new points
75 * @param[in] termination The criteria to terminate the search of each keypoint.
76 * @param[in] use_initial_estimate The flag to indicate whether the initial estimated position should be used
77 * @param[in] epsilon The error for terminating the algorithm
78 * @param[in] num_iterations The maximum number of iterations before terminate the algorithm
79 * @param[in] window_dimension The size of the window on which to perform the algorithm
80 * @param[in] level The pyramid level
81 * @param[in] num_levels The number of pyramid levels
82 * @param[in] pyramid_scale Scale factor used for generating the pyramid
83 */
84 void configure(const ITensor *input_old, const ITensor *input_new, const ITensor *old_scharr_gx, const ITensor *old_scharr_gy,
85 const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates, IKeyPointArray *new_points,
86 INELKInternalKeypointArray *old_points_internal, INELKInternalKeypointArray *new_points_internal,
87 Termination termination, bool use_initial_estimate, float epsilon, unsigned int num_iterations, size_t window_dimension,
88 size_t level, size_t num_levels, float pyramid_scale);
89
90 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010091 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 BorderSize border_size() const override;
93
94private:
95 /** Initialise the array of keypoints in the provide range
96 *
97 * @param[in] start Index of first element in the keypoints array to be initialised
98 * @param[in] end Index after last elelemnt in the keypoints array to be initialised
99 */
100 void init_keypoints(int start, int end);
101 /** Compute the structure tensor A^T * A based on the scharr gradients I_x and I_y
102 *
103 * @param[in] keypoint Keypoint for which gradients are computed
104 * @param[out] bilinear_ix Intermediate interpolated data for X gradient
105 * @param[out] bilinear_iy Intermediate interpolated data for Y gradient
106 *
107 * @return Values A11, A12, A22
108 */
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100109 std::tuple<int, int, int> compute_spatial_gradient_matrix(const NELKInternalKeypoint &keypoint, int32_t *bilinear_ix, int32_t *bilinear_iy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 /** Compute the vector A^T * b, i.e. -sum(I_d * I_t) for d in {x,y}
111 *
112 * @param[in] old_keypoint Old keypoint for which gradient is computed
113 * @param[in] new_keypoint New keypoint for which gradient is computed
114 * @param[in] bilinear_ix Intermediate interpolated data for X gradient
115 * @param[in] bilinear_iy Intermediate interpolated data for Y gradient
116 *
117 * @return Values b1, b2
118 */
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100119 std::pair<int, int> compute_image_mismatch_vector(const NELKInternalKeypoint &old_keypoint, const NELKInternalKeypoint &new_keypoint, const int32_t *bilinear_ix, const int32_t *bilinear_iy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
121 const ITensor *_input_old;
122 const ITensor *_input_new;
123 const ITensor *_old_scharr_gx;
124 const ITensor *_old_scharr_gy;
125 IKeyPointArray *_new_points;
126 const IKeyPointArray *_new_points_estimates;
127 const IKeyPointArray *_old_points;
128 INELKInternalKeypointArray *_old_points_internal;
129 INELKInternalKeypointArray *_new_points_internal;
130 Termination _termination;
131 bool _use_initial_estimate;
132 float _pyramid_scale;
133 float _epsilon;
134 unsigned int _num_iterations;
135 int _window_dimension;
136 unsigned int _level;
137 unsigned int _num_levels;
138 ValidRegion _valid_region;
139};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100140} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000141#endif /*ARM_COMPUTE_NELKTRACKERKERNEL_H */