NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
percentileFilter.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include "NumCpp/Core/Shape.hpp"
31 #include "NumCpp/Core/Slice.hpp"
32 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
36 
37 namespace nc
38 {
39  namespace filter
40  {
41  //============================================================================
42  // Method Description:
55  template<typename dtype>
56  NdArray<dtype> percentileFilter(const NdArray<dtype>& inImageArray, uint32 inSize, double inPercentile,
57  Boundary inBoundaryType = Boundary::REFLECT, dtype inConstantValue = 0)
58  {
59  NdArray<dtype> arrayWithBoundary = boundary::addBoundary2d(inImageArray, inBoundaryType, inSize, inConstantValue);
60  NdArray<dtype> output(inImageArray.shape());
61 
62  const Shape inShape = inImageArray.shape();
63  const uint32 boundarySize = inSize / 2; // integer division
64  const uint32 endPointRow = boundarySize + inShape.rows;
65  const uint32 endPointCol = boundarySize + inShape.cols;
66 
67  for (uint32 row = boundarySize; row < endPointRow; ++row)
68  {
69  for (uint32 col = boundarySize; col < endPointCol; ++col)
70  {
71  NdArray<dtype> window = arrayWithBoundary(Slice(row - boundarySize, row + boundarySize + 1),
72  Slice(col - boundarySize, col + boundarySize + 1));
73 
74  output(row - boundarySize, col - boundarySize) =
75  percentile(window, inPercentile, Axis::NONE, "nearest").item();
76  }
77  }
78 
79  return output;
80  }
81  } // namespace filter
82 } // namespace nc
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2975
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
nc::Axis::NONE
@ NONE
nc::percentile
NdArray< dtype > percentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: percentile.hpp:66
percentile.hpp
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::filter::percentileFilter
NdArray< dtype > percentileFilter(const NdArray< dtype > &inImageArray, uint32 inSize, double inPercentile, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: percentileFilter.hpp:56
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:45
nc::filter::boundary::addBoundary2d
NdArray< dtype > addBoundary2d(const NdArray< dtype > &inImage, Boundary inBoundaryType, uint32 inKernalSize, dtype inConstantValue=0)
Definition: addBoundary2d.hpp:61
Shape.hpp
nc::filter::Boundary::REFLECT
@ REFLECT
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
nc::filter::Boundary
Boundary
Boundary condition to apply to the image filter.
Definition: Boundary.hpp:37
Types.hpp
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:43
Slice.hpp
addBoundary2d.hpp