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