NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
gaussianFilter.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.hpp"
35 
36 #include <cmath>
37 #include <string>
38 #include <utility>
39 
40 namespace nc
41 {
42  namespace filter
43  {
44  //============================================================================
45  // Method Description:
57  template<typename dtype>
58  NdArray<dtype> gaussianFilter(const NdArray<dtype>& inImageArray, double inSigma,
59  Boundary inBoundaryType = Boundary::REFLECT, dtype inConstantValue = 0)
60  {
61  if (inSigma <= 0)
62  {
63  THROW_INVALID_ARGUMENT_ERROR("input sigma value must be greater than zero.");
64  }
65 
66  // calculate the kernel size based off of the input sigma value
67  constexpr uint32 MIN_KERNEL_SIZE = 5;
68  uint32 kernelSize = std::max(static_cast<uint32>(std::ceil(inSigma * 2.0 * 4.0)), MIN_KERNEL_SIZE); // 4 standard deviations
69  if (kernelSize % 2 == 0)
70  {
71  ++kernelSize; // make sure the kernel is an odd size
72  }
73 
74  const auto kernalHalfSize = static_cast<double>(kernelSize / 2); // integer division
75 
76  // calculate the gaussian kernel
77  NdArray<double> kernel(kernelSize);
78  for (double row = 0; row < kernelSize; ++row)
79  {
80  for (double col = 0; col < kernelSize; ++col)
81  {
82  kernel(static_cast<uint32>(row), static_cast<uint32>(col)) =
83  utils::gaussian(row - kernalHalfSize, col - kernalHalfSize, inSigma);
84  }
85  }
86 
87  // normalize the kernel
88  kernel /= kernel.sum().item();
89 
90  // perform the convolution
91  NdArray<dtype> output = convolve(inImageArray.template astype<double>(),
92  kernelSize,
93  kernel,
94  inBoundaryType,
95  inConstantValue).template astype<dtype>();
96 
97  return output;
98  }
99  } // namespace filter
100 } // 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
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4598
value_type item() const
Definition: NdArrayCore.hpp:3102
NdArray< dtype > gaussianFilter(const NdArray< dtype > &inImageArray, double inSigma, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: gaussianFilter.hpp:58
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:37
double gaussian(double inX, double inY, double inSigma) noexcept
Definition: gaussian.hpp:48
Definition: Coordinate.hpp:45
dtype ceil(dtype inValue) noexcept
Definition: ceil.hpp:48
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40