Skip to content
Snippets Groups Projects
Commit ed827659 authored by Andrews Sobral's avatar Andrews Sobral
Browse files

Added automatic object factory. Now it supports retrieving algorithm object by name.

parent 71783317
No related branches found
No related tags found
No related merge requests found
Showing
with 190 additions and 142 deletions
......@@ -15,18 +15,16 @@ You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace cv;
#include "package_bgs/bgslibrary.h"
using namespace ibgs;
int main(int argc, char **argv)
{
std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
VideoCapture capture;
cv::VideoCapture capture;
if (argc > 1)
{
......@@ -43,76 +41,57 @@ int main(int argc, char **argv)
}
/* Background Subtraction Methods */
IBGS *bgs;
bgs = new FrameDifference;
//bgs = new StaticFrameDifference;
//bgs = new WeightedMovingMean;
//bgs = new WeightedMovingVariance;
//bgs = new MixtureOfGaussianV1; // only on OpenCV 2.x
//bgs = new MixtureOfGaussianV2;
//bgs = new AdaptiveBackgroundLearning;
//bgs = new AdaptiveSelectiveBackgroundLearning;
//bgs = new GMG; // only on OpenCV 2.x
//bgs = new KNN; // only on OpenCV 3.x
//bgs = new DPAdaptiveMedian;
//bgs = new DPGrimsonGMM;
//bgs = new DPZivkovicAGMM;
//bgs = new DPMean;
//bgs = new DPWrenGA;
//bgs = new DPPratiMediod;
//bgs = new DPEigenbackground;
//bgs = new DPTexture;
//bgs = new T2FGMM_UM;
//bgs = new T2FGMM_UV;
//bgs = new T2FMRF_UM;
//bgs = new T2FMRF_UV;
//bgs = new FuzzySugenoIntegral;
//bgs = new FuzzyChoquetIntegral;
//bgs = new MultiLayer;
//bgs = new PixelBasedAdaptiveSegmenter;
//bgs = new LBSimpleGaussian;
//bgs = new LBFuzzyGaussian;
//bgs = new LBMixtureOfGaussians;
//bgs = new LBAdaptiveSOM;
//bgs = new LBFuzzyAdaptiveSOM;
//bgs = new LBP_MRF;
//bgs = new VuMeter;
//bgs = new KDE;
//bgs = new IndependentMultimodal;
//bgs = new MultiCue;
//bgs = new SigmaDelta;
//bgs = new SuBSENSE;
//bgs = new LOBSTER;
//bgs = new PAWCS;
//bgs = new TwoPoints;
//bgs = new ViBe;
//bgs = new CodeBook;
int key = 0;
cv::Mat img_input;
while (key != 'q')
{
capture >> img_input;
if (img_input.empty()) break;
cv::imshow("input", img_input);
cv::Mat img_mask;
cv::Mat img_bkgmodel;
bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image
auto algorithmsName = BGS_Factory::Instance()->GetRegisteredAlgorithmsName();
//if(!img_mask.empty())
// cv::imshow("Foreground", img_mask);
// do something
key = cvWaitKey(33);
auto key = 0;
for (const std::string& algorithmName : algorithmsName)
{
std::cout << "Running " << algorithmName << std::endl;
auto bgs = BGS_Factory::Instance()->Create(algorithmName);
cv::Mat img_input;
capture.set(CV_CAP_PROP_POS_FRAMES, 0); // Set index to 0 (start frame)
auto frame_counter = 0;
std::cout << "Press 's' to stop:" << std::endl;
while (key != 's')
{
// Capture frame-by-frame
capture >> img_input;
frame_counter += 1;
if (img_input.empty()) break;
cv::imshow("input", img_input);
cv::Mat img_mask;
cv::Mat img_bkgmodel;
try
{
bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image
//if(!img_mask.empty())
// cv::imshow("Foreground", img_mask);
// do something
}
catch (std::exception& e)
{
std::cout << "Exception occurred" << std::endl;
std::cout << e.what() << std::endl;
}
key = cv::waitKey(33);
}
std::cout << "Press 'q' to exit, or anything else to move to the next algorithm:" << std::endl;
key = cv::waitKey(0);
if (key == 'q')
break;
cv::destroyAllWindows();
}
delete bgs;
capture.release();
cvDestroyAllWindows();
return 0;
}
......@@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include "package_bgs/bgslibrary.h"
......@@ -24,84 +25,58 @@ int main(int argc, char **argv)
std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
/* Background Subtraction Methods */
IBGS *bgs;
bgs = new FrameDifference;
//bgs = new StaticFrameDifference;
//bgs = new WeightedMovingMean;
//bgs = new WeightedMovingVariance;
//bgs = new MixtureOfGaussianV1; // only on OpenCV 2.x
//bgs = new MixtureOfGaussianV2;
//bgs = new AdaptiveBackgroundLearning;
//bgs = new AdaptiveSelectiveBackgroundLearning;
//bgs = new GMG; // only on OpenCV 2.x
//bgs = new KNN; // only on OpenCV 3.x
//bgs = new DPAdaptiveMedian;
//bgs = new DPGrimsonGMM;
//bgs = new DPZivkovicAGMM;
//bgs = new DPMean;
//bgs = new DPWrenGA;
//bgs = new DPPratiMediod;
//bgs = new DPEigenbackground;
//bgs = new DPTexture;
//bgs = new T2FGMM_UM;
//bgs = new T2FGMM_UV;
//bgs = new T2FMRF_UM;
//bgs = new T2FMRF_UV;
//bgs = new FuzzySugenoIntegral;
//bgs = new FuzzyChoquetIntegral;
//bgs = new MultiLayer;
//bgs = new PixelBasedAdaptiveSegmenter;
//bgs = new LBSimpleGaussian;
//bgs = new LBFuzzyGaussian;
//bgs = new LBMixtureOfGaussians;
//bgs = new LBAdaptiveSOM;
//bgs = new LBFuzzyAdaptiveSOM;
//bgs = new LBP_MRF;
//bgs = new VuMeter;
//bgs = new KDE;
//bgs = new IndependentMultimodal;
//bgs = new MultiCue;
//bgs = new SigmaDelta;
//bgs = new SuBSENSE;
//bgs = new LOBSTER;
//bgs = new PAWCS;
//bgs = new TwoPoints;
//bgs = new ViBe;
//bgs = new CodeBook;
int frameNumber = 1;
int key = 0;
while (key != 'q')
auto algorithmsName = BGS_Factory::Instance()->GetRegisteredAlgorithmsName();
auto key = 0;
for (const std::string& algorithmName : algorithmsName)
{
std::stringstream ss;
ss << frameNumber;
std::string fileName = "dataset/frames/" + ss.str() + ".png";
std::cout << "reading " << fileName << std::endl;
std::cout << "Running " << algorithmName << std::endl;
auto bgs = BGS_Factory::Instance()->Create(algorithmName);
cv::Mat img_input = cv::imread(fileName, CV_LOAD_IMAGE_COLOR);
auto frame_counter = 0;
std::cout << "Press 's' to stop:" << std::endl;
while (key != 's')
{
// Capture frame-by-frame
frame_counter++;
std::stringstream ss;
ss << frame_counter;
std::string fileName = "dataset/frames/" + ss.str() + ".png";
std::cout << "reading " << fileName << std::endl;
if (img_input.empty())
break;
cv::Mat img_input = cv::imread(fileName, CV_LOAD_IMAGE_COLOR);
if (img_input.empty())
break;
cv::imshow("input", img_input);
cv::imshow("input", img_input);
cv::Mat img_mask;
cv::Mat img_bkgmodel;
bgs->process(img_input, img_mask, img_bkgmodel);
// by default, "bgs->process(.)" automatically shows the foreground mask image
// or set "bgs->setShowOutput(false)" to disable
cv::Mat img_mask;
cv::Mat img_bkgmodel;
try
{
bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image
//if(!img_mask.empty())
// cv::imshow("Foreground", img_mask);
// do something
//if(!img_mask.empty())
// cv::imshow("Foreground", img_mask);
// do something
}
catch (std::exception& e)
{
std::cout << "Exception occurred" << std::endl;
std::cout << e.what() << std::endl;
}
key = cv::waitKey(33);
}
std::cout << "Press 'q' to exit, or anything else to move to the next algorithm:" << std::endl;
key = cv::waitKey(0);
if (key == 'q')
break;
key = cvWaitKey(33);
frameNumber++;
cv::destroyAllWindows();
}
cvWaitKey(0);
delete bgs;
cvDestroyAllWindows();
return 0;
}
......@@ -43,5 +43,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<AdaptiveBackgroundLearning> register_AdaptiveBackgroundLearning("AdaptiveBackgroundLearning");
}
}
......@@ -43,5 +43,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<AdaptiveSelectiveBackgroundLearning> register_AdaptiveSelectiveBackgroundLearning("AdaptiveSelectiveBackgroundLearning");
}
}
......@@ -64,5 +64,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<CodeBook> register_CodeBook("CodeBook");
}
}
......@@ -49,5 +49,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPAdaptiveMedian> register_DPAdaptiveMedian("DPAdaptiveMedian");
}
}
......@@ -51,5 +51,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPEigenbackground> register_DPEigenbackground("DPEigenbackground");
}
}
......@@ -51,5 +51,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPGrimsonGMM> register_DPGrimsonGMM("DPGrimsonGMM");
}
}
......@@ -51,5 +51,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPMean> register_DPMean("DPMean");
}
}
......@@ -52,5 +52,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPPratiMediod> register_DPPratiMediod("DPPratiMediod");
}
}
......@@ -55,5 +55,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPTexture> register_DPTextured("DPTexture");
}
}
......@@ -51,5 +51,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPWrenGA> register_DPWrenGA("DPWrenGA");
}
}
......@@ -51,5 +51,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<DPZivkovicAGMM> register_DPZivkovicAGMM("DPZivkovicAGMM");
}
}
......@@ -38,6 +38,8 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<FrameDifference> register_FrameDifference("FrameDifference");
}
}
......@@ -49,5 +49,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<FuzzyChoquetIntegral> register_FuzzyChoquetIntegral("FuzzyChoquetIntegral");
}
}
......@@ -49,5 +49,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<FuzzySugenoIntegral> register_FuzzySugenoIntegral("FuzzySugenoIntegral");
}
}
......@@ -43,6 +43,8 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<GMG> register_GMG("GMG");
}
}
......
......@@ -19,6 +19,11 @@ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
#include <iostream>
#include <fstream>
#include <list>
#include <memory>
#include <string>
#include <functional>
#include <map>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/imgproc/imgproc_c.h>
......@@ -79,7 +84,62 @@ namespace bgslibrary
virtual void saveConfig() = 0;
virtual void loadConfig() = 0;
};
class BGS_Factory
{
public:
static BGS_Factory* BGS_Factory::Instance()
{
static BGS_Factory factory;
return &factory;
}
std::shared_ptr<IBGS> BGS_Factory::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> BGS_Factory::GetRegisteredAlgorithmsName()
{
std::vector<std::string> algorithmsName;
for (auto it = factoryFunctionRegistry.begin(); it != factoryFunctionRegistry.end(); ++it) {
algorithmsName.push_back(it->first);
}
return algorithmsName;
}
void BGS_Factory::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(std::string className)
{
// register the class factory function
BGS_Factory::Instance()->RegisterFactoryFunction(className,
[](void) -> IBGS* { return new T(); });
}
};
}
}
namespace ibgs = bgslibrary::algorithms;
......@@ -39,5 +39,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<IndependentMultimodal> register_IndependentMultimodal("IndependentMultimodal");
}
}
......@@ -53,5 +53,7 @@ namespace bgslibrary
void saveConfig();
void loadConfig();
};
static BGS_Register<KDE> register_KDE("KDE");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment