16 #ifndef MEDIAN_FILTER_H 17 #define MEDIAN_FILTER_H 32 m_sorted(new T[size]),
33 m_sortend(m_sorted + size - 1) {
44 m_index = int((m_size * p) / 100.f);
45 if (m_index >= m_size) m_index = m_size-1;
46 if (m_index < 0) m_index = 0;
51 static bool warned =
false;
53 std::cerr <<
"WARNING: MedianFilter::push: attempt to push NaN" << std::endl;
54 std::cerr <<
"WARNING: MedianFilter::push: (only one warning will be printed)" << std::endl;
60 const int sz1 = m_size-1;
61 for (
int i = 0; i < sz1; ++i) m_frame[i] = m_frame[i+1];
62 m_frame[m_size-1] = value;
67 return m_sorted[m_index];
75 int ix = int((m_size * percentile) / 100.f);
76 if (ix >= m_size) ix = m_size-1;
82 for (
int i = 0; i < m_size; ++i) m_frame[i] = 0;
83 for (
int i = 0; i < m_size; ++i) m_sorted[i] = 0;
86 static std::vector<T>
filter(
int size,
const std::vector<T> &in) {
89 for (
int i = 0; i < int(in.size()); ++i) {
92 if (i >= size/2) out.push_back(median);
94 while (out.size() < in.size()) {
96 out.push_back(f.
get());
111 T *point = std::lower_bound(m_sorted, m_sortend, value);
112 const int n = m_sortend - point;
113 for (
int i = n; i > 0; --i) point[i] = point[i-1];
120 T *point = std::lower_bound(m_sorted, m_sortend + 1, value);
121 if (*point != value) {
122 std::cerr <<
"WARNING: MedianFilter::drop: *point is " << *point
123 <<
", expected " << value << std::endl;
125 const int n = m_sortend - point;
126 for (
int i = 0; i < n; ++i) point[i] = point[i+1];