blob: 0e5d50fd21e1b1407b27902a8576faa85959561a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Abe Mbise1b993382017-12-19 13:51:59 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/NEON/kernels/NECannyEdgeKernel.h"
29#include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
33#include "arm_compute/runtime/NEON/functions/NESobel3x3.h"
34#include "arm_compute/runtime/NEON/functions/NESobel5x5.h"
35#include "arm_compute/runtime/NEON/functions/NESobel7x7.h"
36#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010037#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <cstring>
40#include <utility>
41
42using namespace arm_compute;
43
Georgios Pinitas658039b2017-09-15 16:30:50 +010044NECannyEdge::NECannyEdge(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
45 : _memory_group(std::move(memory_manager)),
46 _sobel(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010047 _gradient(),
48 _non_max_suppr(),
49 _edge_trace(),
50 _border_mag_gradient(),
51 _border_edge_trace(),
52 _gx(),
53 _gy(),
54 _magnitude(),
55 _phase(),
56 _nonmax(),
57 _output(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058{
59}
60
Georgios Pinitas09d34512018-08-30 16:02:11 +010061void 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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062{
Abe Mbise1b993382017-12-19 13:51:59 +000063 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
65 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
Abe Mbise1b993382017-12-19 13:51:59 +000067 ARM_COMPUTE_ERROR_ON((gradient_size != 3) && (gradient_size != 5) && (gradient_size != 7));
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +010068 ARM_COMPUTE_ERROR_ON((lower_thr < 0) || (lower_thr >= upper_thr));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
70 _output = output;
71
72 const TensorShape &shape = input->info()->tensor_shape();
73 TensorInfo gradient_info;
74 TensorInfo magnitude_info;
75
76 // Initialize images
77 if(gradient_size < 7)
78 {
79 gradient_info.init(shape, Format::S16);
80 magnitude_info.init(shape, Format::U16);
81 }
82 else
83 {
84 gradient_info.init(shape, Format::S32);
85 magnitude_info.init(shape, Format::U32);
86 }
87
88 _gx.allocator()->init(gradient_info);
89 _gy.allocator()->init(gradient_info);
90 _magnitude.allocator()->init(magnitude_info);
91
92 TensorInfo info(shape, Format::U8);
93 _phase.allocator()->init(info);
94 _nonmax.allocator()->init(info);
95
Georgios Pinitas658039b2017-09-15 16:30:50 +010096 // Manage intermediate buffers
97 _memory_group.manage(&_gx);
98 _memory_group.manage(&_gy);
99
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 // Configure/Init sobelNxN
101 if(gradient_size == 3)
102 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100103 auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
105 _sobel = std::move(k);
106 }
107 else if(gradient_size == 5)
108 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100109 auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
111 _sobel = std::move(k);
112 }
113 else if(gradient_size == 7)
114 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100115 auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
117 _sobel = std::move(k);
118 }
119 else
120 {
Abe Mbise1b993382017-12-19 13:51:59 +0000121 ARM_COMPUTE_ERROR("Gradient size %d not supported\n", gradient_size);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 }
123
Georgios Pinitas658039b2017-09-15 16:30:50 +0100124 // Manage intermediate buffers
125 _memory_group.manage(&_magnitude);
126 _memory_group.manage(&_phase);
127
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 // Configure gradient
Georgios Pinitas09d34512018-08-30 16:02:11 +0100129 auto k = arm_compute::support::cpp14::make_unique<NEGradientKernel>();
130 k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
131 _gradient = std::move(k);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132
Georgios Pinitas658039b2017-09-15 16:30:50 +0100133 // Allocate intermediate tensors
134 _gx.allocator()->allocate();
135 _gy.allocator()->allocate();
136
137 // Manage intermediate buffers
138 _memory_group.manage(&_nonmax);
139
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 // Configure non-maxima suppression
141 _non_max_suppr.configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED);
142
143 // Fill border around magnitude image as non-maxima suppression will access
144 // it. If border mode is undefined filling the border is a nop.
145 _border_mag_gradient.configure(&_magnitude, _non_max_suppr.border_size(), border_mode, constant_border_value);
146
Georgios Pinitas658039b2017-09-15 16:30:50 +0100147 // Allocate intermediate tensors
148 _phase.allocator()->allocate();
149 _magnitude.allocator()->allocate();
150
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 // Configure edge tracing
152 _edge_trace.configure(&_nonmax, output);
153
154 // Fill border with "No edge" to stop recursion in edge trace
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100155 _border_edge_trace.configure(&_nonmax, _edge_trace.border_size(), BorderMode::CONSTANT, static_cast<float>(0.f));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
157 // Allocate intermediate tensors
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 _nonmax.allocator()->allocate();
159}
160
161void NECannyEdge::run()
162{
163 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164
Georgios Pinitas658039b2017-09-15 16:30:50 +0100165 _memory_group.acquire();
166
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 // Run sobelNxN
168 _sobel->run();
169
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 // Run gradient
171 NEScheduler::get().schedule(_gradient.get(), Window::DimY);
172
Abe Mbise1b993382017-12-19 13:51:59 +0000173 // Fill border before non-maxima suppression. Nop for border mode undefined.
174 NEScheduler::get().schedule(&_border_mag_gradient, Window::DimZ);
175
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 // Run non-maxima suppression
177 NEScheduler::get().schedule(&_non_max_suppr, Window::DimY);
178
179 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
Abe Mbise1b993382017-12-19 13:51:59 +0000180 std::fill_n(_output->buffer(), _output->info()->total_size(), 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
182 // Fill border before edge trace
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100183 NEScheduler::get().schedule(&_border_edge_trace, Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
185 // Run edge tracing
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100186 NEScheduler::get().schedule(&_edge_trace, Window::DimY);
Georgios Pinitas658039b2017-09-15 16:30:50 +0100187
188 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189}