Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initialize repository
  • Loading branch information
Ivan Pozdnyakov authored and Ivan Pozdnyakov committed May 14, 2015
1 parent b04b1ef commit 5306449
Show file tree
Hide file tree
Showing 296 changed files with 2,895 additions and 775 deletions.
44 changes: 44 additions & 0 deletions .gitignore
@@ -0,0 +1,44 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile ~/.gitignore_global

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Video Files
*.mp4
*.avi

# Python Files
*.py

# sqlite DB files
*.db
140 changes: 140 additions & 0 deletions AdaptiveMedianBGS.cpp
@@ -0,0 +1,140 @@
/*
This file is part of BGSLibrary.
BGSLibrary is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BGSLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
/****************************************************************************
*
* 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.h"

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)
{
// 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;
}
}
}

88 changes: 88 additions & 0 deletions AdaptiveMedianBGS.h
@@ -0,0 +1,88 @@
/*
This file is part of BGSLibrary.
BGSLibrary is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BGSLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
/****************************************************************************
*
* AdaptiveMedianBGS.hpp
*
* 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
Example:
Algorithms::BackgroundSubtraction::AdaptiveMedianParams params;
params.SetFrameSize(width, height);
params.LowThreshold() = 40;
params.HighThreshold() = 2*params.LowThreshold();
params.SamplingRate() = 7;
params.LearningFrames() = 30;
Algorithms::BackgroundSubtraction::AdaptiveMedianBGS bgs;
bgs.Initalize(params);
******************************************************************************/

#include "Bgs.h"

namespace Algorithms
{
namespace BackgroundSubtraction
{
// --- Parameters used by the Adaptive Median BGS algorithm ---
class AdaptiveMedianParams : public BgsParams
{
public:
unsigned char &LowThreshold() { return m_low_threshold; }
unsigned char &HighThreshold() { return m_high_threshold; }

int &SamplingRate() { return m_samplingRate; }
int &LearningFrames() { return m_learning_frames; }

private:
unsigned char m_low_threshold;
unsigned char m_high_threshold;

int m_samplingRate;
int m_learning_frames;
};


// --- Adaptive Median BGS algorithm ---
class AdaptiveMedianBGS : public Bgs
{
public:
virtual ~AdaptiveMedianBGS() {}

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();

private:
void SubtractPixel(int r, int c, const RgbPixel& pixel,
unsigned char& low_threshold, unsigned char& high_threshold);

AdaptiveMedianParams m_params;
RgbImage m_median;
};
}
}
67 changes: 67 additions & 0 deletions Bgs.h
@@ -0,0 +1,67 @@
/*
This file is part of BGSLibrary.
BGSLibrary is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BGSLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
/****************************************************************************
*
* Bgs.hpp
*
* Purpose: Base class for BGS algorithms.
*
* Author: Donovan Parks, October 2007
*
******************************************************************************/

#ifndef BGS_H_
#define BGS_H_

#include "Image.h"
#include "BgsParams.h"

namespace Algorithms
{
namespace BackgroundSubtraction
{
class Bgs
{
public:
static const int BACKGROUND = 0;
static const int FOREGROUND = 255;

virtual ~Bgs() {}

// Initialize any data required by the BGS algorithm. Should be called once before calling
// any of the following functions.
virtual void Initalize(const BgsParams& param) = 0;

// Initialize the background model. Typically, the background model is initialized using the first
// frame of the incoming video stream, but alternatives are possible.
virtual void InitModel(const RgbImage& data) = 0;

// Subtract the current frame from the background model and produce a binary foreground mask using
// both a low and high threshold value.
virtual void Subtract(int frame_num, const RgbImage& data,
BwImage& low_threshold_mask, BwImage& high_threshold_mask) = 0;

// Update the background model. Only pixels set to background in update_mask are updated.
virtual void Update(int frame_num, const RgbImage& data, const BwImage& update_mask) = 0;

// Return the current background model.
virtual RgbImage *Background() = 0;
};
}
}

#endif

0 comments on commit 5306449

Please sign in to comment.