NumCpp  2.1.0
A C++ implementation of the Python Numpy library
convolve.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/Functions/dot.hpp"
39 #include "NumCpp/NdArray.hpp"
40 #include "NumCpp/Utils/sqr.hpp"
41 
42 #include <string>
43 
44 namespace nc
45 {
46  namespace filter
47  {
48  //============================================================================
49  // Method Description:
62  template<typename dtype>
63  NdArray<dtype> convolve(const NdArray<dtype>& inImageArray, uint32 inSize,
64  const NdArray<dtype>& inWeights, Boundary inBoundaryType = Boundary::REFLECT, dtype inConstantValue = 0)
65  {
66  if (inWeights.size() != utils::sqr(inSize))
67  {
68  THROW_INVALID_ARGUMENT_ERROR("input weights do no match input kernal size.");
69  }
70 
71  NdArray<dtype> arrayWithBoundary = boundary::addBoundary2d(inImageArray, inBoundaryType, inSize, inConstantValue);
72  NdArray<dtype> output(inImageArray.shape());
73 
74  NdArray<dtype> weightsFlat = rot90(inWeights, 2).flatten();
75  const Shape inShape = inImageArray.shape();
76  const uint32 boundarySize = inSize / 2; // integer division
77  const uint32 endPointRow = boundarySize + inShape.rows;
78  const uint32 endPointCol = boundarySize + inShape.cols;
79 
80  for (uint32 row = boundarySize; row < endPointRow; ++row)
81  {
82  for (uint32 col = boundarySize; col < endPointCol; ++col)
83  {
84  NdArray<dtype> window = arrayWithBoundary(Slice(row - boundarySize, row + boundarySize + 1),
85  Slice(col - boundarySize, col + boundarySize + 1)).flatten();
86 
87  output(row - boundarySize, col - boundarySize) = dot(window, weightsFlat).item();
88  }
89  }
90 
91  return output;
92  }
93  } // namespace filter
94 } // namespace nc
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2958
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
Error.hpp
nc::dot
NdArray< dtype > dot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: dot.hpp:48
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
dot.hpp
Boundary.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::filter::convolve
NdArray< dtype > convolve(const NdArray< dtype > &inImageArray, uint32 inSize, const NdArray< dtype > &inWeights, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: convolve.hpp:63
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4326
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::NdArray::flatten
NdArray< dtype > flatten() const
Definition: NdArrayCore.hpp:2775
nc::filter::Boundary::REFLECT
@ REFLECT
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
sqr.hpp
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
nc::rot90
NdArray< dtype > rot90(const NdArray< dtype > &inArray, uint8 inK=1)
Definition: rot90.hpp:52
Types.hpp
rot90.hpp
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
Slice.hpp
addBoundary2d.hpp