NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
mirror2d.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/Core/Slice.hpp"
33 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
36 
37 namespace nc
38 {
39  namespace filter
40  {
41  namespace boundary
42  {
43  //============================================================================
44  // Method Description:
51  template<typename dtype>
52  NdArray<dtype> mirror2d(const NdArray<dtype>& inImage, uint32 inBoundarySize)
53  {
55 
56  const Shape inShape = inImage.shape();
57  Shape outShape(inShape);
58  outShape.rows += inBoundarySize * 2;
59  outShape.cols += inBoundarySize * 2;
60 
61  NdArray<dtype> outArray(outShape);
62  outArray.put(Slice(inBoundarySize, inBoundarySize + inShape.rows),
63  Slice(inBoundarySize, inBoundarySize + inShape.cols),
64  inImage);
65 
66  for (uint32 row = 0; row < inBoundarySize; ++row)
67  {
68  // bottom
69  outArray.put(row,
70  Slice(inBoundarySize, inBoundarySize + inShape.cols),
71  inImage(inBoundarySize - row, Slice(0, inShape.cols)));
72 
73  // top
74  outArray.put(row + inBoundarySize + inShape.rows,
75  Slice(inBoundarySize, inBoundarySize + inShape.cols),
76  inImage(inShape.rows - row - 2, Slice(0, inShape.cols)));
77  }
78 
79  for (uint32 col = 0; col < inBoundarySize; ++col)
80  {
81  // left
82  outArray.put(Slice(inBoundarySize, inBoundarySize + inShape.rows),
83  col,
84  inImage(Slice(0, inShape.rows), inBoundarySize - col));
85 
86  // right
87  outArray.put(Slice(inBoundarySize, inBoundarySize + inShape.rows),
88  col + inBoundarySize + inShape.cols,
89  inImage(Slice(0, inShape.rows), inShape.cols - col - 2));
90  }
91 
92  // now fill in the corners
93  NdArray<dtype> lowerLeft =
94  flipud(outArray(Slice(inBoundarySize + 1, 2 * inBoundarySize + 1), Slice(0, inBoundarySize)));
95  NdArray<dtype> lowerRight = flipud(outArray(Slice(inBoundarySize + 1, 2 * inBoundarySize + 1),
96  Slice(outShape.cols - inBoundarySize, outShape.cols)));
97 
98  const uint32 upperRowStart = outShape.rows - 2 * inBoundarySize - 1;
99  NdArray<dtype> upperLeft =
100  flipud(outArray(Slice(upperRowStart, upperRowStart + inBoundarySize), Slice(0, inBoundarySize)));
101  NdArray<dtype> upperRight = flipud(outArray(Slice(upperRowStart, upperRowStart + inBoundarySize),
102  Slice(outShape.cols - inBoundarySize, outShape.cols)));
103 
104  outArray.put(Slice(0, inBoundarySize), Slice(0, inBoundarySize), lowerLeft);
105  outArray.put(Slice(0, inBoundarySize),
106  Slice(outShape.cols - inBoundarySize, outShape.cols),
107  lowerRight);
108  outArray.put(Slice(outShape.rows - inBoundarySize, outShape.rows), Slice(0, inBoundarySize), upperLeft);
109  outArray.put(Slice(outShape.rows - inBoundarySize, outShape.rows),
110  Slice(outShape.cols - inBoundarySize, outShape.cols),
111  upperRight);
112 
113  return outArray;
114  }
115  } // namespace boundary
116  } // namespace filter
117 } // namespace nc
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
NdArray< dtype > & put(int32 inIndex, value_type inValue)
Definition: NdArrayCore.hpp:3479
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
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 > mirror2d(const NdArray< dtype > &inImage, uint32 inBoundarySize)
Definition: mirror2d.hpp:52
Definition: Coordinate.hpp:45
NdArray< dtype > flipud(const NdArray< dtype > &inArray)
Definition: flipud.hpp:46
std::uint32_t uint32
Definition: Types.hpp:40