-
Notifications
You must be signed in to change notification settings - Fork 19
/
AdaptiveMedianBGS.cpp
123 lines (108 loc) · 3.46 KB
/
AdaptiveMedianBGS.cpp
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
/****************************************************************************
*
* AdaptiveMedianBGS.cpp
*
* Purpose: Implementation of the simple adaptive median background
* subtraction algorithm described in:
* "Segmentation and tracking of piglets in images"
* by McFarlane and Schofield
*
* Author: Donovan Parks, September 2007
*
******************************************************************************/
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include "AdaptiveMedianBGS.hpp"
using namespace Algorithms::BackgroundSubtraction;
void AdaptiveMedianBGS::Initalize(const BgsParams& param)
{
m_params = (AdaptiveMedianParams&)param;
m_median = cvCreateImage(cvSize(m_params.Width(), m_params.Height()), IPL_DEPTH_8U, 3);
cvSet(m_median.Ptr(), CV_RGB(BACKGROUND,BACKGROUND,BACKGROUND));
}
RgbImage* AdaptiveMedianBGS::Background()
{
return &m_median;
}
void AdaptiveMedianBGS::InitModel(const RgbImage& data)
{
// initialize the background model
for (unsigned int r = 0; r < m_params.Height(); ++r)
{
for(unsigned int c = 0; c < m_params.Width(); ++c)
{
m_median(r,c) = data(r,c);
}
}
}
void AdaptiveMedianBGS::Update(int frame_num, const RgbImage& data, const BwImage& update_mask)
{
if((frame_num % m_params.SamplingRate() == 1) || (frame_num < m_params.LearningFrames()))
{
// update background model
for (unsigned int r = 0; r < m_params.Height(); ++r)
{
for(unsigned int c = 0; c < m_params.Width(); ++c)
{
// perform conditional updating only if we are passed the learning phase
if(update_mask(r,c) == BACKGROUND || frame_num < m_params.LearningFrames())
{
for(int ch = 0; ch < NUM_CHANNELS; ++ch)
{
if(data(r,c,ch) > m_median(r,c,ch))
{
m_median(r,c,ch)++;
}
else if(data(r,c,ch) < m_median(r,c,ch))
{
m_median(r,c,ch)--;
}
}
}
}
}
}
}
void AdaptiveMedianBGS::SubtractPixel(int r, int c, const RgbPixel& pixel,
unsigned char& low_threshold, unsigned char& high_threshold)
{
// perform background subtraction
low_threshold = high_threshold = FOREGROUND;
int diffR = abs(pixel(0) - m_median(r,c,0));
int diffG = abs(pixel(1) - m_median(r,c,1));
int diffB = abs(pixel(2) - m_median(r,c,2));
if(diffR <= m_params.LowThreshold() && diffG <= m_params.LowThreshold() && diffB <= m_params.LowThreshold())
{
low_threshold = BACKGROUND;
}
if(diffR <= m_params.HighThreshold() && diffG <= m_params.HighThreshold() && diffB <= m_params.HighThreshold())
{
high_threshold = BACKGROUND;
}
}
///////////////////////////////////////////////////////////////////////////////
//Input:
// data - a pointer to the image data
//Output:
// output - a pointer to the data of a gray value image
// (the memory should already be reserved)
// values: 255-foreground, 0-background
///////////////////////////////////////////////////////////////////////////////
void AdaptiveMedianBGS::Subtract(int frame_num, const RgbImage& data,
BwImage& low_threshold_mask, BwImage& high_threshold_mask)
{
unsigned char low_threshold, high_threshold;
// update each pixel of the image
for(unsigned int r = 0; r < m_params.Height(); ++r)
{
for(unsigned int c = 0; c < m_params.Width(); ++c)
{
// perform background subtraction
SubtractPixel(r, c, data(r,c), low_threshold, high_threshold);
// setup silhouette mask
low_threshold_mask(r,c) = low_threshold;
high_threshold_mask(r,c) = high_threshold;
}
}
}