Skip to content
Snippets Groups Projects
Select Git revision
  • eae879c06cf318c4bba3cfebaec1ad62697554f3
  • master default protected
  • beta
  • dev
  • andrewssobral-patch-1
  • update
  • thomas-fork
  • 2.0
  • v3.2.0
  • v3.1.0
  • v3.0
  • bgslib_py27_ocv3_win64
  • bgslib_java_2.0.0
  • bgslib_console_2.0.0
  • bgslib_matlab_win64_2.0.0
  • bgslib_qtgui_2.0.0
  • 2.0.0
  • bgs_console_2.0.0
  • bgs_matlab_win64_2.0.0
  • bgs_qtgui_2.0.0
  • v1.9.2_x86_mfc_gui
  • v1.9.2_x64_java_gui
  • v1.9.2_x86_java_gui
23 results

AdaptiveMedianBGS.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    AdaptiveMedianBGS.cpp 3.13 KiB
    #include "AdaptiveMedianBGS.h"
    
    #if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
    
    using namespace bgslibrary::algorithms::dp;
    
    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;
        }
      }
    }
    
    #endif