Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | // Configure non-maxima suppression |
| 124 | _non_max_suppr.configure(&_magnitude, &_phase, &_nonmax, upper_thr, lower_thr, border_mode == BorderMode::UNDEFINED); |
| 125 | |
| 126 | // Fill border around magnitude image as non-maxima suppression will access |
| 127 | // it. If border mode is undefined filling the border is a nop. |
| 128 | _border_mag_gradient.configure(&_magnitude, _non_max_suppr.border_size(), border_mode, constant_border_value); |
| 129 | |
| 130 | // Configure edge tracing |
| 131 | _edge_trace.configure(&_nonmax, output); |
| 132 | |
| 133 | // Fill border with "No edge" to stop recursion in edge trace |
| 134 | _border_edge_trace.configure(&_nonmax, _edge_trace.border_size(), BorderMode::CONSTANT, 0); |
| 135 | |
| 136 | // Allocate intermediate tensors |
| 137 | _gx.allocator()->allocate(); |
| 138 | _gy.allocator()->allocate(); |
| 139 | _phase.allocator()->allocate(); |
| 140 | _magnitude.allocator()->allocate(); |
| 141 | _nonmax.allocator()->allocate(); |
| 142 | } |
| 143 | |
| 144 | void NECannyEdge::run() |
| 145 | { |
| 146 | ARM_COMPUTE_ERROR_ON_MSG(_sobel == nullptr, "Unconfigured function"); |
| 147 | ARM_COMPUTE_ERROR_ON(_output == nullptr); |
| 148 | |
| 149 | // Run sobelNxN |
| 150 | _sobel->run(); |
| 151 | |
| 152 | // Fill border before non-maxima suppression. Nop for border mode undefined. |
| 153 | _border_mag_gradient.run(_border_mag_gradient.window()); |
| 154 | |
| 155 | // Run gradient |
| 156 | NEScheduler::get().schedule(_gradient.get(), Window::DimY); |
| 157 | |
| 158 | // Run non-maxima suppression |
| 159 | NEScheduler::get().schedule(&_non_max_suppr, Window::DimY); |
| 160 | |
| 161 | ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr); |
| 162 | memset(_output->buffer(), 0, _output->info()->total_size()); |
| 163 | |
| 164 | // Fill border before edge trace |
| 165 | _border_edge_trace.run(_border_edge_trace.window()); |
| 166 | |
| 167 | // Run edge tracing |
| 168 | _edge_trace.run(_edge_trace.window()); |
| 169 | } |