NumCpp  2.7.0
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:
56  template<typename dtype>
57  NdArray<dtype> gaussianFilter(const NdArray<dtype>& inImageArray, double inSigma,
58  Boundary inBoundaryType = Boundary::REFLECT, dtype inConstantValue = 0)
59  {
60  if (inSigma <= 0)
61  {
62  THROW_INVALID_ARGUMENT_ERROR("input sigma value must be greater than zero.");
63  }
64 
65  // calculate the kernel size based off of the input sigma value
66  constexpr uint32 MIN_KERNEL_SIZE = 5;
67  uint32 kernelSize = std::max(static_cast<uint32>(std::ceil(inSigma * 2.0 * 4.0)), MIN_KERNEL_SIZE); // 4 standard deviations
68  if (kernelSize % 2 == 0)
69  {
70  ++kernelSize; // make sure the kernel is an odd size
71  }
72 
73  const auto kernalHalfSize = static_cast<double>(kernelSize / 2); // integer division
74 
75  // calculate the gaussian kernel
76  NdArray<double> kernel(kernelSize);
77  for (double row = 0; row < kernelSize; ++row)
78  {
79  for (double col = 0; col < kernelSize; ++col)
80  {
81  kernel(static_cast<uint32>(row), static_cast<uint32>(col)) =
82  utils::gaussian(row - kernalHalfSize, col - kernalHalfSize, inSigma);
83  }
84  }
85 
86  // normalize the kernel
87  kernel /= kernel.sum().item();
88 
89  // perform the convolution
90  NdArray<dtype> output = convolve(inImageArray.template astype<double>(),
91  kernelSize,
92  kernel,
93  inBoundaryType,
94  inConstantValue).template astype<dtype>();
95 
96  return output;
97  }
98  } // namespace filter
99 } // 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:4392
value_type item() const
Definition: NdArrayCore.hpp:2931
NdArray< dtype > gaussianFilter(const NdArray< dtype > &inImageArray, double inSigma, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: gaussianFilter.hpp:57
NdArray< dtype > convolve(const NdArray< dtype > &inImageArray, uint32 inSize, const NdArray< dtype > &inWeights, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: convolve.hpp:61
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:44
std::uint32_t uint32
Definition: Types.hpp:40