Skip to content

Commit

Permalink
Merge pull request andrewssobral#17 from HemangShah1/bgsB1
Browse files Browse the repository at this point in the history
Added algorithms SuBSENSE and LOBSTER to package_bgs/pl
  • Loading branch information
andrewssobral committed Oct 19, 2014
2 parents 153d012 + b1bccd4 commit 740f6b3
Show file tree
Hide file tree
Showing 19 changed files with 2,644 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
#include "package_bgs/sjn/SJN_MultiCueBGS.h"
#include "package_bgs/bl/SigmaDeltaBGS.h"

#include "package_bgs/pl/SuBSENSE.h"
#include "package_bgs/pl/LOBSTER.h"

int main(int argc, char **argv)
{
std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
Expand Down Expand Up @@ -156,6 +159,10 @@ int main(int argc, char **argv)
/*** BL Package (thanks to Benjamin Laugraud) ***/
//bgs = new SigmaDeltaBGS;

/*** PL Package (thanks to Pierre-Luc) ***/
//bgs = new SuBSENSEBGS();
//bgs = new LOBSTERBGS();

int key = 0;
while(key != 'q')
{
Expand Down
69 changes: 69 additions & 0 deletions package_bgs/pl/BackgroundSubtractorLBSP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "BackgroundSubtractorLBSP.h"
#include "DistanceUtils.h"
#include "RandUtils.h"
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iomanip>
#include <exception>

#ifndef SIZE_MAX
# if __WORDSIZE == 64
# define SIZE_MAX (18446744073709551615UL)
# else
# define SIZE_MAX (4294967295U)
# endif
#endif

// local define used to determine the default median blur kernel size
#define DEFAULT_MEDIAN_BLUR_KERNEL_SIZE (9)

BackgroundSubtractorLBSP::BackgroundSubtractorLBSP(float fRelLBSPThreshold, size_t nLBSPThresholdOffset)
: m_nImgChannels(0)
,m_nImgType(0)
,m_nLBSPThresholdOffset(nLBSPThresholdOffset)
,m_fRelLBSPThreshold(fRelLBSPThreshold)
,m_nTotPxCount(0)
,m_nTotRelevantPxCount(0)
,m_nFrameIndex(SIZE_MAX)
,m_nFramesSinceLastReset(0)
,m_nModelResetCooldown(0)
,m_aPxIdxLUT(nullptr)
,m_aPxInfoLUT(nullptr)
,m_nDefaultMedianBlurKernelSize(DEFAULT_MEDIAN_BLUR_KERNEL_SIZE)
,m_bInitialized(false)
,m_bAutoModelResetEnabled(true)
,m_bUsingMovingCamera(false)
,nDebugCoordX(0),nDebugCoordY(0) {
CV_Assert(m_fRelLBSPThreshold>=0);
}

BackgroundSubtractorLBSP::~BackgroundSubtractorLBSP() {}

void BackgroundSubtractorLBSP::initialize(const cv::Mat& oInitImg) {
this->initialize(oInitImg,cv::Mat());
}

cv::AlgorithmInfo* BackgroundSubtractorLBSP::info() const {
return nullptr;
}

cv::Mat BackgroundSubtractorLBSP::getROICopy() const {
return m_oROI.clone();
}

void BackgroundSubtractorLBSP::setROI(cv::Mat& oROI) {
LBSP::validateROI(oROI);
CV_Assert(cv::countNonZero(oROI)>0);
if(m_bInitialized) {
cv::Mat oLatestBackgroundImage;
getBackgroundImage(oLatestBackgroundImage);
initialize(oLatestBackgroundImage,oROI);
}
else
m_oROI = oROI.clone();
}

void BackgroundSubtractorLBSP::setAutomaticModelReset(bool bVal) {
m_bAutoModelResetEnabled = bVal;
}
85 changes: 85 additions & 0 deletions package_bgs/pl/BackgroundSubtractorLBSP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/background_segm.hpp>
#include "LBSP.h"

/*!
Local Binary Similarity Pattern (LBSP)-based change detection algorithm (abstract version/base class).
For more details on the different parameters, see P.-L. St-Charles and G.-A. Bilodeau, "Improving Background
Subtraction using Local Binary Similarity Patterns", in WACV 2014, or G.-A. Bilodeau et al, "Change Detection
in Feature Space Using Local Binary Similarity Patterns", in CRV 2013.
This algorithm is currently NOT thread-safe.
*/
class BackgroundSubtractorLBSP : public cv::BackgroundSubtractor {
public:
//! full constructor
BackgroundSubtractorLBSP(float fRelLBSPThreshold, size_t nLBSPThresholdOffset=0);
//! default destructor
virtual ~BackgroundSubtractorLBSP();
//! (re)initiaization method; needs to be called before starting background subtraction
virtual void initialize(const cv::Mat& oInitImg);
//! (re)initiaization method; needs to be called before starting background subtraction
virtual void initialize(const cv::Mat& oInitImg, const cv::Mat& oROI)=0;
//! primary model update function; the learning param is used to override the internal learning speed (ignored when <= 0)
virtual void operator()(cv::InputArray image, cv::OutputArray fgmask, double learningRate=0)=0;
//! unused, always returns nullptr
virtual cv::AlgorithmInfo* info() const;
//! returns a copy of the ROI used for descriptor extraction
virtual cv::Mat getROICopy() const;
//! sets the ROI to be used for descriptor extraction (note: this function will reinit the model and return the usable ROI)
virtual void setROI(cv::Mat& oROI);
//! turns automatic model reset on or off
void setAutomaticModelReset(bool);

protected:
struct PxInfoBase {
int nImgCoord_Y;
int nImgCoord_X;
size_t nModelIdx;
};
//! background model ROI used for LBSP descriptor extraction (specific to the input image size)
cv::Mat m_oROI;
//! input image size
cv::Size m_oImgSize;
//! input image channel size
size_t m_nImgChannels;
//! input image type
int m_nImgType;
//! LBSP internal threshold offset value, used to reduce texture noise in dark regions
const size_t m_nLBSPThresholdOffset;
//! LBSP relative internal threshold (kept here since we don't keep an LBSP object)
const float m_fRelLBSPThreshold;
//! total number of pixels (depends on the input frame size) & total number of relevant pixels
size_t m_nTotPxCount, m_nTotRelevantPxCount;
//! current frame index, frame count since last model reset & model reset cooldown counters
size_t m_nFrameIndex, m_nFramesSinceLastReset, m_nModelResetCooldown;
//! pre-allocated internal LBSP threshold values LUT for all possible 8-bit intensities
size_t m_anLBSPThreshold_8bitLUT[UCHAR_MAX+1];
//! internal pixel index LUT for all relevant analysis regions (based on the provided ROI)
size_t* m_aPxIdxLUT;
//! internal pixel info LUT for all possible pixel indexes
PxInfoBase* m_aPxInfoLUT;
//! default kernel size for median blur post-proc filtering
const int m_nDefaultMedianBlurKernelSize;
//! specifies whether the algorithm is fully initialized or not
bool m_bInitialized;
//! specifies whether automatic model resets are enabled or not
bool m_bAutoModelResetEnabled;
//! specifies whether the camera is considered moving or not
bool m_bUsingMovingCamera;
//! copy of latest pixel intensities (used when refreshing model)
cv::Mat m_oLastColorFrame;
//! copy of latest descriptors (used when refreshing model)
cv::Mat m_oLastDescFrame;
//! the foreground mask generated by the method at [t-1]
cv::Mat m_oLastFGMask;

public:
// ######## DEBUG PURPOSES ONLY ##########
int nDebugCoordX, nDebugCoordY;
std::string sDebugName;
};

Loading

0 comments on commit 740f6b3

Please sign in to comment.