arm_compute v18.05
[platform/upstream/armcl.git] / arm_compute / core / NEON / kernels / NECannyEdgeKernel.h
1 /*
2  * Copyright (c) 2016-2018 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 #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:
39     const char *name() const override
40     {
41         return "NEGradientKernel";
42     }
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:
69     void run(const Window &window, const ThreadInfo &info) override;
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
88 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
89 /** NEON kernel to perform Gradient computation for FP16 datatype
90  */
91 class NEGradientFP16Kernel : public NEGradientKernel
92 {
93 public:
94     const char *name() const override
95     {
96         return "NEGradientFP16Kernel";
97     }
98     // Inherited methods overriden:
99     void configure(const ITensor *gx, const ITensor *gy, ITensor *magnitude, ITensor *phase, int32_t norm_type) override;
100 };
101 #else  /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
102 /** NEON kernel to perform Gradient computation for FP16 datatype */
103 using NEGradientFP16Kernel = NEGradientKernel;
104 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
105
106 /** NEON kernel to perform Non-Maxima suppression for Canny Edge.
107  *
108  * @note This kernel is meant to be used alongside CannyEdge and performs a non-maxima suppression using magnitude and phase of input
109  *       to characterize points as possible edges. Thus, at the end, each point will be set to EDGE, NO_EDGE or MAYBE.
110  *
111  * @note Hysteresis is computed in @ref NEEdgeTraceKernel
112  */
113 class NEEdgeNonMaxSuppressionKernel : public INEKernel
114 {
115 public:
116     const char *name() const override
117     {
118         return "NEEdgeNonMaxSuppressionKernel";
119     }
120     /** Default constructor */
121     NEEdgeNonMaxSuppressionKernel();
122     /** Prevent instances of this class from being copied (As this class contains pointers) */
123     NEEdgeNonMaxSuppressionKernel(const NEEdgeNonMaxSuppressionKernel &) = delete;
124     /** Prevent instances of this class from being copied (As this class contains pointers) */
125     NEEdgeNonMaxSuppressionKernel &operator=(const NEEdgeNonMaxSuppressionKernel &) = delete;
126     /** Allow instances of this class to be moved */
127     NEEdgeNonMaxSuppressionKernel(NEEdgeNonMaxSuppressionKernel &&) = default;
128     /** Allow instances of this class to be moved */
129     NEEdgeNonMaxSuppressionKernel &operator=(NEEdgeNonMaxSuppressionKernel &&) = default;
130     /** Default destructor */
131     ~NEEdgeNonMaxSuppressionKernel() = default;
132
133     /** Initialise the kernel's sources, destination and border mode.
134      *
135      * @param[in]  magnitude        Source tensor - Magnitude. Data type supported: U16/U32.
136      * @param[in]  phase            Source tensor - Quantized phase. Data type supported: U8.
137      * @param[out] output           Output tensor. Data type supported: U8. It will be filled with 0 for "no edge", 127 for "maybe", 255 for "edge"
138      * @param[in]  upper_thr        Upper threshold used for the hysteresis
139      * @param[in]  lower_thr        Lower threshold used for the hysteresis
140      * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
141      */
142     void configure(const ITensor *magnitude, const ITensor *phase, ITensor *output, int32_t upper_thr, int32_t lower_thr, bool border_undefined);
143
144     // Inherited methods overridden:
145     void run(const Window &window, const ThreadInfo &info) override;
146     BorderSize border_size() const override;
147
148 private:
149     /** Common signature for all the specialised non-maxima suppression functions
150      *
151      * @param[in]  magnitude_ptr Pointer to the first input tensor.
152      * @param[in]  phase_ptr     Pointer to the second input tensor.
153      * @param[out] output_ptr    Pointer to the output tensor
154      * @param[in]  stride_mag    Stride of the magnitude tensor
155      * @param[in]  upper_thr     Upper threshold used for the hysteresis
156      * @param[in]  lower_thr     Lower threshold used for the hysteresis
157      */
158     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,
159                                          const int32_t lower_thr);
160
161     EdgeNonMaxSupprFunction *_func;      /**< Non-Maxima suppression function to use for the particular tensor types passed to configure() */
162     const ITensor           *_magnitude; /**< Source tensor - Magnitude */
163     const ITensor           *_phase;     /**< Source tensor - Quantized phase */
164     ITensor                 *_output;    /**< Destination tensor */
165     int32_t                  _lower_thr; /**< Lower threshold used for the hysteresis */
166     int32_t                  _upper_thr; /**< Upper threshold used for the hysteresis */
167 };
168
169 /** NEON kernel to perform Edge tracing */
170 class NEEdgeTraceKernel : public INEKernel
171 {
172 public:
173     const char *name() const override
174     {
175         return "NEEdgeTraceKernel";
176     }
177     /** Default constructor */
178     NEEdgeTraceKernel();
179     /** Prevent instances of this class from being copied (As this class contains pointers) */
180     NEEdgeTraceKernel(const NEEdgeTraceKernel &) = delete;
181     /** Prevent instances of this class from being copied (As this class contains pointers) */
182     NEEdgeTraceKernel &operator=(const NEEdgeTraceKernel &) = delete;
183     /** Allow instances of this class to be moved */
184     NEEdgeTraceKernel(NEEdgeTraceKernel &&) = default;
185     /** Allow instances of this class to be moved */
186     NEEdgeTraceKernel &operator=(NEEdgeTraceKernel &&) = default;
187     /** Default constructor */
188     ~NEEdgeTraceKernel() = default;
189
190     /** Initialise the kernel's source, destination and border mode.
191      *
192      * @param[in,out] input  Source tensor. Data type supported: U8. Must contain 0 for "no edge", 127 for "maybe", 255 for "edge"
193      * @param[in,out] output Destination tensor. Data type supported: U8. Must be initialized to 0 (No edge).
194      */
195     void configure(ITensor *input, ITensor *output);
196
197     // Inherited methods overridden:
198     void run(const Window &window, const ThreadInfo &info) override;
199     BorderSize border_size() const override;
200     bool       is_parallelisable() const override;
201
202 private:
203     ITensor *_input;  /**< Source tensor */
204     ITensor *_output; /**< Destination tensor */
205 };
206 } // namespace arm_compute
207 #endif /* __ARM_COMPUTE_NECANNYEDGEKERNEL_H */