Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | #ifndef __ARM_COMPUTE_NECANNYEDGEKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NECANNYEDGEKERNEL_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
| 28 | |
| 29 | #include <cstdint> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | class ITensor; |
| 34 | |
| 35 | /** Computes magnitude and quantised phase from inputs gradients. */ |
| 36 | class NEGradientKernel : public INEKernel |
| 37 | { |
| 38 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 39 | const char *name() const override |
| 40 | { |
| 41 | return "NEGradientKernel"; |
| 42 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | /** Default constructor */ |
| 44 | NEGradientKernel(); |
| 45 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 46 | NEGradientKernel(const NEGradientKernel &) = delete; |
| 47 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 48 | NEGradientKernel &operator=(const NEGradientKernel &) = delete; |
| 49 | /** Allow instances of this class to be moved */ |
| 50 | NEGradientKernel(NEGradientKernel &&) = default; |
| 51 | /** Allow instances of this class to be moved */ |
| 52 | NEGradientKernel &operator=(NEGradientKernel &&) = default; |
| 53 | /** Default destructor */ |
| 54 | virtual ~NEGradientKernel() = default; |
| 55 | |
| 56 | /** Initialise the kernel's sources, destinations and border mode. |
| 57 | * |
| 58 | * @note gx, gy and magnitude must all be the same size (either 16 or 32) |
| 59 | * |
| 60 | * @param[in] gx Source tensor - Gx component. Data type supported: S16/S32. |
| 61 | * @param[in] gy Source tensor - Gy component. Data type supported: same as @p gx. |
| 62 | * @param[out] magnitude Destination tensor - Magnitude. Data type supported: U16 (if the data type of @p gx is S16) / U32 (if the data type of @p gx is S32). |
| 63 | * @param[out] phase Destination tensor - Quantized phase. Data type supported: U8. |
| 64 | * @param[in] norm_type Normalization type. If 1, L1-Norm otherwise L2-Norm |
| 65 | */ |
| 66 | virtual void configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type); |
| 67 | |
| 68 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 69 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | /** Common signature for all the specialised gradient functions |
| 73 | * |
| 74 | * @param[in] gx_ptr Pointer to the first input tensor. |
| 75 | * @param[in] gy_ptr Pointer to the second input tensor. |
| 76 | * @param[out] magnitude_ptr Pointer to the first output tensor |
| 77 | * @param[out] phase_ptr Pointer to the second output tensor |
| 78 | */ |
| 79 | using GradientFunction = void(const void *__restrict gx_ptr, const void *__restrict gy_ptr, void *__restrict magnitude_ptr, void *__restrict phase_ptr); |
| 80 | |
| 81 | GradientFunction *_func; /**< Gradient function to use for the particular tensor types passed to configure() */ |
| 82 | const ITensor *_gx; /**< Source tensor - Gx component */ |
| 83 | const ITensor *_gy; /**< Source tensor - Gy component */ |
| 84 | ITensor *_magnitude; /**< Destination tensor - Magnitude */ |
| 85 | ITensor *_phase; /**< Destination tensor - Quantized phase */ |
| 86 | }; |
| 87 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | /** NEON kernel to perform Non-Maxima suppression for Canny Edge. |
| 89 | * |
| 90 | * @note This kernel is meant to be used alongside CannyEdge and performs a non-maxima suppression using magnitude and phase of input |
| 91 | * to characterize points as possible edges. Thus, at the end, each point will be set to EDGE, NO_EDGE or MAYBE. |
| 92 | * |
| 93 | * @note Hysteresis is computed in @ref NEEdgeTraceKernel |
| 94 | */ |
| 95 | class NEEdgeNonMaxSuppressionKernel : public INEKernel |
| 96 | { |
| 97 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 98 | const char *name() const override |
| 99 | { |
| 100 | return "NEEdgeNonMaxSuppressionKernel"; |
| 101 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | /** Default constructor */ |
| 103 | NEEdgeNonMaxSuppressionKernel(); |
| 104 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 105 | NEEdgeNonMaxSuppressionKernel(const NEEdgeNonMaxSuppressionKernel &) = delete; |
| 106 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 107 | NEEdgeNonMaxSuppressionKernel &operator=(const NEEdgeNonMaxSuppressionKernel &) = delete; |
| 108 | /** Allow instances of this class to be moved */ |
| 109 | NEEdgeNonMaxSuppressionKernel(NEEdgeNonMaxSuppressionKernel &&) = default; |
| 110 | /** Allow instances of this class to be moved */ |
| 111 | NEEdgeNonMaxSuppressionKernel &operator=(NEEdgeNonMaxSuppressionKernel &&) = default; |
| 112 | /** Default destructor */ |
| 113 | ~NEEdgeNonMaxSuppressionKernel() = default; |
| 114 | |
| 115 | /** Initialise the kernel's sources, destination and border mode. |
| 116 | * |
| 117 | * @param[in] magnitude Source tensor - Magnitude. Data type supported: U16/U32. |
| 118 | * @param[in] phase Source tensor - Quantized phase. Data type supported: U8. |
| 119 | * @param[out] output Output tensor. Data type supported: U8. It will be filled with 0 for "no edge", 127 for "maybe", 255 for "edge" |
| 120 | * @param[in] upper_thr Upper threshold used for the hysteresis |
| 121 | * @param[in] lower_thr Lower threshold used for the hysteresis |
| 122 | * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant. |
| 123 | */ |
| 124 | void configure(const ITensor *magnitude, const ITensor *phase, ITensor *output, int32_t upper_thr, int32_t lower_thr, bool border_undefined); |
| 125 | |
| 126 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 127 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | BorderSize border_size() const override; |
| 129 | |
| 130 | private: |
| 131 | /** Common signature for all the specialised non-maxima suppression functions |
| 132 | * |
| 133 | * @param[in] magnitude_ptr Pointer to the first input tensor. |
| 134 | * @param[in] phase_ptr Pointer to the second input tensor. |
| 135 | * @param[out] output_ptr Pointer to the output tensor |
| 136 | * @param[in] stride_mag Stride of the magnitude tensor |
| 137 | * @param[in] upper_thr Upper threshold used for the hysteresis |
| 138 | * @param[in] lower_thr Lower threshold used for the hysteresis |
| 139 | */ |
| 140 | using EdgeNonMaxSupprFunction = void(const void *__restrict magnitude_ptr, const void *__restrict phase_ptr, void *__restrict output_ptr, const uint32_t stride_mag, const int32_t upper_thr, |
| 141 | const int32_t lower_thr); |
| 142 | |
| 143 | EdgeNonMaxSupprFunction *_func; /**< Non-Maxima suppression function to use for the particular tensor types passed to configure() */ |
| 144 | const ITensor *_magnitude; /**< Source tensor - Magnitude */ |
| 145 | const ITensor *_phase; /**< Source tensor - Quantized phase */ |
| 146 | ITensor *_output; /**< Destination tensor */ |
| 147 | int32_t _lower_thr; /**< Lower threshold used for the hysteresis */ |
| 148 | int32_t _upper_thr; /**< Upper threshold used for the hysteresis */ |
| 149 | }; |
| 150 | |
| 151 | /** NEON kernel to perform Edge tracing */ |
| 152 | class NEEdgeTraceKernel : public INEKernel |
| 153 | { |
| 154 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 155 | const char *name() const override |
| 156 | { |
| 157 | return "NEEdgeTraceKernel"; |
| 158 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | /** Default constructor */ |
| 160 | NEEdgeTraceKernel(); |
| 161 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 162 | NEEdgeTraceKernel(const NEEdgeTraceKernel &) = delete; |
| 163 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 164 | NEEdgeTraceKernel &operator=(const NEEdgeTraceKernel &) = delete; |
| 165 | /** Allow instances of this class to be moved */ |
| 166 | NEEdgeTraceKernel(NEEdgeTraceKernel &&) = default; |
| 167 | /** Allow instances of this class to be moved */ |
| 168 | NEEdgeTraceKernel &operator=(NEEdgeTraceKernel &&) = default; |
| 169 | /** Default constructor */ |
| 170 | ~NEEdgeTraceKernel() = default; |
| 171 | |
| 172 | /** Initialise the kernel's source, destination and border mode. |
| 173 | * |
| 174 | * @param[in,out] input Source tensor. Data type supported: U8. Must contain 0 for "no edge", 127 for "maybe", 255 for "edge" |
| 175 | * @param[in,out] output Destination tensor. Data type supported: U8. Must be initialized to 0 (No edge). |
| 176 | */ |
| 177 | void configure(ITensor *input, ITensor *output); |
| 178 | |
| 179 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 180 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | BorderSize border_size() const override; |
| 182 | bool is_parallelisable() const override; |
| 183 | |
| 184 | private: |
| 185 | ITensor *_input; /**< Source tensor */ |
| 186 | ITensor *_output; /**< Destination tensor */ |
| 187 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 188 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | #endif /* __ARM_COMPUTE_NECANNYEDGEKERNEL_H */ |