632e4701dbbe6a1a5728b7109e24ef87ac05a27b
[platform/upstream/armcl.git] / src / runtime / NEON / functions / NECannyEdge.cpp
1 /*
2  * Copyright (c) 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/NECannyEdge.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/NEON/kernels/NECannyEdgeKernel.h"
30 #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
31 #include "arm_compute/core/TensorInfo.h"
32 #include "arm_compute/core/Validate.h"
33 #include "arm_compute/runtime/NEON/NEScheduler.h"
34 #include "arm_compute/runtime/NEON/functions/NESobel3x3.h"
35 #include "arm_compute/runtime/NEON/functions/NESobel5x5.h"
36 #include "arm_compute/runtime/NEON/functions/NESobel7x7.h"
37 #include "arm_compute/runtime/TensorAllocator.h"
38
39 #include <cstring>
40 #include <utility>
41
42 using namespace arm_compute;
43
44 NECannyEdge::NECannyEdge()
45     : _sobel(), _gradient(), _non_max_suppr(), _edge_trace(), _border_mag_gradient(), _border_edge_trace(), _gx(), _gy(), _magnitude(), _phase(), _nonmax(), _output(nullptr)
46 {
47 }
48
49 void NECannyEdge::configure(ITensor *input, ITensor *output, int32_t upper_thr, int32_t lower_thr, int32_t gradient_size, int32_t norm_type, BorderMode border_mode, uint8_t constant_border_value,
50                             bool use_fp16)
51 {
52     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
53     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
54     ARM_COMPUTE_ERROR_ON(gradient_size < 3);
55     ARM_COMPUTE_ERROR_ON(gradient_size > 7);
56     ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr);
57     ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
58
59     _output = output;
60
61     const TensorShape &shape = input->info()->tensor_shape();
62     TensorInfo         gradient_info;
63     TensorInfo         magnitude_info;
64
65     /* Initialize images */
66     if(gradient_size < 7)
67     {
68         gradient_info.init(shape, Format::S16);
69         magnitude_info.init(shape, Format::U16);
70     }
71     else
72     {
73         gradient_info.init(shape, Format::S32);
74         magnitude_info.init(shape, Format::U32);
75     }
76
77     _gx.allocator()->init(gradient_info);
78     _gy.allocator()->init(gradient_info);
79     _magnitude.allocator()->init(magnitude_info);
80
81     TensorInfo info(shape, Format::U8);
82     _phase.allocator()->init(info);
83     _nonmax.allocator()->init(info);
84
85     /* Configure/Init sobelNxN */
86     if(gradient_size == 3)
87     {
88         auto k = arm_compute::cpp14::make_unique<NESobel3x3>();
89         k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
90         _sobel = std::move(k);
91     }
92     else if(gradient_size == 5)
93     {
94         auto k = arm_compute::cpp14::make_unique<NESobel5x5>();
95         k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
96         _sobel = std::move(k);
97     }
98     else if(gradient_size == 7)
99     {
100         auto k = arm_compute::cpp14::make_unique<NESobel7x7>();
101         k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
102         _sobel = std::move(k);
103     }
104     else
105     {
106         ARM_COMPUTE_ERROR("Gradient size not supported\n");
107     }
108
109     /* Configure gradient */
110     if(use_fp16)
111     {
112         auto k = arm_compute::cpp14::make_unique<NEGradientFP16Kernel>();
113         k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
114         _gradient = std::move(k);
115     }
116     else
117     {
118         auto k = arm_compute::cpp14::make_unique<NEGradientKernel>();
119         k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
120         _gradient = std::move(k);
121     }
122
123     _gx.allocator()->allocate();
124     _gy.allocator()->allocate();
125
126     /* Configure non-maxima suppression */
127     _non_max_suppr.configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED);
128
129     _phase.allocator()->allocate();
130
131     if(border_mode != BorderMode::UNDEFINED)
132     {
133         /* Configure border filling for magnitude image */
134         _border_mag_gradient.configure(&_magnitude, _non_max_suppr.border_size(), BorderMode::CONSTANT, 0);
135     }
136
137     _magnitude.allocator()->allocate();
138
139     /* Configure edge tracing */
140     _edge_trace.configure(&_nonmax, output);
141
142     // Fill border with "No edge" to stop recursion in edge trace
143     _border_edge_trace.configure(&_nonmax, _edge_trace.border_size(), BorderMode::CONSTANT, 0);
144
145     _nonmax.allocator()->allocate();
146 }
147
148 void NECannyEdge::run()
149 {
150     ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
151     ARM_COMPUTE_ERROR_ON(_output == nullptr);
152
153     /* Run sobelNxN */
154     _sobel->run();
155
156     /* Run gradient */
157     NEScheduler::get().multithread(_gradient.get());
158
159     /* Fill border before non-maxima suppression */
160     _border_mag_gradient.run(_border_mag_gradient.window());
161
162     /* Run non-maxima suppression */
163     NEScheduler::get().multithread(&_non_max_suppr);
164
165     ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
166     memset(_output->buffer(), 0, _output->info()->total_size());
167
168     // Fill border before edge trace
169     _border_edge_trace.run(_border_edge_trace.window());
170
171     /* Run edge tracing */
172     _edge_trace.run(_edge_trace.window());
173 }