NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
convolve.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <string>
31 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Slice.hpp"
35 #include "NumCpp/Core/Types.hpp"
38 #include "NumCpp/Functions/dot.hpp"
40 #include "NumCpp/NdArray.hpp"
41 #include "NumCpp/Utils/sqr.hpp"
42 
43 namespace nc
44 {
45  namespace filter
46  {
47  //============================================================================
48  // Method Description:
61  template<typename dtype>
62  NdArray<dtype> convolve(const NdArray<dtype>& inImageArray,
63  uint32 inSize,
64  const NdArray<dtype>& inWeights,
65  Boundary inBoundaryType = Boundary::REFLECT,
66  dtype inConstantValue = 0)
67  {
68  if (inWeights.size() != utils::sqr(inSize))
69  {
70  THROW_INVALID_ARGUMENT_ERROR("input weights do no match input kernal size.");
71  }
72 
73  NdArray<dtype> arrayWithBoundary =
74  boundary::addBoundary2d(inImageArray, inBoundaryType, inSize, inConstantValue);
75  NdArray<dtype> output(inImageArray.shape());
76 
77  NdArray<dtype> weightsFlat = rot90(inWeights, 2).flatten();
78  const Shape inShape = inImageArray.shape();
79  const uint32 boundarySize = inSize / 2; // integer division
80  const uint32 endPointRow = boundarySize + inShape.rows;
81  const uint32 endPointCol = boundarySize + inShape.cols;
82 
83  for (uint32 row = boundarySize; row < endPointRow; ++row)
84  {
85  for (uint32 col = boundarySize; col < endPointCol; ++col)
86  {
87  NdArray<dtype> window = arrayWithBoundary(Slice(row - boundarySize, row + boundarySize + 1),
88  Slice(col - boundarySize, col + boundarySize + 1))
89  .flatten();
90 
91  output(row - boundarySize, col - boundarySize) = dot(window, weightsFlat).item();
92  }
93  }
94 
95  return output;
96  }
97  } // namespace filter
98 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4289
NdArray< dtype > flatten() const
Definition: NdArrayCore.hpp:2751
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
NdArray< dtype > addBoundary2d(const NdArray< dtype > &inImage, Boundary inBoundaryType, uint32 inKernalSize, dtype inConstantValue=0)
Definition: addBoundary2d.hpp:60
NdArray< dtype > convolve(const NdArray< dtype > &inImageArray, uint32 inSize, const NdArray< dtype > &inWeights, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: convolve.hpp:62
Boundary
Boundary condition to apply to the image filter.
Definition: Boundary.hpp:38
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Definition: Coordinate.hpp:45
NdArray< dtype > dot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: dot.hpp:47
NdArray< dtype > rot90(const NdArray< dtype > &inArray, uint8 inK=1)
Definition: rot90.hpp:50
std::uint32_t uint32
Definition: Types.hpp:40