Commit f1a198d1 authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #1064 from sovrasov:bgsegm_sample_update

parents f4da1502 5acfbfad
/*
* FGBGTest.cpp
*
* Created on: May 7, 2012
* Author: Andrew B. Godbehere
*/
#include "opencv2/bgsegm.hpp" #include "opencv2/bgsegm.hpp"
#include "opencv2/videoio.hpp" #include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp" #include "opencv2/highgui.hpp"
...@@ -14,37 +7,67 @@ ...@@ -14,37 +7,67 @@
using namespace cv; using namespace cv;
using namespace cv::bgsegm; using namespace cv::bgsegm;
static void help() const String about =
"\nA program demonstrating the use and capabilities of different background subtraction algrorithms\n"
"Using OpenCV version " + String(CV_VERSION) +
"\nPress q or ESC to exit\n";
const String keys =
"{help h usage ? | | print this message }"
"{vid | | path to a video file }"
"{algo | GMG | name of the algorithm (GMG, CNT, KNN, MOG, MOG2) }"
;
static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName)
{ {
std::cout << Ptr<BackgroundSubtractor> algo;
"\nA program demonstrating the use and capabilities of a particular BackgroundSubtraction\n" if(algoName == String("GMG"))
"algorithm described in A. Godbehere, A. Matsukawa, K. Goldberg, \n" algo = createBackgroundSubtractorGMG(20, 0.7);
"\"Visual Tracking of Human Visitors under Variable-Lighting Conditions for a Responsive\n" else if(algoName == String("CNT"))
"Audio Art Installation\", American Control Conference, 2012, used in an interactive\n" algo = createBackgroundSubtractorCNT();
"installation at the Contemporary Jewish Museum in San Francisco, CA from March 31 through\n" else if(algoName == String("KNN"))
"July 31, 2011.\n" algo = createBackgroundSubtractorKNN();
"Call:\n" else if(algoName == String("MOG"))
"./BackgroundSubtractorGMG_sample\n" algo = createBackgroundSubtractorMOG();
"Using OpenCV version " << CV_VERSION << "\n"<<std::endl; else if(algoName == String("MOG2"))
algo = createBackgroundSubtractorMOG2();
return algo;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
help();
setUseOptimized(true); setUseOptimized(true);
setNumThreads(8); setNumThreads(8);
Ptr<BackgroundSubtractor> fgbg = createBackgroundSubtractorGMG(20, 0.7); CommandLineParser parser(argc, argv, keys);
if (!fgbg) parser.about(about);
parser.printMessage();
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
String videoPath = parser.get<String>("vid");
String algoName = parser.get<String>("algo");
if (!parser.check())
{
parser.printErrors();
return 0;
}
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algoName);
if (!bgfs)
{ {
std::cerr << "Failed to create BackgroundSubtractor.GMG Algorithm." << std::endl; std::cerr << "Failed to create " << algoName << " background subtractor" << std::endl;
return -1; return -1;
} }
VideoCapture cap; VideoCapture cap;
if (argc > 1) if (argc > 1)
cap.open(argv[1]); cap.open(videoPath);
else else
cap.open(0); cap.open(0);
...@@ -65,7 +88,7 @@ int main(int argc, char** argv) ...@@ -65,7 +88,7 @@ int main(int argc, char** argv)
if (frame.empty()) if (frame.empty())
break; break;
fgbg->apply(frame, fgmask); bgfs->apply(frame, fgmask);
frame.convertTo(segm, CV_8U, 0.5); frame.convertTo(segm, CV_8U, 0.5);
add(frame, Scalar(100, 100, 0), segm, fgmask); add(frame, Scalar(100, 100, 0), segm, fgmask);
......
...@@ -341,9 +341,12 @@ private: ...@@ -341,9 +341,12 @@ private:
void BackgroundSubtractorCNTImpl::apply(InputArray image, OutputArray _fgmask, double learningRate) void BackgroundSubtractorCNTImpl::apply(InputArray image, OutputArray _fgmask, double learningRate)
{ {
CV_Assert(image.type() == CV_8UC1); CV_Assert(image.depth() == CV_8U);
Mat frameIn = image.getMat(); Mat frameIn = image.getMat();
if(frameIn.channels() != 1)
cvtColor(frameIn, frameIn, COLOR_BGR2GRAY);
_fgmask.create(image.size(), CV_8U); // OutputArray usage requires this step _fgmask.create(image.size(), CV_8U); // OutputArray usage requires this step
Mat fgMask = _fgmask.getMat(); Mat fgMask = _fgmask.getMat();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment