Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_LKTRACKERKERNEL_H |
| 25 | #define ARM_COMPUTE_LKTRACKERKERNEL_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | class ITensor; |
| 39 | |
| 40 | /** Internal keypoint class for Lucas-Kanade Optical Flow */ |
| 41 | struct 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 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | /** Interface for NEON Array of Internal Key Points. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | using INELKInternalKeypointArray = IArray<NELKInternalKeypoint>; |
| 50 | |
| 51 | /** Interface for the Lucas-Kanade tracker kernel */ |
| 52 | class NELKTrackerKernel : public INEKernel |
| 53 | { |
| 54 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 55 | const char *name() const override |
| 56 | { |
| 57 | return "NELKTrackerKernel"; |
| 58 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | /** Default constructor */ |
| 60 | NELKTrackerKernel(); |
| 61 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 62 | NELKTrackerKernel(const NELKTrackerKernel &) = delete; |
| 63 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 64 | NELKTrackerKernel &operator=(const NELKTrackerKernel &) = delete; |
| 65 | /** Allow instances of this class to be moved */ |
| 66 | NELKTrackerKernel(NELKTrackerKernel &&) = default; |
| 67 | /** Allow instances of this class to be moved */ |
| 68 | NELKTrackerKernel &operator=(NELKTrackerKernel &&) = default; |
| 69 | /** Default destructor */ |
| 70 | ~NELKTrackerKernel() = default; |
| 71 | |
| 72 | /** Initialise the kernel input and output |
| 73 | * |
| 74 | * @param[in] input_old Pointer to the input old tensor. Data type supported: U8 |
| 75 | * @param[in] input_new Pointer to the input new tensor. Data type supported. U8 |
| 76 | * @param[in] old_scharr_gx Pointer to the input scharr X tensor. Data type supported: S16 |
| 77 | * @param[in] old_scharr_gy Pointer to the input scharr Y tensor. Data type supported: S16 |
| 78 | * @param[in] old_points Pointer to the IKeyPointArray storing old key points |
| 79 | * @param[in] new_points_estimates Pointer to the IKeyPointArray storing new estimates key points |
| 80 | * @param[out] new_points Pointer to the IKeyPointArray storing new key points |
| 81 | * @param[in, out] old_points_internal Pointer to the array of NELKInternalKeypoint for old points |
| 82 | * @param[out] new_points_internal Pointer to the array of NELKInternalKeypoint for new points |
| 83 | * @param[in] termination The criteria to terminate the search of each keypoint. |
| 84 | * @param[in] use_initial_estimate The flag to indicate whether the initial estimated position should be used |
| 85 | * @param[in] epsilon The error for terminating the algorithm |
| 86 | * @param[in] num_iterations The maximum number of iterations before terminate the algorithm |
| 87 | * @param[in] window_dimension The size of the window on which to perform the algorithm |
| 88 | * @param[in] level The pyramid level |
| 89 | * @param[in] num_levels The number of pyramid levels |
| 90 | * @param[in] pyramid_scale Scale factor used for generating the pyramid |
| 91 | */ |
| 92 | void configure(const ITensor *input_old, const ITensor *input_new, const ITensor *old_scharr_gx, const ITensor *old_scharr_gy, |
| 93 | const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates, IKeyPointArray *new_points, |
| 94 | INELKInternalKeypointArray *old_points_internal, INELKInternalKeypointArray *new_points_internal, |
| 95 | Termination termination, bool use_initial_estimate, float epsilon, unsigned int num_iterations, size_t window_dimension, |
| 96 | size_t level, size_t num_levels, float pyramid_scale); |
| 97 | |
| 98 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 99 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | BorderSize border_size() const override; |
| 101 | |
| 102 | private: |
| 103 | /** Initialise the array of keypoints in the provide range |
| 104 | * |
| 105 | * @param[in] start Index of first element in the keypoints array to be initialised |
| 106 | * @param[in] end Index after last elelemnt in the keypoints array to be initialised |
| 107 | */ |
| 108 | void init_keypoints(int start, int end); |
| 109 | /** Compute the structure tensor A^T * A based on the scharr gradients I_x and I_y |
| 110 | * |
| 111 | * @param[in] keypoint Keypoint for which gradients are computed |
| 112 | * @param[out] bilinear_ix Intermediate interpolated data for X gradient |
| 113 | * @param[out] bilinear_iy Intermediate interpolated data for Y gradient |
| 114 | * |
| 115 | * @return Values A11, A12, A22 |
| 116 | */ |
Michalis Spyrou | 490bf2e | 2017-09-29 11:24:55 +0100 | [diff] [blame] | 117 | std::tuple<int, int, int> compute_spatial_gradient_matrix(const NELKInternalKeypoint &keypoint, int32_t *bilinear_ix, int32_t *bilinear_iy); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | /** Compute the vector A^T * b, i.e. -sum(I_d * I_t) for d in {x,y} |
| 119 | * |
| 120 | * @param[in] old_keypoint Old keypoint for which gradient is computed |
| 121 | * @param[in] new_keypoint New keypoint for which gradient is computed |
| 122 | * @param[in] bilinear_ix Intermediate interpolated data for X gradient |
| 123 | * @param[in] bilinear_iy Intermediate interpolated data for Y gradient |
| 124 | * |
| 125 | * @return Values b1, b2 |
| 126 | */ |
Michalis Spyrou | 490bf2e | 2017-09-29 11:24:55 +0100 | [diff] [blame] | 127 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | |
| 129 | const ITensor *_input_old; |
| 130 | const ITensor *_input_new; |
| 131 | const ITensor *_old_scharr_gx; |
| 132 | const ITensor *_old_scharr_gy; |
| 133 | IKeyPointArray *_new_points; |
| 134 | const IKeyPointArray *_new_points_estimates; |
| 135 | const IKeyPointArray *_old_points; |
| 136 | INELKInternalKeypointArray *_old_points_internal; |
| 137 | INELKInternalKeypointArray *_new_points_internal; |
| 138 | Termination _termination; |
| 139 | bool _use_initial_estimate; |
| 140 | float _pyramid_scale; |
| 141 | float _epsilon; |
| 142 | unsigned int _num_iterations; |
| 143 | int _window_dimension; |
| 144 | unsigned int _level; |
| 145 | unsigned int _num_levels; |
| 146 | ValidRegion _valid_region; |
| 147 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 148 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 149 | #endif /*ARM_COMPUTE_NELKTRACKERKERNEL_H */ |