bgfg_segm.cpp 2.47 KB
Newer Older
1 2 3
#include "opencv2/core.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
4
#include "opencv2/video/background_segm.hpp"
5
#include "opencv2/highgui.hpp"
6
#include <stdio.h>
7

8
using namespace std;
9 10
using namespace cv;

11
static void help()
Gary Bradski's avatar
Gary Bradski committed
12
{
Gary Bradski's avatar
Gary Bradski committed
13
 printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
14 15
"Learns the background at the start and then segments.\n"
"Learning is togged by the space key. Will read from file or camera\n"
16 17
"Usage: \n"
"			./bgfg_segm [--camera]=<use camera, if this key is present>, [--file_name]=<path to movie file> \n\n");
Gary Bradski's avatar
Gary Bradski committed
18
}
19

20
const char* keys =
21
{
22 23
    "{c  camera   |         | use camera or not}"
    "{fn file_name|tree.avi | movie file        }"
24 25
};

26
//this is a sample for foreground detection functions
27
int main(int argc, const char** argv)
28
{
29
    help();
30

31
    CommandLineParser parser(argc, argv, keys);
32
    bool useCamera = parser.has("camera");
33
    string file = parser.get<string>("file_name");
34
    VideoCapture cap;
35 36
    bool update_bg_model = true;

37
    if( useCamera )
38
        cap.open(0);
39
    else
40
        cap.open(file.c_str());
41 42

    parser.printMessage();
43

44
    if( !cap.isOpened() )
45 46 47 48
    {
        printf("can not open camera or video file\n");
        return -1;
    }
49

50 51 52 53
    namedWindow("image", WINDOW_NORMAL);
    namedWindow("foreground mask", WINDOW_NORMAL);
    namedWindow("foreground image", WINDOW_NORMAL);
    namedWindow("mean background image", WINDOW_NORMAL);
54

55
    Ptr<BackgroundSubtractor> bg_model = createBackgroundSubtractorMOG2();
56

57 58
    Mat img, fgmask, fgimg;

59
    for(;;)
60
    {
61
        cap >> img;
62

63 64
        if( img.empty() )
            break;
65

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
66
        //cvtColor(_img, img, COLOR_BGR2GRAY);
67

68 69 70 71
        if( fgimg.empty() )
          fgimg.create(img.size(), img.type());

        //update the model
72
        bg_model->apply(img, fgmask, update_bg_model ? -1 : 0);
73 74 75 76 77

        fgimg = Scalar::all(0);
        img.copyTo(fgimg, fgmask);

        Mat bgimg;
78
        bg_model->getBackgroundImage(bgimg);
79

80 81
        imshow("image", img);
        imshow("foreground mask", fgmask);
82 83 84 85
        imshow("foreground image", fgimg);
        if(!bgimg.empty())
          imshow("mean background image", bgimg );

86
        char k = (char)waitKey(30);
87 88
        if( k == 27 ) break;
        if( k == ' ' )
Gary Bradski's avatar
Gary Bradski committed
89
        {
90
            update_bg_model = !update_bg_model;
Gary Bradski's avatar
Gary Bradski committed
91
            if(update_bg_model)
92
                printf("Background update is on\n");
Gary Bradski's avatar
Gary Bradski committed
93
            else
94
                printf("Background update is off\n");
Gary Bradski's avatar
Gary Bradski committed
95
        }
96 97 98 99
    }

    return 0;
}