NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
gaussianFilter1d.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
36 
37 #include <cmath>
38 #include <string>
39 
40 namespace nc
41 {
42  namespace filter
43  {
44  //============================================================================
45  // Method Description:
57  template<typename dtype>
58  NdArray<dtype> gaussianFilter1d(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(1, kernelSize);
78  for (double i = 0; i < kernelSize; ++i)
79  {
80  kernel[static_cast<uint32>(i)] = utils::gaussian1d(i - kernalHalfSize, 0.0, inSigma);
81  }
82 
83  // normalize the kernel
84  kernel /= kernel.sum().item();
85 
86  // perform the convolution
87  NdArray<dtype> output = convolve1d(inImageArray.template astype<double>(),
88  kernel,
89  inBoundaryType,
90  inConstantValue).template astype<dtype>();
91 
92  return output;
93  }
94  } // namespace filter
95 } // namespace nc
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2975
nc::filter::convolve1d
NdArray< dtype > convolve1d(const NdArray< dtype > &inImageArray, const NdArray< dtype > &inWeights, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: convolve1d.hpp:56
Error.hpp
gaussian1d.hpp
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
Boundary.hpp
nc::utils::gaussian1d
double gaussian1d(double inX, double inMu, double inSigma) noexcept
Definition: gaussian1d.hpp:48
nc::NdArray::sum
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4471
nc::filter::Boundary::REFLECT
@ REFLECT
nc
Definition: Coordinate.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::filter::Boundary
Boundary
Boundary condition to apply to the image filter.
Definition: Boundary.hpp:37
nc::max
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:45
Types.hpp
convolve1d.hpp
nc::filter::gaussianFilter1d
NdArray< dtype > gaussianFilter1d(const NdArray< dtype > &inImageArray, double inSigma, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: gaussianFilter1d.hpp:58
nc::ceil
dtype ceil(dtype inValue) noexcept
Definition: ceil.hpp:48