-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPratiMediodBGS.hpp
129 lines (97 loc) · 3.57 KB
/
PratiMediodBGS.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/****************************************************************************
*
* PratiMediodBGS.hpp
*
* Purpose: Implementation of the temporal median background
* subtraction algorithm described in:
*
* [1] "Detecting Moving Objects, Shosts, and Shadows in Video Stream"
* by R. Cucchiara et al (2003)
*
* [2] "Reliable Background Suppression for Complex Scenes"
* by S. Calderara et al (2006)
*
* Author: Donovan Parks, September 2007
*
* Please note that this is not an implementation of the complete system
* given in the above papers. It simply implements the temporal media background
* subtraction algorithm.
Example:
Algorithms::BackgroundSubtraction::PratiParams params;
params.SetFrameSize(width, height);
params.LowThreshold() = 30;
params.HighThreshold() = 2*params.LowThreshold(); // Note: high threshold is used by post-processing
params.SamplingRate() = 5;
params.HistorySize() = 16;
params.Weight() = 5;
Algorithms::BackgroundSubtraction::PratiMediodBGS bgs;
bgs.Initalize(params);
******************************************************************************/
#ifndef PRATI_MEDIA_BGS_H
#define PRATI_MEDIA_BGS_H
#include <vector>
#include "Bgs.hpp"
namespace Algorithms
{
namespace BackgroundSubtraction
{
// --- Parameters used by the Prati Mediod BGS algorithm ---
class PratiParams : public BgsParams
{
public:
unsigned int &LowThreshold() { return m_low_threshold; }
unsigned int &HighThreshold() { return m_high_threshold; }
int &Weight() { return m_weight; }
int &SamplingRate() { return m_sampling_rate; }
int &HistorySize() { return m_history_size; }
private:
// The low threshold is used to supress noise. The high thresohld is used
// to find pixels highly likely to be foreground. This implementation uses an L-inf
// distance measure and a pixel p is considered F/G if D(I(p), B(p)) > threshold.
// The two threshold maps are combined as in [2].
unsigned int m_low_threshold;
unsigned int m_high_threshold;
// The weight parameter controls the amount of influence given to previous background samples
// see w_b in equation (2) of [1]
// in [2] this value is set to 1
int m_weight;
// Number of samples to consider when calculating temporal mediod value
int m_history_size;
// Rate at which to obtain new samples
int m_sampling_rate;
};
// --- Prati Mediod BGS algorithm ---
class PratiMediodBGS : public Bgs
{
private:
// sum of L-inf distances from a sample point to all other sample points
struct MEDIAN_BUFFER
{
std::vector<RgbPixel> pixels; // vector of pixels at give location in image
std::vector<int> dist; // distance from pixel to all other pixels
int pos; // current position in circular buffer
RgbPixel median; // median at this pixel location
int medianDist; // distance from median pixel to all other pixels
};
public:
PratiMediodBGS();
~PratiMediodBGS();
void Initalize(const BgsParams& param);
void InitModel(const RgbImage& data);
void Subtract(int frame_num, const RgbImage& data,
BwImage& low_threshold_mask, BwImage& high_threshold_mask);
void Update(int frame_num, const RgbImage& data, const BwImage& update_mask);
RgbImage* Background() { return &m_background; }
private:
MEDIAN_BUFFER* m_median_buffer;
void CalculateMasks(int r, int c, const RgbPixel& pixel);
void Combine(const BwImage& low_mask, const BwImage& high_mask, BwImage& output);
void UpdateMediod(int r, int c, const RgbImage& new_frame, int& dist);
PratiParams m_params;
RgbImage m_background;
BwImage m_mask_low_threshold;
BwImage m_mask_high_threshold;
};
};
};
#endif