Skip to content
Snippets Groups Projects
Select Git revision
  • 7201cda8bb44ecc3eafc2ba70f0c243998e786f0
  • 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

Config.h

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    IBGS.h 4.40 KiB
    #pragma once
    
    #include <iostream>
    #include <fstream>
    #include <list>
    #include <memory>
    #include <string>
    #include <functional>
    #include <map>
    
    #include <opencv2/opencv.hpp>
    
    // opencv legacy includes
    #include <opencv2/imgproc/types_c.h>
    #include <opencv2/imgproc/imgproc_c.h>
    #ifndef MEX_COMPILE_FLAG
    #include <opencv2/highgui/highgui_c.h>
    #endif
    
    #include "../utils/ILoadSaveConfig.h"
    
    #if !defined(bgs_register)
    #define bgs_register(x) static BGS_Register<x> register_##x(quote(x))
    #endif
    
    namespace bgslibrary
    {
      namespace algorithms
      {
        class IBGS : public ILoadSaveConfig
        {
        private:
          friend std::ostream& operator<<(std::ostream& o, const std::shared_ptr<IBGS>& ibgs) {
            return ibgs.get()->dump(o);
          }
          friend std::ostream& operator<<(std::ostream& o, const IBGS *ibgs) {
            return ibgs->dump(o);
          }
          friend std::string to_string(const std::shared_ptr<IBGS>& ibgs) {
            std::ostringstream ss;
            ss << ibgs;
            return ss.str();
          }
        public:
          virtual std::ostream& dump(std::ostream& o) const {
            return o << getAlgorithmName();
          }
          std::string getAlgorithmName() const {
            return algorithmName;
          }
          void setShowOutput(const bool _showOutput) {
            showOutput = _showOutput;
          }
          cv::Mat apply(const cv::Mat &img_input) {
            setShowOutput(false);
            cv::Mat _img_foreground;
            cv::Mat _img_background;
            process(img_input, _img_foreground, _img_background);
                    _img_background.copyTo(img_background);
            return _img_foreground;
          }
          cv::Mat getBackgroundModel() {
            return img_background;
          }
          IBGS(const std::string _algorithmName){
            //debug_construction(IBGS);
            algorithmName = _algorithmName;
          }
          IBGS(){
            //debug_construction(IBGS);
            algorithmName = "";
          }
          virtual ~IBGS() {
            //debug_destruction(IBGS);
          }
          virtual void process(const cv::Mat &img_input, cv::Mat &img_foreground, cv::Mat &img_background) = 0;
        protected:
          std::string algorithmName;
          bool firstTime = true;
          bool showOutput = true;
          cv::Mat img_background;
          cv::Mat img_foreground;
          void init(const cv::Mat &img_input, cv::Mat &img_outfg, cv::Mat &img_outbg) {
            assert(img_input.empty() == false);
            //img_outfg = cv::Mat::zeros(img_input.size(), img_input.type());
            //img_outbg = cv::Mat::zeros(img_input.size(), img_input.type());
            img_outfg = cv::Mat::zeros(img_input.size(), CV_8UC1);
            img_outbg = cv::Mat::zeros(img_input.size(), CV_8UC3);
          }
        };
        
        class BGS_Factory
        {
        public:
          BGS_Factory() {
            //debug_construction(BGS_Factory);
          }
          virtual ~BGS_Factory() {
            //debug_destruction(BGS_Factory);
          }
          static BGS_Factory* Instance() {
            static BGS_Factory factory;
            return &factory;
          }
    
          std::shared_ptr<IBGS> Create(std::string name) {
            IBGS* instance = nullptr;
    
            // find name in the registry and call factory method.
            auto it = factoryFunctionRegistry.find(name);
            if (it != factoryFunctionRegistry.end())
              instance = it->second();
    
            // wrap instance in a shared ptr and return
            if (instance != nullptr)
              return std::shared_ptr<IBGS>(instance);
            else
              return nullptr;
          }
    
          std::vector<std::string> GetRegisteredAlgorithmsName() {
            std::vector<std::string> algorithmsName;
            for (auto it = factoryFunctionRegistry.begin(); it != factoryFunctionRegistry.end(); ++it) {
              algorithmsName.push_back(it->first);
            }
            return algorithmsName;
          }
    
          void RegisterFactoryFunction(std::string name,
            std::function<IBGS*(void)> classFactoryFunction) {
            // register the class factory function
            factoryFunctionRegistry[name] = classFactoryFunction;
          }
          
        private:
          std::map<std::string, std::function<IBGS*(void)>> factoryFunctionRegistry;
        };
    
        template<class T>
        class BGS_Register
        {
        public:
          BGS_Register(const std::string className) {
            //debug_construction(BGS_Register);
            // register the class factory function
            BGS_Factory::Instance()->RegisterFactoryFunction(className,
              [](void) -> IBGS* { return new T(); });
          }
          virtual ~BGS_Register() {
            //debug_destruction(BGS_Register);
          }
        };
      }
    }