annotate maths/MedianFilter.h @ 389:6577e8dda1c2

Add median filter (originally from devuvuzelator code)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 04 Apr 2014 12:40:40 +0100
parents
children ec9f5b9801bd
rev   line source
c@389 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@389 2
c@389 3 #ifndef MEDIAN_FILTER_H
c@389 4 #define MEDIAN_FILTER_H
c@389 5
c@389 6 #include <algorithm>
c@389 7 #include <cassert>
c@389 8 #include <cmath>
c@389 9 #include <iostream>
c@389 10
c@389 11 template <typename T>
c@389 12 class MedianFilter
c@389 13 {
c@389 14 public:
c@389 15 MedianFilter(int size, float percentile = 50.f) :
c@389 16 m_size(size),
c@389 17 m_frame(new T[size]),
c@389 18 m_sorted(new T[size]),
c@389 19 m_sortend(m_sorted + size - 1) {
c@389 20 setPercentile(percentile);
c@389 21 reset();
c@389 22 }
c@389 23
c@389 24 ~MedianFilter() {
c@389 25 delete[] m_frame;
c@389 26 delete[] m_sorted;
c@389 27 }
c@389 28
c@389 29 void setPercentile(float p) {
c@389 30 m_index = int((m_size * p) / 100.f);
c@389 31 if (m_index >= m_size) m_index = m_size-1;
c@389 32 if (m_index < 0) m_index = 0;
c@389 33 }
c@389 34
c@389 35 void push(T value) {
c@389 36 if (value != value) return; // nan
c@389 37 drop(m_frame[0]);
c@389 38 const int sz1 = m_size-1;
c@389 39 for (int i = 0; i < sz1; ++i) m_frame[i] = m_frame[i+1];
c@389 40 m_frame[m_size-1] = value;
c@389 41 put(value);
c@389 42 }
c@389 43
c@389 44 T get() const {
c@389 45 return m_sorted[m_index];
c@389 46 }
c@389 47
c@389 48 T getAt(float percentile) {
c@389 49 int ix = int((m_size * percentile) / 100.f);
c@389 50 if (ix >= m_size) ix = m_size-1;
c@389 51 if (ix < 0) ix = 0;
c@389 52 return m_sorted[ix];
c@389 53 }
c@389 54
c@389 55 void reset() {
c@389 56 for (int i = 0; i < m_size; ++i) m_frame[i] = 0;
c@389 57 for (int i = 0; i < m_size; ++i) m_sorted[i] = 0;
c@389 58 }
c@389 59
c@389 60 private:
c@389 61 const int m_size;
c@389 62 T *const m_frame;
c@389 63 T *const m_sorted;
c@389 64 T *const m_sortend;
c@389 65 int m_index;
c@389 66
c@389 67 void put(T value) {
c@389 68 // precondition: m_sorted contains m_size-1 values, packed at start
c@389 69 // postcondition: m_sorted contains m_size values, one of which is value
c@389 70 T *point = std::lower_bound(m_sorted, m_sortend, value);
c@389 71 const int n = m_sortend - point;
c@389 72 for (int i = n; i > 0; --i) point[i] = point[i-1];
c@389 73 *point = value;
c@389 74 }
c@389 75
c@389 76 void drop(T value) {
c@389 77 // precondition: m_sorted contains m_size values, one of which is value
c@389 78 // postcondition: m_sorted contains m_size-1 values, packed at start
c@389 79 T *point = std::lower_bound(m_sorted, m_sortend + 1, value);
c@389 80 if (*point != value) {
c@389 81 std::cerr << "WARNING: MedianFilter::drop: *point is " << *point
c@389 82 << ", expected " << value << std::endl;
c@389 83 }
c@389 84 const int n = m_sortend - point;
c@389 85 for (int i = 0; i < n; ++i) point[i] = point[i+1];
c@389 86 *m_sortend = T(0);
c@389 87 }
c@389 88 };
c@389 89
c@389 90 #endif
c@389 91