blob: 401b9e47af7b59104d9865b395dd451f1b6746c0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbiere8a49832018-01-18 10:04:05 +00002 * Copyright (c) 2016-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#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
31namespace arm_compute
32{
33class ITensor;
34
35/** Computes magnitude and quantised phase from inputs gradients. */
36class NEGradientKernel : public INEKernel
37{
38public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000039 const char *name() const override
40 {
41 return "NEGradientKernel";
42 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 /** 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 Pflanzerc186b572017-09-07 09:48:04 +010069 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
71protected:
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
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000088#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089/** NEON kernel to perform Gradient computation
90 */
91class NEGradientFP16Kernel : public NEGradientKernel
92{
93public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000094 const char *name() const override
95 {
96 return "NEGradientFP16Kernel";
97 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 // Inherited methods overriden:
99 void configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type) override;
100};
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000101#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102using NEGradientFP16Kernel = NEGradientKernel;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000103#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
105/** NEON kernel to perform Non-Maxima suppression for Canny Edge.
106 *
107 * @note This kernel is meant to be used alongside CannyEdge and performs a non-maxima suppression using magnitude and phase of input
108 * to characterize points as possible edges. Thus, at the end, each point will be set to EDGE, NO_EDGE or MAYBE.
109 *
110 * @note Hysteresis is computed in @ref NEEdgeTraceKernel
111 */
112class NEEdgeNonMaxSuppressionKernel : public INEKernel
113{
114public:
Anthony Barbiere8a49832018-01-18 10:04:05 +0000115 const char *name() const override
116 {
117 return "NEEdgeNonMaxSuppressionKernel";
118 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 /** Default constructor */
120 NEEdgeNonMaxSuppressionKernel();
121 /** Prevent instances of this class from being copied (As this class contains pointers) */
122 NEEdgeNonMaxSuppressionKernel(const NEEdgeNonMaxSuppressionKernel &) = delete;
123 /** Prevent instances of this class from being copied (As this class contains pointers) */
124 NEEdgeNonMaxSuppressionKernel &operator=(const NEEdgeNonMaxSuppressionKernel &) = delete;
125 /** Allow instances of this class to be moved */
126 NEEdgeNonMaxSuppressionKernel(NEEdgeNonMaxSuppressionKernel &&) = default;
127 /** Allow instances of this class to be moved */
128 NEEdgeNonMaxSuppressionKernel &operator=(NEEdgeNonMaxSuppressionKernel &&) = default;
129 /** Default destructor */
130 ~NEEdgeNonMaxSuppressionKernel() = default;
131
132 /** Initialise the kernel's sources, destination and border mode.
133 *
134 * @param[in] magnitude Source tensor - Magnitude. Data type supported: U16/U32.
135 * @param[in] phase Source tensor - Quantized phase. Data type supported: U8.
136 * @param[out] output Output tensor. Data type supported: U8. It will be filled with 0 for "no edge", 127 for "maybe", 255 for "edge"
137 * @param[in] upper_thr Upper threshold used for the hysteresis
138 * @param[in] lower_thr Lower threshold used for the hysteresis
139 * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant.
140 */
141 void configure(const ITensor *magnitude, const ITensor *phase, ITensor *output, int32_t upper_thr, int32_t lower_thr, bool border_undefined);
142
143 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100144 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 BorderSize border_size() const override;
146
147private:
148 /** Common signature for all the specialised non-maxima suppression functions
149 *
150 * @param[in] magnitude_ptr Pointer to the first input tensor.
151 * @param[in] phase_ptr Pointer to the second input tensor.
152 * @param[out] output_ptr Pointer to the output tensor
153 * @param[in] stride_mag Stride of the magnitude tensor
154 * @param[in] upper_thr Upper threshold used for the hysteresis
155 * @param[in] lower_thr Lower threshold used for the hysteresis
156 */
157 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,
158 const int32_t lower_thr);
159
160 EdgeNonMaxSupprFunction *_func; /**< Non-Maxima suppression function to use for the particular tensor types passed to configure() */
161 const ITensor *_magnitude; /**< Source tensor - Magnitude */
162 const ITensor *_phase; /**< Source tensor - Quantized phase */
163 ITensor *_output; /**< Destination tensor */
164 int32_t _lower_thr; /**< Lower threshold used for the hysteresis */
165 int32_t _upper_thr; /**< Upper threshold used for the hysteresis */
166};
167
168/** NEON kernel to perform Edge tracing */
169class NEEdgeTraceKernel : public INEKernel
170{
171public:
Anthony Barbiere8a49832018-01-18 10:04:05 +0000172 const char *name() const override
173 {
174 return "NEEdgeTraceKernel";
175 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 /** Default constructor */
177 NEEdgeTraceKernel();
178 /** Prevent instances of this class from being copied (As this class contains pointers) */
179 NEEdgeTraceKernel(const NEEdgeTraceKernel &) = delete;
180 /** Prevent instances of this class from being copied (As this class contains pointers) */
181 NEEdgeTraceKernel &operator=(const NEEdgeTraceKernel &) = delete;
182 /** Allow instances of this class to be moved */
183 NEEdgeTraceKernel(NEEdgeTraceKernel &&) = default;
184 /** Allow instances of this class to be moved */
185 NEEdgeTraceKernel &operator=(NEEdgeTraceKernel &&) = default;
186 /** Default constructor */
187 ~NEEdgeTraceKernel() = default;
188
189 /** Initialise the kernel's source, destination and border mode.
190 *
191 * @param[in,out] input Source tensor. Data type supported: U8. Must contain 0 for "no edge", 127 for "maybe", 255 for "edge"
192 * @param[in,out] output Destination tensor. Data type supported: U8. Must be initialized to 0 (No edge).
193 */
194 void configure(ITensor *input, ITensor *output);
195
196 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100197 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 BorderSize border_size() const override;
199 bool is_parallelisable() const override;
200
201private:
202 ITensor *_input; /**< Source tensor */
203 ITensor *_output; /**< Destination tensor */
204};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100205} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206#endif /* __ARM_COMPUTE_NECANNYEDGEKERNEL_H */