void AdaptiveBackgroundLearning::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
{
  if(img_input.empty())
    return;

  loadConfig();

  if(firstTime)
    saveConfig();

  if(img_background.empty())
    img_input.copyTo(img_background);

  cv::Mat img_input_f(img_input.size(), CV_32F);
  img_input.convertTo(img_input_f, CV_32F, 1./255.);

  cv::Mat img_background_f(img_background.size(), CV_32F);
  img_background.convertTo(img_background_f, CV_32F, 1./255.);

  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 < 0)
  {
    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);

    counter=0;
  }
  else counter++;
  
  cv::Mat img_foreground(img_input.size(), CV_8U);
  img_diff_f.convertTo(img_foreground, CV_8U, 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);
  
  if(showForeground)
    cv::imshow("A-Learning FG", img_foreground);

  if(showBackground)
    cv::imshow("A-Learning BG", img_background);

  img_foreground.copyTo(img_output);
  img_background.copyTo(img_bgmodel);

  firstTime = false;
}
void VuMeter::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
{
  if(img_input.empty())
    return;
  else
    frame = new IplImage(img_input);

  loadConfig();

  if(firstTime)
  {
    bgs.SetAlpha(alpha);
    bgs.SetBinSize(binSize);
    bgs.SetThreshold(threshold);

    gray = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
    cvCvtColor(frame,gray,CV_RGB2GRAY);

    background = cvCreateImage(cvGetSize(gray),IPL_DEPTH_8U,1);
    cvCopy(gray, background);

    mask = cvCreateImage(cvGetSize(gray),IPL_DEPTH_8U,1);
    cvZero(mask);

    saveConfig();
  }
  else
    cvCvtColor(frame,gray,CV_RGB2GRAY);
  
  bgs.UpdateBackground(gray,background,mask);
  cv::Mat img_foreground(mask);
  cv::Mat img_bkg(background);

  if(enableFilter)
  {
    cv::erode(img_foreground,img_foreground,cv::Mat());
    cv::medianBlur(img_foreground, img_foreground, 5);
  }

  if(showOutput)
  {
    if(!img_foreground.empty())
      cv::imshow("VuMeter", img_foreground);
    
    if(!img_bkg.empty())
      cv::imshow("VuMeter Bkg Model", img_bkg);
  }

  img_foreground.copyTo(img_output);
  img_bkg.copyTo(img_bgmodel);
  
  delete frame;
  firstTime = false;
}