blob: f2105582eba635fce583f85ec312d2c487c1ab78 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbiere8a49832018-01-18 10:04:05 +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#ifndef __ARM_COMPUTE_LKTRACKERKERNEL_H__
25#define __ARM_COMPUTE_LKTRACKERKERNEL_H__
26
27#include "arm_compute/core/IArray.h"
28#include "arm_compute/core/NEON/INEKernel.h"
29#include "arm_compute/core/Types.h"
30
31#include <cstddef>
32#include <cstdint>
33#include <tuple>
34#include <utility>
35
36namespace arm_compute
37{
38class ITensor;
39
40/** Internal keypoint class for Lucas-Kanade Optical Flow */
41struct NELKInternalKeypoint
42{
43 float x{ 0.f }; /**< x coordinate of the keypoint */
44 float y{ 0.f }; /**< y coordinate of the keypoint */
45 bool tracking_status{ false }; /**< the tracking status of the keypoint */
46};
47
48using INELKInternalKeypointArray = IArray<NELKInternalKeypoint>;
49
50/** Interface for the Lucas-Kanade tracker kernel */
51class NELKTrackerKernel : public INEKernel
52{
53public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000054 const char *name() const override
55 {
56 return "NELKTrackerKernel";
57 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 /** Default constructor */
59 NELKTrackerKernel();
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 NELKTrackerKernel(const NELKTrackerKernel &) = delete;
62 /** Prevent instances of this class from being copied (As this class contains pointers) */
63 NELKTrackerKernel &operator=(const NELKTrackerKernel &) = delete;
64 /** Allow instances of this class to be moved */
65 NELKTrackerKernel(NELKTrackerKernel &&) = default;
66 /** Allow instances of this class to be moved */
67 NELKTrackerKernel &operator=(NELKTrackerKernel &&) = default;
68 /** Default destructor */
69 ~NELKTrackerKernel() = default;
70
71 /** Initialise the kernel input and output
72 *
73 * @param[in] input_old Pointer to the input old tensor. Data type supported: U8
74 * @param[in] input_new Pointer to the input new tensor. Data type supported. U8
75 * @param[in] old_scharr_gx Pointer to the input scharr X tensor. Data type supported: S16
76 * @param[in] old_scharr_gy Pointer to the input scharr Y tensor. Data type supported: S16
77 * @param[in] old_points Pointer to the IKeyPointArray storing old key points
78 * @param[in] new_points_estimates Pointer to the IKeyPointArray storing new estimates key points
79 * @param[out] new_points Pointer to the IKeyPointArray storing new key points
80 * @param[in, out] old_points_internal Pointer to the array of NELKInternalKeypoint for old points
81 * @param[out] new_points_internal Pointer to the array of NELKInternalKeypoint for new points
82 * @param[in] termination The criteria to terminate the search of each keypoint.
83 * @param[in] use_initial_estimate The flag to indicate whether the initial estimated position should be used
84 * @param[in] epsilon The error for terminating the algorithm
85 * @param[in] num_iterations The maximum number of iterations before terminate the algorithm
86 * @param[in] window_dimension The size of the window on which to perform the algorithm
87 * @param[in] level The pyramid level
88 * @param[in] num_levels The number of pyramid levels
89 * @param[in] pyramid_scale Scale factor used for generating the pyramid
90 */
91 void configure(const ITensor *input_old, const ITensor *input_new, const ITensor *old_scharr_gx, const ITensor *old_scharr_gy,
92 const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates, IKeyPointArray *new_points,
93 INELKInternalKeypointArray *old_points_internal, INELKInternalKeypointArray *new_points_internal,
94 Termination termination, bool use_initial_estimate, float epsilon, unsigned int num_iterations, size_t window_dimension,
95 size_t level, size_t num_levels, float pyramid_scale);
96
97 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010098 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 BorderSize border_size() const override;
100
101private:
102 /** Initialise the array of keypoints in the provide range
103 *
104 * @param[in] start Index of first element in the keypoints array to be initialised
105 * @param[in] end Index after last elelemnt in the keypoints array to be initialised
106 */
107 void init_keypoints(int start, int end);
108 /** Compute the structure tensor A^T * A based on the scharr gradients I_x and I_y
109 *
110 * @param[in] keypoint Keypoint for which gradients are computed
111 * @param[out] bilinear_ix Intermediate interpolated data for X gradient
112 * @param[out] bilinear_iy Intermediate interpolated data for Y gradient
113 *
114 * @return Values A11, A12, A22
115 */
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100116 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 +0100117 /** Compute the vector A^T * b, i.e. -sum(I_d * I_t) for d in {x,y}
118 *
119 * @param[in] old_keypoint Old keypoint for which gradient is computed
120 * @param[in] new_keypoint New keypoint for which gradient is computed
121 * @param[in] bilinear_ix Intermediate interpolated data for X gradient
122 * @param[in] bilinear_iy Intermediate interpolated data for Y gradient
123 *
124 * @return Values b1, b2
125 */
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100126 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 +0100127
128 const ITensor *_input_old;
129 const ITensor *_input_new;
130 const ITensor *_old_scharr_gx;
131 const ITensor *_old_scharr_gy;
132 IKeyPointArray *_new_points;
133 const IKeyPointArray *_new_points_estimates;
134 const IKeyPointArray *_old_points;
135 INELKInternalKeypointArray *_old_points_internal;
136 INELKInternalKeypointArray *_new_points_internal;
137 Termination _termination;
138 bool _use_initial_estimate;
139 float _pyramid_scale;
140 float _epsilon;
141 unsigned int _num_iterations;
142 int _window_dimension;
143 unsigned int _level;
144 unsigned int _num_levels;
145 ValidRegion _valid_region;
146};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100147} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148#endif /*__ARM_COMPUTE_NELKTRACKERKERNEL_H__ */