background subtraction - OpenCV: how to use createBackgroundSubtractorMOG -
i trying go through tutorial på opencv.org:
the mog pointer initialized as
ptr<backgroundsubtractor> pmog; //mog background subtractor
and in main, used in following manner:
pmog = createbackgroundsubtractormog();
however, yields following error:
error: identifier "createbackgroundsubtractormog" undefined
also, when background model updated, following command used:
pmog->apply(frame, fgmaskmog);
which in turn yields following error:
error: class "cv::backgroundsubtractor" has no member "apply"
any idea of can done this? many in advance!
here entire tutorial code:
//opencv #include <opencv2/highgui/highgui.hpp> #include <opencv2/video/background_segm.hpp> //c #include <stdio.h> //c++ #include <iostream> #include <sstream> using namespace cv; using namespace std; //global variables mat frame; //current frame mat fgmaskmog; //fg mask generated mog method mat fgmaskmog2; //fg mask fg mask generated mog2 method ptr<backgroundsubtractor> pmog; //mog background subtractor ptr<backgroundsubtractor> pmog2; //mog2 background subtractor int keyboard; //function declarations void help(); void processvideo(char* videofilename); void processimages(char* firstframefilename); void help() { cout << "--------------------------------------------------------------------------" << endl << "this program shows how use background subtraction methods provided " << endl << " opencv. can process both videos (-vid) , images (-img)." << endl << endl << "usage:" << endl << "./bs {-vid <video filename>|-img <image filename>}" << endl << "for example: ./bs -vid video.avi" << endl << "or: ./bs -img /data/images/1.png" << endl << "--------------------------------------------------------------------------" << endl << endl; } int main(int argc, char* argv[]) { //print information help(); //check input parameter correctness if(argc != 3) { cerr <<"incorret input list" << endl; cerr <<"exiting..." << endl; return exit_failure; } //create gui windows namedwindow("frame"); namedwindow("fg mask mog"); namedwindow("fg mask mog 2"); //create background subtractor objects pmog = createbackgroundsubtractormog(); //mog approach pmog2 = createbackgroundsubtractormog2(); //mog2 approach if(strcmp(argv[1], "-vid") == 0) { //input data coming video processvideo(argv[2]); } else if(strcmp(argv[1], "-img") == 0) { //input data coming sequence of images processimages(argv[2]); } else { //error in reading input parameters cerr <<"please, check input parameters." << endl; cerr <<"exiting..." << endl; return exit_failure; } //destroy gui windows destroyallwindows(); return exit_success; } void processvideo(char* videofilename) { //create capture object videocapture capture(videofilename); if(!capture.isopened()){ //error in opening video input cerr << "unable open video file: " << videofilename << endl; exit(exit_failure); } //read input data. esc or 'q' quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //read current frame if(!capture.read(frame)) { cerr << "unable read next frame." << endl; cerr << "exiting..." << endl; exit(exit_failure); } //update background model pmog->apply(frame, fgmaskmog); pmog2->apply(frame, fgmaskmog2); //get frame number , write on current frame stringstream ss; rectangle(frame, cv::point(10, 2), cv::point(100,20), cv::scalar(255,255,255), -1); ss << capture.get(cap_prop_pos_frames); string framenumberstring = ss.str(); puttext(frame, framenumberstring.c_str(), cv::point(15, 15), font_hershey_simplex, 0.5 , cv::scalar(0,0,0)); //show current frame , fg masks imshow("frame", frame); imshow("fg mask mog", fgmaskmog); imshow("fg mask mog 2", fgmaskmog2); //get input keyboard keyboard = waitkey( 30 ); } //delete capture object capture.release(); } void processimages(char* fistframefilename) { //read first file of sequence frame = imread(fistframefilename); if(!frame.data){ //error in opening first image cerr << "unable open first image frame: " << fistframefilename << endl; exit(exit_failure); } //current image filename string fn(fistframefilename); //read input data. esc or 'q' quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //update background model pmog->apply(frame, fgmaskmog); pmog2->apply(frame, fgmaskmog2); //get frame number , write on current frame size_t index = fn.find_last_of("/"); if(index == string::npos) { index = fn.find_last_of("\\"); } size_t index2 = fn.find_last_of("."); string prefix = fn.substr(0,index+1); string suffix = fn.substr(index2); string framenumberstring = fn.substr(index+1, index2-index-1); istringstream iss(framenumberstring); int framenumber = 0; iss >> framenumber; rectangle(frame, cv::point(10, 2), cv::point(100,20), cv::scalar(255,255,255), -1); puttext(frame, framenumberstring.c_str(), cv::point(15, 15), font_hershey_simplex, 0.5 , cv::scalar(0,0,0)); //show current frame , fg masks imshow("frame", frame); imshow("fg mask mog", fgmaskmog); imshow("fg mask mog 2", fgmaskmog2); //get input keyboard keyboard = waitkey( 30 ); //search next image in sequence ostringstream oss; oss << (framenumber + 1); string nextframenumberstring = oss.str(); string nextframefilename = prefix + nextframenumberstring + suffix; //read next frame frame = imread(nextframefilename); if(!frame.data){ //error in opening next image in sequence cerr << "unable open image frame: " << nextframefilename << endl; exit(exit_failure); } //update path of current frame fn.assign(nextframefilename); } }
i happened meet problem today. tutorial opencv 3.0, not opencv 2.4+, make few changes follows:
//opencv #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/video/background_segm.hpp> //c #include <stdio.h> //c++ #include <iostream> #include <sstream> using namespace cv; using namespace std; //global variables mat frame; //current frame mat fgmaskmog; //fg mask generated mog method mat fgmaskmog2; //fg mask fg mask generated mog2 method ptr<backgroundsubtractor> pmog; //mog background subtractor ptr<backgroundsubtractor> pmog2; //mog2 background subtractor int keyboard; //function declarations void help(); void processvideo(char* videofilename); void processimages(char* firstframefilename); void help() { cout << "--------------------------------------------------------------------------" << endl << "this program shows how use background subtraction methods provided " << endl << " opencv. can process both videos (-vid) , images (-img)." << endl << endl << "usage:" << endl << "./bs {-vid <video filename>|-img <image filename>}" << endl << "for example: ./bs -vid video.avi" << endl << "or: ./bs -img /data/images/1.png" << endl << "--------------------------------------------------------------------------" << endl << endl; } int main(int argc, char* argv[]) { //print information help(); //check input parameter correctness if(argc != 3) { cerr <<"incorret input list" << endl; cerr <<"exiting..." << endl; return exit_failure; } //create gui windows namedwindow("frame"); namedwindow("fg mask mog"); namedwindow("fg mask mog 2"); //create background subtractor objects //note here!!!! pmog= new backgroundsubtractormog(); //mog approach pmog2 = new backgroundsubtractormog2(); //mog2 approach if(strcmp(argv[1], "-vid") == 0) { //input data coming video processvideo(argv[2]); } else if(strcmp(argv[1], "-img") == 0) { //input data coming sequence of images processimages(argv[2]); } else { //error in reading input parameters cerr <<"please, check input parameters." << endl; cerr <<"exiting..." << endl; return exit_failure; } //destroy gui windows destroyallwindows(); return exit_success; } void processvideo(char* videofilename) { //create capture object videocapture capture(videofilename); if(!capture.isopened()){ //error in opening video input cerr << "unable open video file: " << videofilename << endl; exit(exit_failure); } //read input data. esc or 'q' quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //read current frame if(!capture.read(frame)) { cerr << "unable read next frame." << endl; cerr << "exiting..." << endl; exit(exit_failure); } //update background model //and here!!! pmog->operator()(frame, fgmaskmog); pmog2->operator()(frame, fgmaskmog2); //get frame number , write on current frame stringstream ss; rectangle(frame, cv::point(10, 2), cv::point(100,20), cv::scalar(255,255,255), -1); ss << capture.get(cv_cap_prop_pos_frames); string framenumberstring = ss.str(); puttext(frame, framenumberstring.c_str(), cv::point(15, 15), font_hershey_simplex, 0.5 , cv::scalar(0,0,0)); //show current frame , fg masks imshow("frame", frame); imshow("fg mask mog", fgmaskmog); imshow("fg mask mog 2", fgmaskmog2); //get input keyboard keyboard = waitkey( 30 ); } //delete capture object capture.release(); } void processimages(char* fistframefilename) { //read first file of sequence frame = imread(fistframefilename); if(!frame.data){ //error in opening first image cerr << "unable open first image frame: " << fistframefilename << endl; exit(exit_failure); } //current image filename string fn(fistframefilename); //read input data. esc or 'q' quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //update background model //also here!!!! pmog->operator()(frame, fgmaskmog); pmog2->operator()(frame, fgmaskmog2); //get frame number , write on current frame size_t index = fn.find_last_of("/"); if(index == string::npos) { index = fn.find_last_of("\\"); } size_t index2 = fn.find_last_of("."); string prefix = fn.substr(0,index+1); string suffix = fn.substr(index2); string framenumberstring = fn.substr(index+1, index2-index-1); istringstream iss(framenumberstring); int framenumber = 0; iss >> framenumber; rectangle(frame, cv::point(10, 2), cv::point(100,20), cv::scalar(255,255,255), -1); puttext(frame, framenumberstring.c_str(), cv::point(15, 15), font_hershey_simplex, 0.5 , cv::scalar(0,0,0)); //show current frame , fg masks imshow("frame", frame); imshow("fg mask mog", fgmaskmog); imshow("fg mask mog 2", fgmaskmog2); //get input keyboard keyboard = waitkey( 30 ); //search next image in sequence ostringstream oss; oss << (framenumber + 1); string nextframenumberstring = oss.str(); string nextframefilename = prefix + nextframenumberstring + suffix; //read next frame frame = imread(nextframefilename); if(!frame.data){ //error in opening next image in sequence cerr << "unable open image frame: " << nextframefilename << endl; exit(exit_failure); } //update path of current frame fn.assign(nextframefilename); } }
Comments
Post a Comment