Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 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_CLLKTRACKERKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_CLLKTRACKERKERNEL_H__ |
| 26 | |
| 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 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | class ICLTensor; |
| 37 | |
| 38 | /** Internal keypoint structure for Lucas-Kanade Optical Flow */ |
| 39 | struct 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 */ |
| 48 | struct 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 */ |
| 57 | struct 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 | |
| 65 | using ICLLKInternalKeypointArray = ICLArray<CLLKInternalKeypoint>; |
| 66 | using ICLCoefficientTableArray = ICLArray<CLCoefficientTable>; |
| 67 | using ICLOldValArray = ICLArray<CLOldValue>; |
| 68 | |
| 69 | /** Interface to run the initialization step of LKTracker */ |
| 70 | class CLLKTrackerInitKernel : public ICLKernel |
| 71 | { |
| 72 | public: |
| 73 | /** Initialise the kernel input and output |
| 74 | * |
| 75 | * @param[in] old_points Pointer to the @ref ICLKeyPointArray storing old key points |
| 76 | * @param[in] new_points_estimates Pointer to the @ref ICLKeyPointArray storing new estimates key points |
| 77 | * @param[out] old_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint old points |
| 78 | * @param[out] new_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint new points |
| 79 | * @param[in] use_initial_estimate The flag to indicate whether the initial estimated position should be used |
| 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 ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates, |
| 85 | ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal, |
| 86 | bool use_initial_estimate, size_t level, size_t num_levels, float pyramid_scale); |
| 87 | |
| 88 | // Inherited methods overridden: |
| 89 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 90 | }; |
| 91 | |
| 92 | /** Interface to run the finalize step of LKTracker, where it truncates the coordinates stored in new_points array */ |
| 93 | class CLLKTrackerFinalizeKernel : public ICLKernel |
| 94 | { |
| 95 | public: |
| 96 | /** Initialise the kernel input and output |
| 97 | * |
| 98 | * @param[in] new_points_internal Pointer to the array of internal @ref CLLKInternalKeypoint new points |
| 99 | * @param[out] new_points Pointer to the @ref ICLKeyPointArray storing new key points |
| 100 | */ |
| 101 | void configure(ICLLKInternalKeypointArray *new_points_internal, ICLKeyPointArray *new_points); |
| 102 | |
| 103 | // Inherited methods overridden: |
| 104 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 105 | }; |
| 106 | |
| 107 | /** Interface to run the first stage of LKTracker, where A11, A12, A22, min_eig, ival, ixval and iyval are computed */ |
| 108 | class CLLKTrackerStage0Kernel : public ICLKernel |
| 109 | { |
| 110 | public: |
| 111 | /** Default constructor */ |
| 112 | CLLKTrackerStage0Kernel(); |
| 113 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 114 | CLLKTrackerStage0Kernel(const CLLKTrackerStage0Kernel &) = delete; |
| 115 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 116 | CLLKTrackerStage0Kernel &operator=(const CLLKTrackerStage0Kernel &) = delete; |
| 117 | /** Allow instances of this class to be moved */ |
| 118 | CLLKTrackerStage0Kernel(CLLKTrackerStage0Kernel &&) = default; |
| 119 | /** Allow instances of this class to be moved */ |
| 120 | CLLKTrackerStage0Kernel &operator=(CLLKTrackerStage0Kernel &&) = default; |
| 121 | /** Initialise the kernel input and output |
| 122 | * |
| 123 | * @param[in] old_input Pointer to the input old tensor. Data types supported: U8 |
| 124 | * @param[in] old_scharr_gx Pointer to the input scharr X tensor. Data types supported: S16 |
| 125 | * @param[in] old_scharr_gy Pointer to the input scharr Y tensor. Data types supported: S16 |
| 126 | * @param[in] old_points_internal Pointer to the array of CLLKInternalKeypoint old points |
| 127 | * @param[in, out] new_points_internal Pointer to the array of CLLKInternalKeypoint new points |
| 128 | * @param[out] coeff_table Pointer to the array holding the Spatial Gradient coefficients |
| 129 | * @param[out] old_ival Pointer to the array holding internal values |
| 130 | * @param[in] window_dimension The size of the window on which to perform the algorithm |
| 131 | * @param[in] level The pyramid level |
| 132 | */ |
| 133 | void configure(const ICLTensor *old_input, const ICLTensor *old_scharr_gx, const ICLTensor *old_scharr_gy, |
| 134 | ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal, |
| 135 | ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival, |
| 136 | size_t window_dimension, size_t level); |
| 137 | |
| 138 | // Inherited methods overridden: |
| 139 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 140 | |
| 141 | private: |
| 142 | const ICLTensor *_old_input; |
| 143 | const ICLTensor *_old_scharr_gx; |
| 144 | const ICLTensor *_old_scharr_gy; |
| 145 | }; |
| 146 | |
| 147 | /** Interface to run the second stage of LKTracker, where the motion vectors of the given points are computed */ |
| 148 | class CLLKTrackerStage1Kernel : public ICLKernel |
| 149 | { |
| 150 | public: |
| 151 | /** Default constructor */ |
| 152 | CLLKTrackerStage1Kernel(); |
| 153 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 154 | CLLKTrackerStage1Kernel(const CLLKTrackerStage1Kernel &) = delete; |
| 155 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 156 | CLLKTrackerStage1Kernel &operator=(const CLLKTrackerStage1Kernel &) = delete; |
| 157 | /** Allow instances of this class to be moved */ |
| 158 | CLLKTrackerStage1Kernel(CLLKTrackerStage1Kernel &&) = default; |
| 159 | /** Allow instances of this class to be moved */ |
| 160 | CLLKTrackerStage1Kernel &operator=(CLLKTrackerStage1Kernel &&) = default; |
| 161 | /** Initialise the kernel input and output |
| 162 | * |
| 163 | * @param[in] new_input Pointer to the input new tensor. Data types supported: U8 |
| 164 | * @param[in, out] new_points_internal Pointer to the array of CLLKInternalKeypoint for new points |
| 165 | * @param[in] coeff_table Pointer to the array holding the Spatial Gradient coefficients |
| 166 | * @param[in] old_ival Pointer to the array holding internal values |
| 167 | * @param[in] termination The criteria to terminate the search of each keypoint. |
| 168 | * @param[in] epsilon The error for terminating the algorithm |
| 169 | * @param[in] num_iterations The maximum number of iterations before terminating the algorithm |
| 170 | * @param[in] window_dimension The size of the window on which to perform the algorithm |
| 171 | * @param[in] level The pyramid level |
| 172 | */ |
| 173 | void configure(const ICLTensor *new_input, ICLLKInternalKeypointArray *new_points_internal, ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival, |
| 174 | Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, size_t level); |
| 175 | |
| 176 | // Inherited methods overridden: |
| 177 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 178 | |
| 179 | private: |
| 180 | const ICLTensor *_new_input; |
| 181 | }; |
| 182 | } |
| 183 | #endif /*__ARM_COMPUTE_CLLKTRACKERKERNEL_H__ */ |