-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdaptiveBackgroundLearning.cpp
105 lines (79 loc) · 3.13 KB
/
AdaptiveBackgroundLearning.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
#include "AdaptiveBackgroundLearning.h"
#include <iostream>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
int i=0,j=0;
AdaptiveBackgroundLearning::AdaptiveBackgroundLearning() : firstTime(true), alpha(0.05), limit(-1), counter(0), minVal(0.0), maxVal(1.0),
enableThreshold(true), threshold(15), showForeground(true), showBackground(true)
{
std::cout << "AdaptiveBackgroundLearning()" << std::endl;
}
AdaptiveBackgroundLearning::~AdaptiveBackgroundLearning()
{
std::cout << "~AdaptiveBackgroundLearning()" << std::endl;
}
void AdaptiveBackgroundLearning::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
{
char path1[1000],path2[1000];
if(img_input.empty())
return;
loadConfig();
if(firstTime)
saveConfig();
if(img_background.empty())
img_input.copyTo(img_background);
cv::Mat img_input_f(img_input.size(), CV_32F);
img_input.convertTo(img_input_f, CV_32F, 1./255.);
cv::Mat img_background_f(img_background.size(), CV_32F);
img_background.convertTo(img_background_f, CV_32F, 1./255.);
cv::Mat img_diff_f(img_input.size(), CV_32F);
cv::absdiff(img_input_f, img_background_f, img_diff_f);
if((limit > 0 && limit < counter) || limit == -1)
{
img_background_f = alpha*img_input_f + (1-alpha)*img_background_f;
cv::Mat img_new_background(img_input.size(), CV_8U);
img_background_f.convertTo(img_new_background, CV_8U, 255.0/(maxVal - minVal), -minVal);
img_new_background.copyTo(img_background);
if(limit > 0 && limit < counter)
counter++;
}
cv::Mat img_foreground(img_input.size(), CV_8U);
img_diff_f.convertTo(img_foreground, CV_8U, 255.0/(maxVal - minVal), -minVal);
if(img_foreground.channels() == 3)
cv::cvtColor(img_foreground, img_foreground, CV_BGR2GRAY);
if(enableThreshold)
cv::threshold(img_foreground, img_foreground, threshold, 255, cv::THRESH_BINARY);
if(showForeground){
cv::imshow("A-Learning FG", img_foreground);
}
if(showBackground){
cv::imshow("A-Learning BG", img_background);
}
img_foreground.copyTo(img_output);
img_background.copyTo(img_bgmodel);
firstTime = false;
}
void AdaptiveBackgroundLearning::saveConfig()
{
CvFileStorage* fs = cvOpenFileStorage("AdaptiveBackgroundLearning.xml", 0, CV_STORAGE_WRITE);
cvWriteReal(fs, "alpha", alpha);
cvWriteInt(fs, "limit", limit);
cvWriteInt(fs, "enableThreshold", enableThreshold);
cvWriteInt(fs, "threshold", threshold);
cvWriteInt(fs, "showForeground", showForeground);
cvWriteInt(fs, "showBackground", showBackground);
cvReleaseFileStorage(&fs);
}
void AdaptiveBackgroundLearning::loadConfig()
{
CvFileStorage* fs = cvOpenFileStorage("AdaptiveBackgroundLearning.xml", 0, CV_STORAGE_READ);
alpha = cvReadRealByName(fs, 0, "alpha", 0.05);
limit = cvReadIntByName(fs, 0, "limit", -1);
enableThreshold = cvReadIntByName(fs, 0, "enableThreshold", true);
threshold = cvReadIntByName(fs, 0, "threshold", 15);
showForeground = cvReadIntByName(fs, 0, "showForeground", true);
showBackground = cvReadIntByName(fs, 0, "showBackground", true);
cvReleaseFileStorage(&fs);
}