NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
gaussianFilter1d.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31
33#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
38
39namespace nc::filter
40{
41 //============================================================================
42 // Method Description:
54 template<typename dtype>
56 double inSigma,
57 Boundary inBoundaryType = Boundary::REFLECT,
58 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 =
68 std::max(static_cast<uint32>(std::ceil(inSigma * 2. * 4.)), 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., inSigma);
81 }
82
83 // normalize the kernel
84 kernel /= kernel.sum().item();
85
86 // perform the convolution
87 NdArray<dtype> output =
88 convolve1d(inImageArray.template astype<double>(), kernel, inBoundaryType, inConstantValue)
89 .template astype<dtype>();
90
91 return output;
92 }
93} // namespace nc::filter
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
value_type item() const
Definition NdArrayCore.hpp:3098
self_type sum(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:4694
Definition addBoundary1d.hpp:42
NdArray< dtype > convolve1d(const NdArray< dtype > &inImageArray, const NdArray< dtype > &inWeights, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition convolve1d.hpp:54
NdArray< dtype > gaussianFilter1d(const NdArray< dtype > &inImageArray, double inSigma, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition gaussianFilter1d.hpp:55
Boundary
Boundary condition to apply to the image filter.
Definition Boundary.hpp:36
@ REFLECT
Definition Boundary.hpp:37
double gaussian1d(double inX, double inMu, double inSigma) noexcept
Definition gaussian1d.hpp:46
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40