blob: fdc2ef8333b5e69095bd3564166651211ec715d7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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_CLLKTRACKERKERNEL_H
25#define ARM_COMPUTE_CLLKTRACKERKERNEL_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/CL/ICLArray.h"
28#include "arm_compute/core/CL/ICLKernel.h"
29#include "arm_compute/core/Types.h"
30
31#include <cstddef>
32#include <cstdint>
33
34namespace arm_compute
35{
36class ICLTensor;
37
38/** Internal keypoint structure for Lucas-Kanade Optical Flow */
39struct CLLKInternalKeypoint
40{
41 float x{ 0.f }; /**< x coordinate of the keypoint */
42 float y{ 0.f }; /**< y coordinate of the keypoint */
43 float tracking_status{ 0.f }; /**< the tracking status of the keypoint */
44 float dummy{ 0.f }; /**< Dummy field, to make sure the data structure 128-bit align, so that GPU can use vload4 */
45};
46
47/** Structure for storing Spatial Gradient Matrix and the minimum eigenvalue for each keypoint */
48struct CLCoefficientTable
49{
50 float A11; /**< iA11 * FLT_SCALE */
51 float A12; /**< iA11 * FLT_SCALE */
52 float A22; /**< iA11 * FLT_SCALE */
53 float min_eig; /**< Minimum eigenvalue */
54};
55
56/** Structure for storing ival, ixval and iyval for each point inside the window */
57struct CLOldValue
58{
59 int16_t ival; /**< ival extracts from old image */
60 int16_t ixval; /**< ixval extracts from scharr Gx image */
61 int16_t iyval; /**< iyval extracts from scharr Gy image */
62 int16_t dummy; /**< Dummy field, to make sure the data structure 128-bit align, so that GPU can use vload4 */
63};
64
Alex Gildayc357c472018-03-21 13:54:09 +000065/** Interface for OpenCL Array of Internal Key Points. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066using ICLLKInternalKeypointArray = ICLArray<CLLKInternalKeypoint>;
Alex Gildayc357c472018-03-21 13:54:09 +000067/** Interface for OpenCL Array of Coefficient Tables. */
68using ICLCoefficientTableArray = ICLArray<CLCoefficientTable>;
69/** Interface for OpenCL Array of Old Values. */
70using ICLOldValArray = ICLArray<CLOldValue>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
72/** Interface to run the initialization step of LKTracker */
73class CLLKTrackerInitKernel : public ICLKernel
74{
75public:
76 /** Initialise the kernel input and output
77 *
78 * @param[in] old_points Pointer to the @ref ICLKeyPointArray storing old key points
79 * @param[in] new_points_estimates Pointer to the @ref ICLKeyPointArray storing new estimates key points
80 * @param[out] old_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint old points
81 * @param[out] new_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint new points
82 * @param[in] use_initial_estimate The flag to indicate whether the initial estimated position should be used
83 * @param[in] level The pyramid level
84 * @param[in] num_levels The number of pyramid levels
85 * @param[in] pyramid_scale Scale factor used for generating the pyramid
86 */
87 void configure(const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates,
88 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
89 bool use_initial_estimate, size_t level, size_t num_levels, float pyramid_scale);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010090 /** Initialise the kernel input and output
91 *
92 * @param[in] compile_context The compile context to be used.
93 * @param[in] old_points Pointer to the @ref ICLKeyPointArray storing old key points
94 * @param[in] new_points_estimates Pointer to the @ref ICLKeyPointArray storing new estimates key points
95 * @param[out] old_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint old points
96 * @param[out] new_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint new points
97 * @param[in] use_initial_estimate The flag to indicate whether the initial estimated position should be used
98 * @param[in] level The pyramid level
99 * @param[in] num_levels The number of pyramid levels
100 * @param[in] pyramid_scale Scale factor used for generating the pyramid
101 */
Manuel Bottini679fc962020-04-21 16:08:53 +0100102 void configure(const CLCompileContext &compile_context, const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100103 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
104 bool use_initial_estimate, size_t level, size_t num_levels, float pyramid_scale);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106 // Inherited methods overridden:
107 void run(const Window &window, cl::CommandQueue &queue) override;
108};
109
110/** Interface to run the finalize step of LKTracker, where it truncates the coordinates stored in new_points array */
111class CLLKTrackerFinalizeKernel : public ICLKernel
112{
113public:
114 /** Initialise the kernel input and output
115 *
116 * @param[in] new_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint new points
117 * @param[out] new_points Pointer to the @ref ICLKeyPointArray storing new key points
118 */
119 void configure(ICLLKInternalKeypointArray *new_points_internal, ICLKeyPointArray *new_points);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100120 /** Initialise the kernel input and output
121 *
122 * @param[in] compile_context The compile context to be used.
123 * @param[in] new_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint new points
124 * @param[out] new_points Pointer to the @ref ICLKeyPointArray storing new key points
125 */
Manuel Bottini679fc962020-04-21 16:08:53 +0100126 void configure(const CLCompileContext &compile_context, ICLLKInternalKeypointArray *new_points_internal, ICLKeyPointArray *new_points);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127
128 // Inherited methods overridden:
129 void run(const Window &window, cl::CommandQueue &queue) override;
130};
131
132/** Interface to run the first stage of LKTracker, where A11, A12, A22, min_eig, ival, ixval and iyval are computed */
133class CLLKTrackerStage0Kernel : public ICLKernel
134{
135public:
136 /** Default constructor */
137 CLLKTrackerStage0Kernel();
138 /** Prevent instances of this class from being copied (As this class contains pointers) */
139 CLLKTrackerStage0Kernel(const CLLKTrackerStage0Kernel &) = delete;
140 /** Prevent instances of this class from being copied (As this class contains pointers) */
141 CLLKTrackerStage0Kernel &operator=(const CLLKTrackerStage0Kernel &) = delete;
142 /** Allow instances of this class to be moved */
143 CLLKTrackerStage0Kernel(CLLKTrackerStage0Kernel &&) = default;
144 /** Allow instances of this class to be moved */
145 CLLKTrackerStage0Kernel &operator=(CLLKTrackerStage0Kernel &&) = default;
146 /** Initialise the kernel input and output
147 *
148 * @param[in] old_input Pointer to the input old tensor. Data types supported: U8
149 * @param[in] old_scharr_gx Pointer to the input scharr X tensor. Data types supported: S16
150 * @param[in] old_scharr_gy Pointer to the input scharr Y tensor. Data types supported: S16
151 * @param[in] old_points_internal Pointer to the array of CLLKInternalKeypoint old points
152 * @param[in, out] new_points_internal Pointer to the array of CLLKInternalKeypoint new points
153 * @param[out] coeff_table Pointer to the array holding the Spatial Gradient coefficients
154 * @param[out] old_ival Pointer to the array holding internal values
155 * @param[in] window_dimension The size of the window on which to perform the algorithm
156 * @param[in] level The pyramid level
157 */
158 void configure(const ICLTensor *old_input, const ICLTensor *old_scharr_gx, const ICLTensor *old_scharr_gy,
159 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
160 ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
161 size_t window_dimension, size_t level);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100162 /** Initialise the kernel input and output
163 *
164 * @param[in] compile_context The compile context to be used.
165 * @param[in] old_input Pointer to the input old tensor. Data types supported: U8
166 * @param[in] old_scharr_gx Pointer to the input scharr X tensor. Data types supported: S16
167 * @param[in] old_scharr_gy Pointer to the input scharr Y tensor. Data types supported: S16
168 * @param[in] old_points_internal Pointer to the array of CLLKInternalKeypoint old points
169 * @param[in, out] new_points_internal Pointer to the array of CLLKInternalKeypoint new points
170 * @param[out] coeff_table Pointer to the array holding the Spatial Gradient coefficients
171 * @param[out] old_ival Pointer to the array holding internal values
172 * @param[in] window_dimension The size of the window on which to perform the algorithm
173 * @param[in] level The pyramid level
174 */
Manuel Bottini679fc962020-04-21 16:08:53 +0100175 void configure(const CLCompileContext &compile_context, const ICLTensor *old_input, const ICLTensor *old_scharr_gx, const ICLTensor *old_scharr_gy,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100176 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
177 ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
178 size_t window_dimension, size_t level);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
180 // Inherited methods overridden:
181 void run(const Window &window, cl::CommandQueue &queue) override;
182
183private:
184 const ICLTensor *_old_input;
185 const ICLTensor *_old_scharr_gx;
186 const ICLTensor *_old_scharr_gy;
187};
188
189/** Interface to run the second stage of LKTracker, where the motion vectors of the given points are computed */
190class CLLKTrackerStage1Kernel : public ICLKernel
191{
192public:
193 /** Default constructor */
194 CLLKTrackerStage1Kernel();
195 /** Prevent instances of this class from being copied (As this class contains pointers) */
196 CLLKTrackerStage1Kernel(const CLLKTrackerStage1Kernel &) = delete;
197 /** Prevent instances of this class from being copied (As this class contains pointers) */
198 CLLKTrackerStage1Kernel &operator=(const CLLKTrackerStage1Kernel &) = delete;
199 /** Allow instances of this class to be moved */
200 CLLKTrackerStage1Kernel(CLLKTrackerStage1Kernel &&) = default;
201 /** Allow instances of this class to be moved */
202 CLLKTrackerStage1Kernel &operator=(CLLKTrackerStage1Kernel &&) = default;
203 /** Initialise the kernel input and output
204 *
205 * @param[in] new_input Pointer to the input new tensor. Data types supported: U8
206 * @param[in, out] new_points_internal Pointer to the array of CLLKInternalKeypoint for new points
207 * @param[in] coeff_table Pointer to the array holding the Spatial Gradient coefficients
208 * @param[in] old_ival Pointer to the array holding internal values
209 * @param[in] termination The criteria to terminate the search of each keypoint.
210 * @param[in] epsilon The error for terminating the algorithm
211 * @param[in] num_iterations The maximum number of iterations before terminating the algorithm
212 * @param[in] window_dimension The size of the window on which to perform the algorithm
213 * @param[in] level The pyramid level
214 */
215 void configure(const ICLTensor *new_input, ICLLKInternalKeypointArray *new_points_internal, ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
216 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, size_t level);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100217 /** Initialise the kernel input and output
218 *
219 * @param[in] compile_context The compile context to be used.
220 * @param[in] new_input Pointer to the input new tensor. Data types supported: U8
221 * @param[in, out] new_points_internal Pointer to the array of CLLKInternalKeypoint for new points
222 * @param[in] coeff_table Pointer to the array holding the Spatial Gradient coefficients
223 * @param[in] old_ival Pointer to the array holding internal values
224 * @param[in] termination The criteria to terminate the search of each keypoint.
225 * @param[in] epsilon The error for terminating the algorithm
226 * @param[in] num_iterations The maximum number of iterations before terminating the algorithm
227 * @param[in] window_dimension The size of the window on which to perform the algorithm
228 * @param[in] level The pyramid level
229 */
Manuel Bottini679fc962020-04-21 16:08:53 +0100230 void configure(const CLCompileContext &compile_context, const ICLTensor *new_input, ICLLKInternalKeypointArray *new_points_internal, ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100231 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, size_t level);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232
233 // Inherited methods overridden:
234 void run(const Window &window, cl::CommandQueue &queue) override;
235
236private:
237 const ICLTensor *_new_input;
238};
Gian Marco Iodicef670a0a2017-09-18 12:20:45 +0100239} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000240#endif /*ARM_COMPUTE_CLLKTRACKERKERNEL_H */