Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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 | */ |
| 24 | #include "arm_compute/runtime/NEON/functions/NEOpticalFlow.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NELKTrackerKernel.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Window.h" |
| 31 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 32 | #include "arm_compute/runtime/NEON/functions/NEScharr3x3.h" |
| 33 | #include "arm_compute/runtime/Pyramid.h" |
| 34 | #include "arm_compute/runtime/Tensor.h" |
| 35 | #include "arm_compute/runtime/TensorAllocator.h" |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 39 | NEOpticalFlow::NEOpticalFlow(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 40 | : _memory_group(std::move(memory_manager)), |
| 41 | _func_scharr(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 42 | _kernel_tracker(), |
| 43 | _scharr_gx(), |
| 44 | _scharr_gy(), |
| 45 | _new_points(nullptr), |
| 46 | _new_points_estimates(nullptr), |
| 47 | _old_points(nullptr), |
| 48 | _new_points_internal(), |
| 49 | _old_points_internal(), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | _num_levels(0) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void NEOpticalFlow::configure(const Pyramid *old_pyramid, const Pyramid *new_pyramid, const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates, |
| 55 | IKeyPointArray *new_points, Termination termination, float epsilon, unsigned int num_iterations, size_t window_dimension, |
| 56 | bool use_initial_estimate, BorderMode border_mode, uint8_t constant_border_value) |
| 57 | { |
| 58 | ARM_COMPUTE_ERROR_ON(nullptr == old_pyramid); |
| 59 | ARM_COMPUTE_ERROR_ON(nullptr == new_pyramid); |
| 60 | ARM_COMPUTE_ERROR_ON(nullptr == old_points); |
| 61 | ARM_COMPUTE_ERROR_ON(nullptr == new_points_estimates); |
| 62 | ARM_COMPUTE_ERROR_ON(nullptr == new_points); |
| 63 | ARM_COMPUTE_ERROR_ON(old_pyramid->info()->num_levels() != new_pyramid->info()->num_levels()); |
| 64 | ARM_COMPUTE_ERROR_ON(0 == old_pyramid->info()->num_levels()); |
| 65 | ARM_COMPUTE_ERROR_ON(old_pyramid->info()->width() != new_pyramid->info()->width()); |
| 66 | ARM_COMPUTE_ERROR_ON(old_pyramid->info()->height() != new_pyramid->info()->height()); |
| 67 | ARM_COMPUTE_ERROR_ON(use_initial_estimate && old_points->num_values() != new_points_estimates->num_values()); |
| 68 | |
| 69 | _num_levels = old_pyramid->info()->num_levels(); |
| 70 | _old_points = old_points; |
| 71 | _new_points = new_points; |
| 72 | _new_points_estimates = new_points_estimates; |
| 73 | |
| 74 | const float pyr_scale = old_pyramid->info()->scale(); |
| 75 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 76 | _func_scharr.clear(); |
| 77 | _kernel_tracker.clear(); |
| 78 | _scharr_gx.clear(); |
| 79 | _scharr_gy.clear(); |
| 80 | |
| 81 | _func_scharr.resize(_num_levels); |
| 82 | _kernel_tracker.resize(_num_levels); |
| 83 | _scharr_gx.resize(_num_levels); |
| 84 | _scharr_gy.resize(_num_levels); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | _old_points_internal = LKInternalKeypointArray(old_points->num_values()); |
| 87 | _new_points_internal = LKInternalKeypointArray(old_points->num_values()); |
| 88 | _new_points->resize(old_points->num_values()); |
| 89 | |
| 90 | for(unsigned int i = 0; i < _num_levels; ++i) |
| 91 | { |
| 92 | // Get images from the ith level of old and right pyramid |
| 93 | IImage *old_ith_input = old_pyramid->get_pyramid_level(i); |
| 94 | IImage *new_ith_input = new_pyramid->get_pyramid_level(i); |
| 95 | |
| 96 | // Get width and height of images |
| 97 | const unsigned int width_ith = old_ith_input->info()->dimension(0); |
| 98 | const unsigned int height_ith = new_ith_input->info()->dimension(1); |
| 99 | |
| 100 | TensorInfo tensor_info(TensorShape(width_ith, height_ith), Format::S16); |
| 101 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 102 | _scharr_gx[i].allocator()->init(tensor_info); |
| 103 | _scharr_gy[i].allocator()->init(tensor_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 105 | // Manage intermediate buffers |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 106 | _memory_group.manage(&_scharr_gx[i]); |
| 107 | _memory_group.manage(&_scharr_gy[i]); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 108 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | // Init Scharr kernel |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 110 | _func_scharr[i].configure(old_ith_input, &_scharr_gx[i], &_scharr_gy[i], border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
| 112 | // Init Lucas-Kanade kernel |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 113 | _kernel_tracker[i].configure(old_ith_input, new_ith_input, &_scharr_gx[i], &_scharr_gy[i], |
| 114 | old_points, new_points_estimates, new_points, |
| 115 | &_old_points_internal, &_new_points_internal, |
| 116 | termination, use_initial_estimate, epsilon, num_iterations, window_dimension, |
| 117 | i, _num_levels, pyr_scale); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 119 | _scharr_gx[i].allocator()->allocate(); |
| 120 | _scharr_gy[i].allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | void NEOpticalFlow::run() |
| 125 | { |
| 126 | ARM_COMPUTE_ERROR_ON_MSG(_num_levels == 0, "Unconfigured function"); |
| 127 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 128 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 129 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | for(unsigned int level = _num_levels; level > 0; --level) |
| 131 | { |
| 132 | // Run Scharr kernel |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 133 | _func_scharr[level - 1].run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | |
| 135 | // Run Lucas-Kanade kernel |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 136 | NEScheduler::get().schedule(&_kernel_tracker[level - 1], Window::DimX); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | } |