NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
wrap2d.hpp
Go to the documentation of this file.
1
28#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
38{
39 //============================================================================
40 // Method Description:
47 template<typename dtype>
48 NdArray<dtype> wrap2d(const NdArray<dtype>& inImage, uint32 inBoundarySize)
49 {
51
52 const Shape inShape = inImage.shape();
53 Shape outShape(inShape);
54 outShape.rows += inBoundarySize * 2;
55 outShape.cols += inBoundarySize * 2;
56
57 NdArray<dtype> outArray(outShape);
58 outArray.put(Slice(inBoundarySize, inBoundarySize + inShape.rows),
59 Slice(inBoundarySize, inBoundarySize + inShape.cols),
60 inImage);
61
62 // bottom
63 outArray.put(Slice(0, inBoundarySize),
64 Slice(inBoundarySize, inBoundarySize + inShape.cols),
65 inImage(Slice(inShape.rows - inBoundarySize, inShape.rows), Slice(0, inShape.cols)));
66
67 // top
68 outArray.put(Slice(inShape.rows + inBoundarySize, outShape.rows),
69 Slice(inBoundarySize, inBoundarySize + inShape.cols),
70 inImage(Slice(0, inBoundarySize), Slice(0, inShape.cols)));
71
72 // left
73 outArray.put(Slice(inBoundarySize, inBoundarySize + inShape.rows),
74 Slice(0, inBoundarySize),
75 inImage(Slice(0, inShape.rows), Slice(inShape.cols - inBoundarySize, inShape.cols)));
76
77 // right
78 outArray.put(Slice(inBoundarySize, inBoundarySize + inShape.rows),
79 Slice(inShape.cols + inBoundarySize, outShape.cols),
80 inImage(Slice(0, inShape.rows), Slice(0, inBoundarySize)));
81
82 // now fill in the corners
83 NdArray<dtype> lowerLeft = outArray(Slice(inBoundarySize, 2 * inBoundarySize), Slice(0, inBoundarySize));
84 NdArray<dtype> lowerRight =
85 outArray(Slice(inBoundarySize, 2 * inBoundarySize), Slice(outShape.cols - inBoundarySize, outShape.cols));
86
87 const uint32 upperRowStart = outShape.rows - 2 * inBoundarySize;
88 NdArray<dtype> upperLeft =
89 outArray(Slice(upperRowStart, upperRowStart + inBoundarySize), Slice(0, inBoundarySize));
90 NdArray<dtype> upperRight = outArray(Slice(upperRowStart, upperRowStart + inBoundarySize),
91 Slice(outShape.cols - inBoundarySize, outShape.cols));
92
93 outArray.put(Slice(0, inBoundarySize), Slice(0, inBoundarySize), upperLeft);
94 outArray.put(Slice(0, inBoundarySize), Slice(outShape.cols - inBoundarySize, outShape.cols), upperRight);
95 outArray.put(Slice(outShape.rows - inBoundarySize, outShape.rows), Slice(0, inBoundarySize), lowerLeft);
96 outArray.put(Slice(outShape.rows - inBoundarySize, outShape.rows),
97 Slice(outShape.cols - inBoundarySize, outShape.cols),
98 lowerRight);
99
100 return outArray;
101 }
102} // namespace nc::filter::boundary
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
self_type & put(index_type inIndex, const value_type &inValue)
Definition: NdArrayCore.hpp:3693
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:45
Definition: addBoundary1d.hpp:44
NdArray< dtype > wrap2d(const NdArray< dtype > &inImage, uint32 inBoundarySize)
Definition: wrap2d.hpp:48
std::uint32_t uint32
Definition: Types.hpp:40