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