Compute Library
 21.02
NonLinearFilter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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 "NonLinearFilter.h"
25 #include "Utils.h"
26 
27 namespace arm_compute
28 {
29 namespace test
30 {
31 namespace validation
32 {
33 namespace reference
34 {
35 template <typename T>
36 SimpleTensor<T> non_linear_filter(const SimpleTensor<T> &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode,
37  uint8_t constant_border_value)
38 {
39  SimpleTensor<T> dst(src.shape(), src.data_type());
40 
41  ARM_COMPUTE_ERROR_ON(pattern == MatrixPattern::OTHER && mask == nullptr);
42  ARM_COMPUTE_UNUSED(pattern);
43 
44  using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type;
45 
46  const int sq_mask_size = mask_size * mask_size;
47  const int half_mask_size = mask_size / 2;
48  std::vector<intermediate_type> vals(sq_mask_size);
49  intermediate_type current_value = 0;
50 
51  const ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(half_mask_size));
52  const uint32_t num_elements = src.num_elements();
53 
54  for(uint32_t element_idx = 0, count = 0, index = 0; element_idx < num_elements; ++element_idx, count = 0, index = 0)
55  {
56  Coordinates id = index2coord(src.shape(), element_idx);
57  if(is_in_valid_region(valid_region, id))
58  {
59  int idx = id.x();
60  int idy = id.y();
61  for(int y = idy - half_mask_size; y <= idy + half_mask_size; ++y)
62  {
63  for(int x = idx - half_mask_size; x <= idx + half_mask_size; ++x, ++index)
64  {
65  id.set(0, x);
66  id.set(1, y);
67  current_value = tensor_elem_at(src, id, border_mode, constant_border_value);
68 
69  if(mask[index] == 255)
70  {
71  vals[count] = static_cast<intermediate_type>(current_value);
72  ++count;
73  }
74  }
75  }
76  std::sort(vals.begin(), vals.begin() + count);
77 
78  ARM_COMPUTE_ERROR_ON(count == 0);
79 
80  switch(function)
81  {
83  dst[element_idx] = saturate_cast<T>(vals[0]);
84  break;
86  dst[element_idx] = saturate_cast<T>(vals[count - 1]);
87  break;
89  dst[element_idx] = saturate_cast<T>(vals[count / 2]);
90  break;
91  default:
92  ARM_COMPUTE_ERROR("Unsupported NonLinearFilter function.");
93  }
94  }
95  }
96 
97  return dst;
98 }
99 
100 template SimpleTensor<uint8_t> non_linear_filter(const SimpleTensor<uint8_t> &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask,
101  BorderMode border_mode, uint8_t constant_border_value);
102 } // namespace reference
103 } // namespace validation
104 } // namespace test
105 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:265
T tensor_elem_at(const SimpleTensor< T > &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
Definition: Utils.h:64
Container for 2D border size.
Definition: Types.h:273
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
const ValidRegion valid_region
Definition: Scale.cpp:221
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Check if a coordinate is within a valid region.
Definition: Utils.h:507
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Coordinates of an item.
Definition: Coordinates.h:37
SimpleTensor< T > non_linear_filter(const SimpleTensor< T > &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value)
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Utils.h:460
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
Borders are left undefined.
typename traits::make_signed_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:437
T saturate_cast(T val)
Saturate a value of type T against the numeric limits of type U.
Definition: Utils.h:414
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
Container for valid region of a window.
Definition: Types.h:188
MatrixPattern
Available matrix patterns.
Definition: Types.h:504
Any other matrix pattern.
NonLinearFilterFunction
Available non linear functions.
Definition: Types.h:513
ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined=false, BorderSize border_size=BorderSize(0))
Create a valid region based on tensor shape, border mode and border size.
Definition: Utils.h:225