bgfg_segm.cpp 2.75 KB
Newer Older
Jin Ma's avatar
Jin Ma committed
1 2 3
#include <iostream>
#include <string>

4 5 6 7
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/ocl.hpp"
#include "opencv2/highgui.hpp"
Jin Ma's avatar
Jin Ma committed
8 9 10 11 12 13 14 15 16 17 18

using namespace std;
using namespace cv;
using namespace cv::ocl;

#define M_MOG  1
#define M_MOG2 2

int main(int argc, const char** argv)
{
    cv::CommandLineParser cmd(argc, argv,
19 20 21 22
        "{ c camera | false       | use camera }"
        "{ f file   | 768x576.avi | input video file }"
        "{ m method | mog         | method (mog, mog2) }"
        "{ h help   | false       | print help message }");
Jin Ma's avatar
Jin Ma committed
23

24
    if (cmd.has("help"))
Jin Ma's avatar
Jin Ma committed
25 26
    {
        cout << "Usage : bgfg_segm [options]" << endl;
27
        cout << "Available options:" << endl;
28
        cmd.printMessage();
29
        return EXIT_SUCCESS;
Jin Ma's avatar
Jin Ma committed
30 31 32 33 34 35 36 37 38
    }

    bool useCamera = cmd.get<bool>("camera");
    string file = cmd.get<string>("file");
    string method = cmd.get<string>("method");

    if (method != "mog" && method != "mog2")
    {
        cerr << "Incorrect method" << endl;
39
        return EXIT_FAILURE;
Jin Ma's avatar
Jin Ma committed
40 41 42 43 44 45 46 47 48 49 50 51
    }

    int m = method == "mog" ? M_MOG : M_MOG2;

    VideoCapture cap;
    if (useCamera)
        cap.open(0);
    else
        cap.open(file);

    if (!cap.isOpened())
    {
52 53
        cout << "can not open camera or video file" << endl;
        return EXIT_FAILURE;
Jin Ma's avatar
Jin Ma committed
54 55 56 57 58 59 60 61 62 63
    }

    Mat frame;
    cap >> frame;

    oclMat d_frame(frame);

    cv::ocl::MOG mog;
    cv::ocl::MOG2 mog2;

64
    oclMat d_fgmask, d_fgimg, d_bgimg;
Jin Ma's avatar
Jin Ma committed
65 66 67

    d_fgimg.create(d_frame.size(), d_frame.type());

68
    Mat fgmask, fgimg, bgimg;
Jin Ma's avatar
Jin Ma committed
69 70 71 72 73 74 75 76 77 78 79 80

    switch (m)
    {
    case M_MOG:
        mog(d_frame, d_fgmask, 0.01f);
        break;

    case M_MOG2:
        mog2(d_frame, d_fgmask);
        break;
    }

81
    for (;;)
Jin Ma's avatar
Jin Ma committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    {
        cap >> frame;
        if (frame.empty())
            break;
        d_frame.upload(frame);

        int64 start = cv::getTickCount();

        //update the model
        switch (m)
        {
        case M_MOG:
            mog(d_frame, d_fgmask, 0.01f);
            mog.getBackgroundImage(d_bgimg);
            break;

        case M_MOG2:
            mog2(d_frame, d_fgmask);
            mog2.getBackgroundImage(d_bgimg);
            break;
        }

        double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
        std::cout << "FPS : " << fps << std::endl;

        d_fgimg.setTo(Scalar::all(0));
        d_frame.copyTo(d_fgimg, d_fgmask);

        d_fgmask.download(fgmask);
        d_fgimg.download(fgimg);
        if (!d_bgimg.empty())
            d_bgimg.download(bgimg);

        imshow("image", frame);
        imshow("foreground mask", fgmask);
        imshow("foreground image", fgimg);
        if (!bgimg.empty())
            imshow("mean background image", bgimg);

121
        if (27 == waitKey(30))
Jin Ma's avatar
Jin Ma committed
122 123 124
            break;
    }

125
    return EXIT_SUCCESS;
Jin Ma's avatar
Jin Ma committed
126
}