FastLED 3.9.15
Loading...
Searching...
No Matches
bilateral_filter_impl.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/math/math.h"
5#include "fl/stl/span.h"
6
7namespace fl {
8namespace detail {
9
10template <typename T, fl::size N = 0>
12 public:
13 explicit BilateralFilterImpl(T sigma_range = T(1.0f))
14 : mSigmaRange(sigma_range), mLastValue(T(0)) {}
15 explicit BilateralFilterImpl(fl::size capacity, T sigma_range)
16 : mBuf(capacity), mSigmaRange(sigma_range), mLastValue(T(0)) {}
17
18 T update(T input) {
19 mBuf.push_back(input);
20 return recompute(input);
21 }
22
24 if (values.size() == 0) return mLastValue;
25 for (fl::size i = 0; i < values.size(); ++i) {
26 mBuf.push_back(values[i]);
27 }
28 return recompute(values[values.size() - 1]);
29 }
30
31 T value() const { return mLastValue; }
32 void reset() { mBuf.clear(); mLastValue = T(0); }
33 bool full() const { return mBuf.full(); }
34 fl::size size() const { return mBuf.size(); }
35 fl::size capacity() const { return mBuf.capacity(); }
36
37 void resize(fl::size new_capacity) {
38 mBuf = circular_buffer<T, N>(new_capacity);
39 mLastValue = T(0);
40 }
41
42 private:
43 T recompute(T reference) {
44 fl::size n = mBuf.size();
45
46 T two_sigma_sq = T(2.0f) * mSigmaRange * mSigmaRange;
47
48 if (two_sigma_sq == T(0)) {
49 mLastValue = reference;
50 return mLastValue;
51 }
52
53 T weighted_sum = T(0);
54 T weight_total = T(0);
55
56 for (fl::size i = 0; i < n; ++i) {
57 T diff = mBuf[i] - reference;
58 T range_weight = fl::exp(-(diff * diff) / two_sigma_sq);
59 weighted_sum = weighted_sum + mBuf[i] * range_weight;
60 weight_total = weight_total + range_weight;
61 }
62
63 if (!(weight_total == T(0))) {
64 mLastValue = weighted_sum / weight_total;
65 } else {
66 mLastValue = reference;
67 }
68 return mLastValue;
69 }
70
74};
75
76} // namespace detail
77} // namespace fl
void resize(fl::size new_capacity)
BilateralFilterImpl(fl::size capacity, T sigma_range)
BilateralFilterImpl(T sigma_range=T(1.0f))
T update(fl::span< const T > values)
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
enable_if< is_fixed_point< T >::value, T >::type exp(T x) FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179