blob: 318cea2342d5b945e9bb69445b72b6ebe7a27529 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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"
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
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010044NECannyEdge::NECannyEdge() // NOLINT
45 : _sobel(),
46 _gradient(),
47 _non_max_suppr(),
48 _edge_trace(),
49 _border_mag_gradient(),
50 _border_edge_trace(),
51 _gx(),
52 _gy(),
53 _magnitude(),
54 _phase(),
55 _nonmax(),
56 _output(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
58}
59
60void 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,
61 bool use_fp16)
62{
63 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
64 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
65 ARM_COMPUTE_ERROR_ON(gradient_size < 3);
66 ARM_COMPUTE_ERROR_ON(gradient_size > 7);
67 ARM_COMPUTE_ERROR_ON(lower_thr > upper_thr);
68 ARM_COMPUTE_ERROR_ON((1 != norm_type) && (2 != norm_type));
69
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
96 // Configure/Init sobelNxN
97 if(gradient_size == 3)
98 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010099 auto k = arm_compute::support::cpp14::make_unique<NESobel3x3>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
101 _sobel = std::move(k);
102 }
103 else if(gradient_size == 5)
104 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100105 auto k = arm_compute::support::cpp14::make_unique<NESobel5x5>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
107 _sobel = std::move(k);
108 }
109 else if(gradient_size == 7)
110 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100111 auto k = arm_compute::support::cpp14::make_unique<NESobel7x7>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 k->configure(input, &_gx, &_gy, border_mode, constant_border_value);
113 _sobel = std::move(k);
114 }
115 else
116 {
117 ARM_COMPUTE_ERROR("Gradient size not supported\n");
118 }
119
120 // Configure gradient
121 if(use_fp16)
122 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100123 auto k = arm_compute::support::cpp14::make_unique<NEGradientFP16Kernel>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
125 _gradient = std::move(k);
126 }
127 else
128 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100129 auto k = arm_compute::support::cpp14::make_unique<NEGradientKernel>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 k->configure(&_gx, &_gy, &_magnitude, &_phase, norm_type);
131 _gradient = std::move(k);
132 }
133
134 // Configure non-maxima suppression
135 _non_max_suppr.configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED);
136
137 // Fill border around magnitude image as non-maxima suppression will access
138 // it. If border mode is undefined filling the border is a nop.
139 _border_mag_gradient.configure(&_magnitude, _non_max_suppr.border_size(), border_mode, constant_border_value);
140
141 // Configure edge tracing
142 _edge_trace.configure(&_nonmax, output);
143
144 // Fill border with "No edge" to stop recursion in edge trace
145 _border_edge_trace.configure(&_nonmax, _edge_trace.border_size(), BorderMode::CONSTANT, 0);
146
147 // Allocate intermediate tensors
148 _gx.allocator()->allocate();
149 _gy.allocator()->allocate();
150 _phase.allocator()->allocate();
151 _magnitude.allocator()->allocate();
152 _nonmax.allocator()->allocate();
153}
154
155void NECannyEdge::run()
156{
157 ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function");
158 ARM_COMPUTE_ERROR_ON(_output == nullptr);
159
160 // Run sobelNxN
161 _sobel->run();
162
163 // Fill border before non-maxima suppression. Nop for border mode undefined.
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100164 NEScheduler::get().schedule(&_border_mag_gradient, Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166 // Run gradient
167 NEScheduler::get().schedule(_gradient.get(), Window::DimY);
168
169 // Run non-maxima suppression
170 NEScheduler::get().schedule(&_non_max_suppr, Window::DimY);
171
172 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
173 memset(_output->buffer(), 0, _output->info()->total_size());
174
175 // Fill border before edge trace
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100176 NEScheduler::get().schedule(&_border_edge_trace, Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
178 // Run edge tracing
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100179 NEScheduler::get().schedule(&_edge_trace, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180}