blob: d9c2532add16de004056b585b18d5899c49c8ae7 [file] [log] [blame]
Moritz Pflanzer7655a672017-09-23 11:57:33 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Moritz Pflanzer7655a672017-09-23 11:57:33 +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#include "Sobel.h"
25
26#include "Utils.h"
27#include "tests/validation/Helpers.h"
28
29#include <array>
30#include <map>
31#include <utility>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39namespace reference
40{
41namespace
42{
43const std::array<int8_t, 9> sobel_3_x{ { -1, 0, 1, -2, 0, 2, -1, 0, 1 } };
44const std::array<int8_t, 9> sobel_3_y{ { -1, -2, -1, 0, 0, 0, 1, 2, 1 } };
45
46const std::array<int8_t, 25> sobel_5_x{ {
47 -1, -2, 0, 2, 1,
48 -4, -8, 0, 8, 4,
49 -6, -12, 0, 12, 6,
50 -4, -8, 0, 8, 4,
51 -1, -2, 0, 2, 1
52 } };
53
54const std::array<int8_t, 25> sobel_5_y{ {
55 -1, -4, -6, -4, -1,
56 -2, -8, -12, -8, -2,
57 0, 0, 0, 0, 0,
58 2, 8, 12, 8, 2,
59 1, 4, 6, 4, 1
60 } };
61
62const std::array<int8_t, 49> sobel_7_x{ {
63 -1, -4, -5, 0, 5, 4, 1,
64 -6, -24, -30, 0, 30, 24, 6,
65 -15, -60, -75, 0, 75, 60, 15,
66 -20, -80, -100, 0, 100, 80, 20,
67 -15, -60, -75, 0, 75, 60, 15,
68 -6, -24, -30, 0, 30, 24, 6,
69 -1, -4, -5, 0, 5, 4, 1
70 } };
71
72const std::array<int8_t, 49> sobel_7_y{ {
73 -1, -6, -15, -20, -15, -6, -1,
74 -4, -24, -60, -80, -60, -24, -4,
75 -5, -30, -75, -100, -75, -30, -5,
76 0, 0, 0, 0, 0, 0, 0,
77 5, 30, 75, 100, 75, 30, 5,
78 4, 24, 60, 80, 60, 24, 4,
79 1, 6, 15, 20, 15, 6, 1
80 } };
81
82const std::map<int, std::pair<const int8_t *, const int8_t *>> masks
83{
84 { 3, { sobel_3_x.data(), sobel_3_y.data() } },
85 { 5, { sobel_5_x.data(), sobel_5_y.data() } },
86 { 7, { sobel_7_x.data(), sobel_7_y.data() } },
87};
88
89template <typename T>
90struct data_type;
91
92template <>
93struct data_type<int16_t>
94{
95 const static DataType value = DataType::S16;
96};
97
98template <>
99struct data_type<int>
100{
101 const static DataType value = DataType::S32;
102};
103} // namespace
104
105template <typename T, typename U>
Isabella Gottardi43ce8982017-11-08 11:13:23 +0000106std::pair<SimpleTensor<T>, SimpleTensor<T>> sobel(const SimpleTensor<U> &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value, GradientDimension gradient_dimension)
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100107{
108 SimpleTensor<T> dst_x(src.shape(), data_type<T>::value, src.num_channels());
109 SimpleTensor<T> dst_y(src.shape(), data_type<T>::value, src.num_channels());
110
111 ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(filter_size / 2));
112
Michalis Spyroufae513c2019-10-16 17:41:33 +0100113 const uint32_t num_elements = src.num_elements();
114 for(uint32_t i = 0; i < num_elements; ++i)
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100115 {
116 Coordinates coord = index2coord(src.shape(), i);
117
118 if(!is_in_valid_region(valid_region, coord))
119 {
120 continue;
121 }
Isabella Gottardi43ce8982017-11-08 11:13:23 +0000122 switch(gradient_dimension)
123 {
124 case GradientDimension::GRAD_X:
125 apply_2d_spatial_filter(coord, src, dst_x, TensorShape{ static_cast<unsigned int>(filter_size), static_cast<unsigned int>(filter_size) }, masks.at(filter_size).first, 1.f, border_mode,
126 constant_border_value);
127 break;
128 case GradientDimension::GRAD_Y:
129 apply_2d_spatial_filter(coord, src, dst_y, TensorShape{ static_cast<unsigned int>(filter_size), static_cast<unsigned int>(filter_size) }, masks.at(filter_size).second, 1.f, border_mode,
130 constant_border_value);
131 break;
132 case GradientDimension::GRAD_XY:
133 apply_2d_spatial_filter(coord, src, dst_x, TensorShape{ static_cast<unsigned int>(filter_size), static_cast<unsigned int>(filter_size) }, masks.at(filter_size).first, 1.f, border_mode,
134 constant_border_value);
135 apply_2d_spatial_filter(coord, src, dst_y, TensorShape{ static_cast<unsigned int>(filter_size), static_cast<unsigned int>(filter_size) }, masks.at(filter_size).second, 1.f, border_mode,
136 constant_border_value);
137 break;
138 default:
139 ARM_COMPUTE_ERROR("Gradient dimension not supported");
140 }
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100141 }
142
143 return std::make_pair(dst_x, dst_y);
144}
145
Isabella Gottardi43ce8982017-11-08 11:13:23 +0000146template std::pair<SimpleTensor<int16_t>, SimpleTensor<int16_t>> sobel(const SimpleTensor<uint8_t> &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value,
147 GradientDimension gradient_dimension);
148template std::pair<SimpleTensor<int>, SimpleTensor<int>> sobel(const SimpleTensor<uint8_t> &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value,
149 GradientDimension gradient_dimension);
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100150} // namespace reference
151} // namespace validation
152} // namespace test
153} // namespace arm_compute