e67e4d6f883e91b3276e0b73c5174c1da17aceb3
[platform/upstream/armcl.git] / src / runtime / NEON / functions / NEFastCorners.cpp
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 #include "arm_compute/runtime/NEON/functions/NEFastCorners.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/ITensor.h"
28 #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
29 #include "arm_compute/core/PixelValue.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Validate.h"
32 #include "arm_compute/runtime/Array.h"
33 #include "arm_compute/runtime/NEON/NEScheduler.h"
34 #include "arm_compute/runtime/TensorAllocator.h"
35
36 using namespace arm_compute;
37
38 NEFastCorners::NEFastCorners()
39     : _fast_corners_kernel(),
40       _border_handler(),
41       _nonmax_kernel(),
42       _fill_kernel(),
43       _out_border_handler_kernel(),
44       _output(),
45       _suppressed(),
46       _non_max(false)
47 {
48 }
49
50 void NEFastCorners::configure(IImage *input, float threshold, bool nonmax_suppression, KeyPointArray *const corners,
51                               BorderMode border_mode, uint8_t constant_border_value)
52 {
53     ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
54     ARM_COMPUTE_ERROR_ON(BorderMode::UNDEFINED != border_mode);
55     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
56     ARM_COMPUTE_ERROR_ON(nullptr == corners);
57     ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255);
58
59     TensorInfo tensor_info(input->info()->tensor_shape(), Format::U8);
60     _output.allocator()->init(tensor_info);
61     _border_handler.configure(input, _fast_corners_kernel.border_size(), border_mode, constant_border_value);
62     /*
63         If border is UNDEFINED _fast_corners_kernel will operate in xwindow (3, width - 3) and ywindow (3, height -3) so
64         the output image will leave the pixels on the borders unchanged. This can cause problems if Non Max Suppression is performed afterwards.
65
66         If non max sup is true && border == UNDEFINED we must set the border texels to 0 before executing the non max sup kernel
67     */
68     _fast_corners_kernel.configure(input, &_output, threshold, nonmax_suppression, BorderMode::UNDEFINED == border_mode);
69
70     _output.allocator()->allocate();
71     _non_max = nonmax_suppression;
72
73     if(!_non_max)
74     {
75         _fill_kernel.configure(&_output, 1 /* we keep all texels >0 */, corners);
76     }
77     else
78     {
79         if(border_mode == BorderMode::UNDEFINED)
80         {
81             // We use this kernel to set the borders to 0 before performing non max sup
82             _out_border_handler_kernel.configure(&_output, _fast_corners_kernel.border_size(), PixelValue(static_cast<uint8_t>(0)));
83         }
84
85         _suppressed.allocator()->init(tensor_info);
86         _suppressed.allocator()->allocate();
87         _nonmax_kernel.configure(&_output, &_suppressed, BorderMode::UNDEFINED == border_mode);
88         _fill_kernel.configure(&_suppressed, 1 /* we keep all texels >0 */, corners);
89     }
90 }
91
92 void NEFastCorners::run()
93 {
94     NEScheduler::get().multithread(&_fast_corners_kernel);
95
96     if(_non_max)
97     {
98         NEScheduler::get().multithread(&_out_border_handler_kernel); // make sure inner borders are set to 0 before running non max sup kernel
99         NEScheduler::get().multithread(&_nonmax_kernel);
100     }
101
102     NEScheduler::get().multithread(&_fill_kernel);
103 }