4/24/2014

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Background subtractor example souce code.

OpenCV support about 3 types subtraction algorithm.

Those are MOG, MOG2, GMG algorithms.

Detailed algorithm explain is, please refer to opencv documnet.

In this post, just introduce source code and result of background subtraction.

firstly, see the results of 3 algorithms.

origin image

MOG result

MOG2 result

CMG result




Especially, in MOG2 result, we can see the gray pixel color, that means shadow.
This is result video of the source code.
source code is here->  

#include < stdio.h>
#include < iostream>

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2/video/background_segm.hpp>


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")
//#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_ml247d.lib")
//#pragma comment(lib, "opencv_stitching247d.lib");
//#pragma comment(lib, "opencv_nonfree247d.lib");
#pragma comment(lib, "opencv_video247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_objdetect247.lib")
//#pragma comment(lib, "opencv_gpu247.lib")
//#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_ml247.lib")
//#pragma comment(lib, "opencv_stitching247.lib");
//#pragma comment(lib, "opencv_nonfree247.lib");
#pragma comment(lib, "opencv_video247d.lib")
#endif 

using namespace cv;
using namespace std;



int main()
{

 //global variables
 Mat frame; //current frame
 Mat resizeF;
 Mat fgMaskMOG; //fg mask generated by MOG method
 Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
 Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method
 

 Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
 Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
 Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor
 


 pMOG = new BackgroundSubtractorMOG();
 pMOG2 = new BackgroundSubtractorMOG2();
 pGMG = new BackgroundSubtractorGMG();
 

 char fileName[100] = "C:\\POSCO\\video\\/cctv 2.mov"; //Gate1_175_p1.avi"; //mm2.avi"; //";//_p1.avi";
 VideoCapture stream1(fileName);   //0 is the id of video device.0 if you have only one camera   

 Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );   

 //unconditional loop   
 while (true) {   
  Mat cameraFrame;   
  if(!(stream1.read(frame))) //get one frame form video   
   break;
  
  resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) );
  pMOG->operator()(resizeF, fgMaskMOG);
  pMOG2->operator()(resizeF, fgMaskMOG2);
  pGMG->operator()(resizeF, fgMaskGMG);
  //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element); 

 


  imshow("Origin", resizeF);
  imshow("MOG", fgMaskMOG);
  imshow("MOG2", fgMaskMOG2);
  imshow("GMG", fgMaskGMG);
  

  if (waitKey(30) >= 0)   
   break;   
 }

}




..


refer to this url that is 3.2 version example.
http://study.marearts.com/2017/04/opencv-background-subtraction-32.html

