|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <opencv2/features2d/features2d.hpp> |
| 4 | +#include <opencv2/video/background_segm.hpp> |
| 5 | +#include "LBSP.h" |
| 6 | + |
| 7 | +/*! |
| 8 | + Local Binary Similarity Pattern (LBSP)-based change detection algorithm (abstract version/base class). |
| 9 | +
|
| 10 | + For more details on the different parameters, see P.-L. St-Charles and G.-A. Bilodeau, "Improving Background |
| 11 | + Subtraction using Local Binary Similarity Patterns", in WACV 2014, or G.-A. Bilodeau et al, "Change Detection |
| 12 | + in Feature Space Using Local Binary Similarity Patterns", in CRV 2013. |
| 13 | +
|
| 14 | + This algorithm is currently NOT thread-safe. |
| 15 | + */ |
| 16 | +class BackgroundSubtractorLBSP : public cv::BackgroundSubtractor { |
| 17 | +public: |
| 18 | + //! full constructor |
| 19 | + BackgroundSubtractorLBSP(float fRelLBSPThreshold, size_t nLBSPThresholdOffset=0); |
| 20 | + //! default destructor |
| 21 | + virtual ~BackgroundSubtractorLBSP(); |
| 22 | + //! (re)initiaization method; needs to be called before starting background subtraction |
| 23 | + virtual void initialize(const cv::Mat& oInitImg); |
| 24 | + //! (re)initiaization method; needs to be called before starting background subtraction |
| 25 | + virtual void initialize(const cv::Mat& oInitImg, const cv::Mat& oROI)=0; |
| 26 | + //! primary model update function; the learning param is used to override the internal learning speed (ignored when <= 0) |
| 27 | + virtual void operator()(cv::InputArray image, cv::OutputArray fgmask, double learningRate=0)=0; |
| 28 | + //! unused, always returns nullptr |
| 29 | + virtual cv::Algorithm* info() const; |
| 30 | + //! returns a copy of the ROI used for descriptor extraction |
| 31 | + virtual cv::Mat getROICopy() const; |
| 32 | + //! sets the ROI to be used for descriptor extraction (note: this function will reinit the model and return the usable ROI) |
| 33 | + virtual void setROI(cv::Mat& oROI); |
| 34 | + //! turns automatic model reset on or off |
| 35 | + void setAutomaticModelReset(bool); |
| 36 | + |
| 37 | +protected: |
| 38 | + struct PxInfoBase { |
| 39 | + int nImgCoord_Y; |
| 40 | + int nImgCoord_X; |
| 41 | + size_t nModelIdx; |
| 42 | + }; |
| 43 | + //! background model ROI used for LBSP descriptor extraction (specific to the input image size) |
| 44 | + cv::Mat m_oROI; |
| 45 | + //! input image size |
| 46 | + cv::Size m_oImgSize; |
| 47 | + //! input image channel size |
| 48 | + size_t m_nImgChannels; |
| 49 | + //! input image type |
| 50 | + int m_nImgType; |
| 51 | + //! LBSP internal threshold offset value, used to reduce texture noise in dark regions |
| 52 | + const size_t m_nLBSPThresholdOffset; |
| 53 | + //! LBSP relative internal threshold (kept here since we don't keep an LBSP object) |
| 54 | + const float m_fRelLBSPThreshold; |
| 55 | + //! total number of pixels (depends on the input frame size) & total number of relevant pixels |
| 56 | + size_t m_nTotPxCount, m_nTotRelevantPxCount; |
| 57 | + //! current frame index, frame count since last model reset & model reset cooldown counters |
| 58 | + size_t m_nFrameIndex, m_nFramesSinceLastReset, m_nModelResetCooldown; |
| 59 | + //! pre-allocated internal LBSP threshold values LUT for all possible 8-bit intensities |
| 60 | + size_t m_anLBSPThreshold_8bitLUT[UCHAR_MAX+1]; |
| 61 | + //! internal pixel index LUT for all relevant analysis regions (based on the provided ROI) |
| 62 | + size_t* m_aPxIdxLUT; |
| 63 | + //! internal pixel info LUT for all possible pixel indexes |
| 64 | + PxInfoBase* m_aPxInfoLUT; |
| 65 | + //! default kernel size for median blur post-proc filtering |
| 66 | + const int m_nDefaultMedianBlurKernelSize; |
| 67 | + //! specifies whether the algorithm is fully initialized or not |
| 68 | + bool m_bInitialized; |
| 69 | + //! specifies whether automatic model resets are enabled or not |
| 70 | + bool m_bAutoModelResetEnabled; |
| 71 | + //! specifies whether the camera is considered moving or not |
| 72 | + bool m_bUsingMovingCamera; |
| 73 | + //! copy of latest pixel intensities (used when refreshing model) |
| 74 | + cv::Mat m_oLastColorFrame; |
| 75 | + //! copy of latest descriptors (used when refreshing model) |
| 76 | + cv::Mat m_oLastDescFrame; |
| 77 | + //! the foreground mask generated by the method at [t-1] |
| 78 | + cv::Mat m_oLastFGMask; |
| 79 | + |
| 80 | +public: |
| 81 | + // ######## DEBUG PURPOSES ONLY ########## |
| 82 | + int nDebugCoordX, nDebugCoordY; |
| 83 | + std::string sDebugName; |
| 84 | +}; |
| 85 | + |
0 commit comments