Compute Library
 21.02
NEOpticalFlow.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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  */
25 
26 #include "arm_compute/core/Error.h"
37 
38 namespace arm_compute
39 {
41 
42 NEOpticalFlow::NEOpticalFlow(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
43  : _memory_group(std::move(memory_manager)),
44  _func_scharr(),
45  _kernel_tracker(),
46  _scharr_gx(),
47  _scharr_gy(),
48  _new_points(nullptr),
49  _new_points_estimates(nullptr),
50  _old_points(nullptr),
51  _new_points_internal(),
52  _old_points_internal(),
53  _num_levels(0)
54 {
55 }
56 
57 void NEOpticalFlow::configure(const Pyramid *old_pyramid, const Pyramid *new_pyramid, const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates,
58  IKeyPointArray *new_points, Termination termination, float epsilon, unsigned int num_iterations, size_t window_dimension,
59  bool use_initial_estimate, BorderMode border_mode, uint8_t constant_border_value)
60 {
61  ARM_COMPUTE_ERROR_ON(nullptr == old_pyramid);
62  ARM_COMPUTE_ERROR_ON(nullptr == new_pyramid);
63  ARM_COMPUTE_ERROR_ON(nullptr == old_points);
64  ARM_COMPUTE_ERROR_ON(nullptr == new_points_estimates);
65  ARM_COMPUTE_ERROR_ON(nullptr == new_points);
66  ARM_COMPUTE_ERROR_ON(old_pyramid->info()->num_levels() != new_pyramid->info()->num_levels());
67  ARM_COMPUTE_ERROR_ON(0 == old_pyramid->info()->num_levels());
68  ARM_COMPUTE_ERROR_ON(old_pyramid->info()->width() != new_pyramid->info()->width());
69  ARM_COMPUTE_ERROR_ON(old_pyramid->info()->height() != new_pyramid->info()->height());
70  ARM_COMPUTE_ERROR_ON(use_initial_estimate && old_points->num_values() != new_points_estimates->num_values());
71 
72  _num_levels = old_pyramid->info()->num_levels();
73  _old_points = old_points;
74  _new_points = new_points;
75  _new_points_estimates = new_points_estimates;
76 
77  const float pyr_scale = old_pyramid->info()->scale();
78 
79  _func_scharr.clear();
80  _kernel_tracker.clear();
81  _scharr_gx.clear();
82  _scharr_gy.clear();
83 
84  _func_scharr.resize(_num_levels);
85  _kernel_tracker.resize(_num_levels);
86  _scharr_gx.resize(_num_levels);
87  _scharr_gy.resize(_num_levels);
88 
89  _old_points_internal = LKInternalKeypointArray(old_points->num_values());
90  _new_points_internal = LKInternalKeypointArray(old_points->num_values());
91  _new_points->resize(old_points->num_values());
92 
93  for(unsigned int i = 0; i < _num_levels; ++i)
94  {
95  // Get images from the ith level of old and right pyramid
96  IImage *old_ith_input = old_pyramid->get_pyramid_level(i);
97  IImage *new_ith_input = new_pyramid->get_pyramid_level(i);
98 
99  // Get width and height of images
100  const unsigned int width_ith = old_ith_input->info()->dimension(0);
101  const unsigned int height_ith = new_ith_input->info()->dimension(1);
102 
103  TensorInfo tensor_info(TensorShape(width_ith, height_ith), Format::S16);
104 
105  _scharr_gx[i].allocator()->init(tensor_info);
106  _scharr_gy[i].allocator()->init(tensor_info);
107 
108  // Manage intermediate buffers
109  _memory_group.manage(&_scharr_gx[i]);
110  _memory_group.manage(&_scharr_gy[i]);
111 
112  // Init Scharr kernel
113  _func_scharr[i].configure(old_ith_input, &_scharr_gx[i], &_scharr_gy[i], border_mode, constant_border_value);
114 
115  // Init Lucas-Kanade kernel
116  _kernel_tracker[i] = std::make_unique<NELKTrackerKernel>();
117  _kernel_tracker[i]->configure(old_ith_input, new_ith_input, &_scharr_gx[i], &_scharr_gy[i],
118  old_points, new_points_estimates, new_points,
119  &_old_points_internal, &_new_points_internal,
120  termination, use_initial_estimate, epsilon, num_iterations, window_dimension,
121  i, _num_levels, pyr_scale);
122 
123  _scharr_gx[i].allocator()->allocate();
124  _scharr_gy[i].allocator()->allocate();
125  }
126 }
127 
129 {
130  ARM_COMPUTE_ERROR_ON_MSG(_num_levels == 0, "Unconfigured function");
131 
132  MemoryGroupResourceScope scope_mg(_memory_group);
133 
134  for(unsigned int level = _num_levels; level > 0; --level)
135  {
136  // Run Scharr kernel
137  _func_scharr[level - 1].run();
138 
139  // Run Lucas-Kanade kernel
140  NEScheduler::get().schedule(_kernel_tracker[level - 1].get(), Window::DimX);
141  }
142 }
143 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:265
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Tensor * get_pyramid_level(size_t index) const override
Retrieves a level of the pyramid as a ITensor pointer.
Definition: Pyramid.cpp:112
Array of type T.
Definition: IArray.h:40
void resize(size_t num)
Resizes the array to contain "num" elements.
Definition: IArray.h:128
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
Basic implementation of the pyramid interface.
Definition: Pyramid.h:40
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
size_t num_values() const
Number of values currently stored in the array.
Definition: IArray.h:68
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
float scale() const
Return the scale factor of the pyramid.
Termination
Termination criteria.
Definition: Types.h:414
const PyramidInfo * info() const override
Interface to be implemented by the child class to return the Pyramid&#39;s metadata.
Definition: Pyramid.cpp:107
1 channel, 1 S16 per channel
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
NEOpticalFlow(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
void configure(const Pyramid *old_pyramid, const Pyramid *new_pyramid, const IKeyPointArray *old_points, const IKeyPointArray *new_points_estimates, IKeyPointArray *new_points, Termination termination, float epsilon, unsigned int num_iterations, size_t window_dimension, bool use_initial_estimate, BorderMode border_mode, uint8_t constant_border_value=0)
Initialise the function input and output.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
size_t width() const
Return the width of the 0th level tensor.
Definition: PyramidInfo.cpp:82
size_t num_levels() const
Return the number of the pyramid levels.
Definition: PyramidInfo.cpp:77
size_t height() const
Return the height of the 0th level tensor.
Definition: PyramidInfo.cpp:87
Array< NELKInternalKeypoint > LKInternalKeypointArray
Array of LK Internel Keypoints.
Definition: NEOpticalFlow.h:47
~NEOpticalFlow()
Default destructor.
void run() override
Run the kernels contained in the function.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94