NumCpp  1.0
A C++ implementation of the Python Numpy library
rankFilter.hpp
Go to the documentation of this file.
1 #pragma once
30 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Slice.hpp"
34 #include "NumCpp/Core/Types.hpp"
37 #include "NumCpp/NdArray.hpp"
38 
39 #include <string>
40 
41 namespace nc
42 {
43  namespace filter
44  {
45  //============================================================================
46  // Method Description:
59  template<typename dtype>
60  NdArray<dtype> rankFilter(const NdArray<dtype>& inImageArray, uint32 inSize, uint32 inRank,
61  Boundary inBoundaryType = Boundary::REFLECT, dtype inConstantValue = 0)
62  {
63  if (inRank < 0 || inRank >= utils::sqr(inSize))
64  {
65  THROW_INVALID_ARGUMENT_ERROR("rank not within filter footprint size.");
66  }
67 
68  NdArray<dtype> arrayWithBoundary = boundary::addBoundary2d(inImageArray, inBoundaryType, inSize, inConstantValue);
69  NdArray<dtype> output(inImageArray.shape());
70 
71  const Shape inShape = inImageArray.shape();
72  const uint32 boundarySize = inSize / 2; // integer division
73  const uint32 endPointRow = boundarySize + inShape.rows;
74  const uint32 endPointCol = boundarySize + inShape.cols;
75 
76  for (uint32 row = boundarySize; row < endPointRow; ++row)
77  {
78  for (uint32 col = boundarySize; col < endPointCol; ++col)
79  {
80  NdArray<dtype> window = arrayWithBoundary(Slice(row - boundarySize, row + boundarySize + 1),
81  Slice(col - boundarySize, col + boundarySize + 1));
82 
83  output(row - boundarySize, col - boundarySize) = sort(window)[inRank];
84  }
85  }
86 
87  return output;
88  }
89  }
90 }
nc::filter::rankFilter
NdArray< dtype > rankFilter(const NdArray< dtype > &inImageArray, uint32 inSize, uint32 inRank, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: rankFilter.hpp:60
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
Error.hpp
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::filter::boundary::addBoundary2d
NdArray< dtype > addBoundary2d(const NdArray< dtype > &inImage, Boundary inBoundaryType, uint32 inKernalSize, dtype inConstantValue=0)
Definition: addBoundary2d.hpp:62
Shape.hpp
nc::filter::Boundary::REFLECT
@ REFLECT
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
nc::filter::Boundary
Boundary
Boundary condition to apply to the image filter.
Definition: Boundary.hpp:38
nc::utils::sqr
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:45
Types.hpp
sort.hpp
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
nc::sort
NdArray< dtype > sort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sort.hpp:48
Slice.hpp
addBoundary2d.hpp