38 comments:

  1. dude, how to change the default video on your code?

    where I should put my own video to substitute the video's name that u have used on the code.

    By the Way, AWESOME JOB MAN! :D

    May God Bless U.

    Please Reply ASAP. Thank You

    ReplyDelete
  2. I'm sorry for asking again.

    how can i change the video file into real live video recording with my webcam?
    which part that I should change?

    Kindly Regards,

    Hope you can give me your answer ASAP.

    thanks

    ReplyDelete
  3. I got this error.. what should I do to avoid this error?

    https://www.dropbox.com/s/j0t1x248s7j4xfe/Screenshot%202014-06-30%2020.35.35.png

    Hope u can reply soon.

    Thanks

    ReplyDelete
  4. change like this
    VideoCapture stream1(filename); -> VideoCapture stream1(0);
    then code will get frame of cam image.
    and..
    in you error message, I never seen *.dll file, and this source code don't need that dll.
    so check the dll what for.
    thank you.

    ReplyDelete
  5. hey bro thanks for sharing that its very usefull for me can i get the document for algorithm or kindly send me the link here thanks

    ReplyDelete
  6. and kindly let me know how to get fps in text format so i can compile my results

    ReplyDelete
  7. Hi htc onex
    if you go this page "http://docs.opencv.org/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html", you will find more information. and save text to use FILE * fp; c style, ofstream o; c++ sytyle syntax.
    Thank you

    ReplyDelete
    Replies
    1. thanks bro its relly helpfull could u please tell me how to save fps in text format in opencv 2.3

      Delete
  8. #include < opencv2\opencv.hpp>
    #include < opencv2/core/core.hpp>
    #include < opencv2/highgui/highgui.hpp>
    #include < opencv2/video/background_segm.hpp>
    #include
    using namespace cv;
    using namespace std;



    int main()
    {

    time_t start, end;
    double fps;
    int counter = 0;
    double sec;
    time(&start);
    //global variables
    Mat frame; //current frame
    Mat resize_blur_Img;
    Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
    Mat binaryImg;
    //Mat TestImg;
    Mat ContourImg; //fg mask fg mask generated by MOG2 method
    Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor

    pMOG2 = new BackgroundSubtractorMOG2(56,100,true);//300,0.0);

    char fileName[100] = "park.avi"; //video\\mm2.avi"; //mm2.avi"; //cctv 2.mov"; //mm2.avi"; //";//_p1.avi";
    VideoCapture stream1(fileName); //0 is the id of video device.0 if you have only one camera

    //morphology element
    Mat element = getStructuringElement(MORPH_RECT, Size(7,7), Point(3,3) );

    //unconditional loop
    while (true) {
    Mat cameraFrame;
    if(!(stream1.read(frame))) //get one frame form video
    break;

    //Resize
    resize(frame, resize_blur_Img, Size(frame.size().width/1, frame.size().height/1) );
    //Blur
    blur(resize_blur_Img, resize_blur_Img, Size(1,1) );
    //Background subtraction
    pMOG2->operator()(resize_blur_Img, fgMaskMOG2, -1);//,-0.5);

    ///////////////////////////////////////////////////////////////////
    //pre procesing
    //1 point delete
    //morphologyEx(fgMaskMOG2, fgMaskMOG2, CV_MOP_ERODE, element);
    morphologyEx(fgMaskMOG2, binaryImg, CV_MOP_CLOSE, element);
    //morphologyEx(fgMaskMOG2, testImg, CV_MOP_OPEN, element);

    //Shadow remove
    //Binary
    threshold(binaryImg, binaryImg, 128, 255, CV_THRESH_BINARY);

    //Find contour
    ContourImg = binaryImg.clone();
    //less blob delete
    vector< vector< Point> > contours;
    findContours(ContourImg,
    contours, // a vector of contours
    CV_RETR_EXTERNAL, // retrieve the external contours
    CV_CHAIN_APPROX_NONE); // all pixels of each contours

    vector< Rect > output;
    vector< vector< Point> >::iterator itc= contours.begin();
    while (itc!=contours.end()) {

    //Create bounding rect of object
    //rect draw on origin image
    Rect mr= boundingRect(Mat(*itc));
    rectangle(resize_blur_Img, mr, CV_RGB(255,0,0));
    ++itc;

    time(&end);
    ++counter;
    sec = difftime (end, start);
    fps = counter / sec;
    printf("FPS = %.2f\n", fps);


    }




    ///////////////////////////////////////////////////////////////////

    //Display
    imshow("Shadow_Removed", binaryImg);
    imshow("Blur_Resize", resize_blur_Img);
    imshow("MOG2", fgMaskMOG2);

    if (waitKey(40) >= 0)
    break;
    }

    }

    ReplyDelete
  9. in the above code kindly tell me where i should put *fp; to save my frames per seconds inb text

    ReplyDelete
  10. OK. the method of to save txt is just c, c++ skill.
    There is two method for saving the information of fps in the txt file.
    First is to create txt file every frames.
    The coding style is like that
    int index=0
    while( .. )
    {
    index++;
    char filename[100];
    sprintf(filename, "%d.txt", index);
    FILE * fp = fopen(filename,"w")
    fprintf(fp, "fps = %lf \n", fps )
    fclose(fp)
    }




    why don't you read basic

    ReplyDelete
  11. Second coding style is ...

    FILE * fp = fopen("fps_info.txt","w")

    while(...)
    {
    fprintf("fps: %lf \n", fps)
    }
    fclose(fp)

    ReplyDelete
  12. Thank you.
    And please search about save txt information in c, c++.
    If you really cannot know how to do, i will send full source code for you.
    Try it and ask me again.

    Thank you very much.

    ReplyDelete
  13. thank you so much bro i am not good at it just trying to be i searched alot i coudnt fix my problem thats y i asked u for help thank you so much stay blessed

    ReplyDelete
  14. sorry man i cant fix my problem help me out to rearrange this code

    ReplyDelete
  15. Hi htc onex,
    Is this your asking source code? plz check.
    but I didn't run the source code in vs.

    #include < stdio.h>
    #include < iostream>

    #include < opencv2\opencv.hpp>
    #include < opencv2/core/core.hpp>
    #include < opencv2/highgui/highgui.hpp>
    #include < opencv2/video/background_segm.hpp>


    #ifdef _DEBUG
    #pragma comment(lib, "opencv_core247d.lib")
    #pragma comment(lib, "opencv_imgproc247d.lib") //MAT processing
    #pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
    //#pragma comment(lib, "opencv_gpu247d.lib")
    //#pragma comment(lib, "opencv_features2d247d.lib")
    #pragma comment(lib, "opencv_highgui247d.lib")
    #pragma comment(lib, "opencv_ml247d.lib")
    //#pragma comment(lib, "opencv_stitching247d.lib");
    //#pragma comment(lib, "opencv_nonfree247d.lib");
    #pragma comment(lib, "opencv_video247d.lib")
    #else
    #pragma comment(lib, "opencv_core247.lib")
    #pragma comment(lib, "opencv_imgproc247.lib")
    #pragma comment(lib, "opencv_objdetect247.lib")
    //#pragma comment(lib, "opencv_gpu247.lib")
    //#pragma comment(lib, "opencv_features2d247.lib")
    #pragma comment(lib, "opencv_highgui247.lib")
    #pragma comment(lib, "opencv_ml247.lib")
    //#pragma comment(lib, "opencv_stitching247.lib");
    //#pragma comment(lib, "opencv_nonfree247.lib");
    #pragma comment(lib, "opencv_video247d.lib")
    #endif

    using namespace cv;
    using namespace std;



    int main()
    {

    //global variables
    Mat frame; //current frame
    Mat resizeF;
    Mat fgMaskMOG; //fg mask generated by MOG method
    Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
    Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method


    Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
    Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
    Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor



    pMOG = new BackgroundSubtractorMOG();
    pMOG2 = new BackgroundSubtractorMOG2();
    pGMG = new BackgroundSubtractorGMG();


    char fileName[100] = "C:\\POSCO\\video\\/cctv 2.mov"; //Gate1_175_p1.avi"; //mm2.avi"; //";//_p1.avi";
    VideoCapture stream1(fileName); //0 is the id of video device.0 if you have only one camera

    Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );


    //for check processing time
    unsigned long AAtime=0, BBtime=0;
    float TakeTime;
    int Frame=0;

    //unconditional loop
    while (true) {
    //for check processing time
    AAtime = getTickCount();

    Mat cameraFrame;
    if(!(stream1.read(frame))) //get one frame form video
    break;

    resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) );
    pMOG->operator()(resizeF, fgMaskMOG);
    pMOG2->operator()(resizeF, fgMaskMOG2);
    pGMG->operator()(resizeF, fgMaskGMG);
    //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element);

    //for check processing time
    BBtime = getTickCount();
    TakeTime = (BBtime - AAtime)/getTickFrequency();
    //save ms i file name and close
    FILE *fp;
    char str[100];
    sprintf_s(str,"%d.txt", Frame);
    fopen_s(&fp, str, "w");
    fprintf(fp, "%d frame takes %lf ms, %lf fps \n",Frame, TakeTime, 1/TakeTime);
    fclose(fp);
    Frame++;



    imshow("Origin", resizeF);
    imshow("MOG", fgMaskMOG);
    imshow("MOG2", fgMaskMOG2);
    imshow("GMG", fgMaskGMG);


    if (waitKey(30) >= 0)
    break;
    }

    }


    thank you.

    ReplyDelete
  16. thank you so much brother its working properly but its saving frames in seperate text files each frame seperately is there any way to get frames in one file

    ReplyDelete
  17. brother kindly share the description of functions in your code it will be helpfull for my research

    ReplyDelete
  18. Anonymous9/2/16 11:56

    Hi bro i am getting these errors in VS
    Error 1 error C2065: 'BackgroundSubtractorGMG' : undeclared identifier

    Error 2 error C2923: 'cv::Ptr' : 'BackgroundSubtractorGMG' is not a valid template type argument for parameter 'T'

    ReplyDelete
    Replies
    1. Check header file and lib version.
      Example version is 247, other version like 3.x will be change class location and method to usage.
      Thank you.

      Delete
  19. Anonymous16/2/16 19:41

    How to make the foreground appear as the original frame pixels, which mean in color?

    ReplyDelete
  20. Anonymous11/3/16 10:31

    I need a source code for MOG background subtraction using openCV with C and not c++.

    ReplyDelete
  21. Anonymous11/3/16 10:33

    I need a source code for MOG background subtraction using openCV with C and not c++.

    ReplyDelete
    Replies
    1. Sorry, I don't know what different c and c++
      Thank you.

      Delete
  22. Hey bro, how to Get background model from BackgroundSubtractorMOG2 in c++
    Help mee, please !! thank you so much !!

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. Hi,
    i am getting error "BackgroundSubtractorGMG" is undefined
    Error: class "cv::backgroundSubstractor" ha no member ""operator().

    Kindly give me sme suggestion

    ReplyDelete
  26. I get "BackgroundSubtractorGMG was not declared in this scope as error message, could you please help

    ReplyDelete
  27. I get the error 72:13: error: expected type-specifier before ‘BackgroundSubtractorMOG’
    pMOG = new BackgroundSubtractorMOG();

    ReplyDelete
  28. Refer this link - http://stackoverflow.com/questions/29429863/why-is-createbackgroundsubtractorgmg-considered-obsolete-and-how-can-i-use-it

    ReplyDelete
  29. how to execute this program in opencv..is this a c++ program ..suggest commands to compile and execute this program in open cv

    ReplyDelete
  30. thnx....for this useful information

    ReplyDelete
  31. hey background_segm.hpp is in-build header file in opencv?
    if not than tell me algorithm of that file...

    ReplyDelete
  32. Ptr<> - what is it and how can i use it?

    ReplyDelete