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