Skip to content

Commit

Permalink
Renamed limit and counter to maxLearningFrames and currentLearningFra…
Browse files Browse the repository at this point in the history
…me. Removed unnecessary temporary matrix img_new_background
  • Loading branch information
andrewssobral committed Jul 27, 2024
1 parent c4e35fb commit 64dec9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
25 changes: 9 additions & 16 deletions bgslibrary/algorithms/AdaptiveBackgroundLearning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace bgslibrary::algorithms;

AdaptiveBackgroundLearning::AdaptiveBackgroundLearning() :
IBGS(quote(AdaptiveBackgroundLearning)),
alpha(0.05), limit(-1), counter(0), minVal(0.0),
alpha(0.05), maxLearningFrames(-1), currentLearningFrame(0), minVal(0.0),
maxVal(1.0), enableThreshold(true), threshold(15)
{
debug_construction(AdaptiveBackgroundLearning);
Expand All @@ -31,24 +31,17 @@ void AdaptiveBackgroundLearning::process(const cv::Mat &img_input, cv::Mat &img_
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)
{
// Adaptive learning phase (controlled by maxLearningFrames)
if ((maxLearningFrames > 0 && currentLearningFrame < maxLearningFrames) || maxLearningFrames == -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++;
img_background_f.convertTo(img_background, CV_8U, 255.0 / (maxVal - minVal), -minVal);

if (maxLearningFrames > 0 && currentLearningFrame < maxLearningFrames)
currentLearningFrame++;
}

//cv::Mat img_foreground(img_input.size(), CV_8U);
img_diff_f.convertTo(img_foreground, CV_8UC1, 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);

Expand All @@ -67,15 +60,15 @@ void AdaptiveBackgroundLearning::process(const cv::Mat &img_input, cv::Mat &img_

void AdaptiveBackgroundLearning::save_config(cv::FileStorage &fs) {
fs << "alpha" << alpha;
fs << "limit" << limit;
fs << "maxLearningFrames" << maxLearningFrames;
fs << "enableThreshold" << enableThreshold;
fs << "threshold" << threshold;
fs << "showOutput" << showOutput;
}

void AdaptiveBackgroundLearning::load_config(cv::FileStorage &fs) {
fs["alpha"] >> alpha;
fs["limit"] >> limit;
fs["maxLearningFrames"] >> maxLearningFrames;
fs["enableThreshold"] >> enableThreshold;
fs["threshold"] >> threshold;
fs["showOutput"] >> showOutput;
Expand Down
4 changes: 2 additions & 2 deletions bgslibrary/algorithms/AdaptiveBackgroundLearning.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace bgslibrary
{
private:
double alpha;
int limit;
long counter;
int maxLearningFrames;
long currentLearningFrame;
double minVal;
double maxVal;
bool enableThreshold;
Expand Down

0 comments on commit 64dec9e

Please sign in to comment.