blob: ff0e11d3a82ba4fdbd7a09f5c50cb40e4bc24cca [file] [log] [blame]
Moritz Pflanzer7655a672017-09-23 11:57:33 +01001/*
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 "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
113 for(int i = 0; i < src.num_elements(); ++i)
114 {
115 Coordinates coord = index2coord(src.shape(), i);
116
117 if(!is_in_valid_region(valid_region, coord))
118 {
119 continue;
120 }
Isabella Gottardi43ce8982017-11-08 11:13:23 +0000121 switch(gradient_dimension)
122 {
123 case GradientDimension::GRAD_X:
124 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,
125 constant_border_value);
126 break;
127 case GradientDimension::GRAD_Y:
128 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,
129 constant_border_value);
130 break;
131 case GradientDimension::GRAD_XY:
132 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,
133 constant_border_value);
134 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,
135 constant_border_value);
136 break;
137 default:
138 ARM_COMPUTE_ERROR("Gradient dimension not supported");
139 }
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100140 }
141
142 return std::make_pair(dst_x, dst_y);
143}
144
Isabella Gottardi43ce8982017-11-08 11:13:23 +0000145template 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,
146 GradientDimension gradient_dimension);
147template std::pair<SimpleTensor<int>, SimpleTensor<int>> sobel(const SimpleTensor<uint8_t> &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value,
148 GradientDimension gradient_dimension);
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100149} // namespace reference
150} // namespace validation
151} // namespace test
152} // namespace arm_compute