blob: d851e9a62b3dbe9eaf2eb2bfe9133a0867d68bfa [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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#include "arm_compute/runtime/NEON/functions/NEOpticalFlow.h"
25
26#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#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"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010036#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38using namespace arm_compute;
39
40NEOpticalFlow::NEOpticalFlow()
41 : _func_scharr(), _kernel_tracker(), _scharr_gx(), _scharr_gy(), _new_points(nullptr), _new_points_estimates(nullptr), _old_points(nullptr), _new_points_internal(), _old_points_internal(),
42 _num_levels(0)
43{
44}
45
46void NEOpticalFlow::configure(const Pyramid *old_pyramid, const Pyramid *new_pyramid, const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates,
47 IKeyPointArray *new_points, Termination termination, float epsilon, unsigned int num_iterations, size_t window_dimension,
48 bool use_initial_estimate, BorderMode border_mode, uint8_t constant_border_value)
49{
50 ARM_COMPUTE_ERROR_ON(nullptr == old_pyramid);
51 ARM_COMPUTE_ERROR_ON(nullptr == new_pyramid);
52 ARM_COMPUTE_ERROR_ON(nullptr == old_points);
53 ARM_COMPUTE_ERROR_ON(nullptr == new_points_estimates);
54 ARM_COMPUTE_ERROR_ON(nullptr == new_points);
55 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->num_levels() != new_pyramid->info()->num_levels());
56 ARM_COMPUTE_ERROR_ON(0 == old_pyramid->info()->num_levels());
57 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->width() != new_pyramid->info()->width());
58 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->height() != new_pyramid->info()->height());
59 ARM_COMPUTE_ERROR_ON(use_initial_estimate && old_points->num_values() != new_points_estimates->num_values());
60
61 _num_levels = old_pyramid->info()->num_levels();
62 _old_points = old_points;
63 _new_points = new_points;
64 _new_points_estimates = new_points_estimates;
65
66 const float pyr_scale = old_pyramid->info()->scale();
67
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010068 _func_scharr = arm_compute::support::cpp14::make_unique<NEScharr3x3[]>(_num_levels);
69 _kernel_tracker = arm_compute::support::cpp14::make_unique<NELKTrackerKernel[]>(_num_levels);
70 _scharr_gx = arm_compute::support::cpp14::make_unique<Tensor[]>(_num_levels);
71 _scharr_gy = arm_compute::support::cpp14::make_unique<Tensor[]>(_num_levels);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
73 _old_points_internal = LKInternalKeypointArray(old_points->num_values());
74 _new_points_internal = LKInternalKeypointArray(old_points->num_values());
75 _new_points->resize(old_points->num_values());
76
77 for(unsigned int i = 0; i < _num_levels; ++i)
78 {
79 // Get images from the ith level of old and right pyramid
80 IImage *old_ith_input = old_pyramid->get_pyramid_level(i);
81 IImage *new_ith_input = new_pyramid->get_pyramid_level(i);
82
83 // Get width and height of images
84 const unsigned int width_ith = old_ith_input->info()->dimension(0);
85 const unsigned int height_ith = new_ith_input->info()->dimension(1);
86
87 TensorInfo tensor_info(TensorShape(width_ith, height_ith), Format::S16);
88
89 _scharr_gx[i].allocator()->init(tensor_info);
90 _scharr_gy[i].allocator()->init(tensor_info);
91
92 // Init Scharr kernel
93 _func_scharr[i].configure(old_ith_input, _scharr_gx.get() + i, _scharr_gy.get() + i, border_mode, constant_border_value);
94
95 // Init Lucas-Kanade kernel
96 _kernel_tracker[i].configure(old_ith_input, new_ith_input, _scharr_gx.get() + i, _scharr_gy.get() + i,
97 old_points, new_points_estimates, new_points,
98 &_old_points_internal, &_new_points_internal,
99 termination, use_initial_estimate, epsilon, num_iterations, window_dimension,
100 i, _num_levels, pyr_scale);
101
102 _scharr_gx[i].allocator()->allocate();
103 _scharr_gy[i].allocator()->allocate();
104 }
105}
106
107void NEOpticalFlow::run()
108{
109 ARM_COMPUTE_ERROR_ON_MSG(_num_levels == 0, "Unconfigured function");
110
111 for(unsigned int level = _num_levels; level > 0; --level)
112 {
113 // Run Scharr kernel
114 _func_scharr[level - 1].run();
115
116 // Run Lucas-Kanade kernel
117 NEScheduler::get().schedule(_kernel_tracker.get() + level - 1, Window::DimX);
118 }
119}