bgfg_segm.cpp 2.95 KB
Newer Older
Konstantin Matskevich's avatar
Konstantin Matskevich committed
1 2 3 4 5 6
#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/core/utility.hpp"
7
#include "opencv2/videoio.hpp"
Konstantin Matskevich's avatar
Konstantin Matskevich committed
8 9 10 11 12 13 14
#include "opencv2/highgui.hpp"
#include "opencv2/video.hpp"

using namespace std;
using namespace cv;

#define M_MOG2 2
15
#define M_KNN  3
Konstantin Matskevich's avatar
Konstantin Matskevich committed
16 17 18

int main(int argc, const char** argv)
{
Konstantin Matskevich's avatar
Konstantin Matskevich committed
19
    CommandLineParser cmd(argc, argv,
20 21 22 23 24
        "{ c camera   |                    | use camera }"
        "{ f file     | ../data/vtest.avi  | input video file }"
        "{ t type     | mog2               | method's type (knn, mog2) }"
        "{ h help     |                    | print help message }"
        "{ m cpu_mode | false              | press 'm' to switch OpenCL<->CPU}");
Konstantin Matskevich's avatar
Konstantin Matskevich committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    if (cmd.has("help"))
    {
        cout << "Usage : bgfg_segm [options]" << endl;
        cout << "Available options:" << endl;
        cmd.printMessage();
        return EXIT_SUCCESS;
    }

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

    if (method != "mog" && method != "mog2")
    {
        cerr << "Incorrect method" << endl;
        return EXIT_FAILURE;
    }

44
    int m = method == "mog2" ? M_MOG2 : M_KNN;
Konstantin Matskevich's avatar
Konstantin Matskevich committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

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

    if (!cap.isOpened())
    {
        cout << "can not open camera or video file" << endl;
        return EXIT_FAILURE;
    }

    UMat frame, fgmask, fgimg;
    cap >> frame;
    fgimg.create(frame.size(), frame.type());

62
    Ptr<BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN();
Konstantin Matskevich's avatar
Konstantin Matskevich committed
63
    Ptr<BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
Konstantin Matskevich's avatar
Konstantin Matskevich committed
64 65 66

    switch (m)
    {
67 68
    case M_KNN:
        knn->apply(frame, fgmask);
Konstantin Matskevich's avatar
Konstantin Matskevich committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        break;

    case M_MOG2:
        mog2->apply(frame, fgmask);
        break;
    }
    bool running=true;
    for (;;)
    {
        if(!running)
            break;
        cap >> frame;
        if (frame.empty())
            break;

Konstantin Matskevich's avatar
Konstantin Matskevich committed
84
        int64 start = getTickCount();
Konstantin Matskevich's avatar
Konstantin Matskevich committed
85 86 87 88

        //update the model
        switch (m)
        {
89 90
        case M_KNN:
            knn->apply(frame, fgmask);
Konstantin Matskevich's avatar
Konstantin Matskevich committed
91 92 93 94 95 96 97
            break;

        case M_MOG2:
            mog2->apply(frame, fgmask);
            break;
        }

Konstantin Matskevich's avatar
Konstantin Matskevich committed
98
        double fps = getTickFrequency() / (getTickCount() - start);
Konstantin Matskevich's avatar
Konstantin Matskevich committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        std::cout << "FPS : " << fps << std::endl;
        std::cout << fgimg.size() << std::endl;
        fgimg.setTo(Scalar::all(0));
        frame.copyTo(fgimg, fgmask);

        imshow("image", frame);
        imshow("foreground mask", fgmask);
        imshow("foreground image", fgimg);

        char key = (char)waitKey(30);

        switch (key)
        {
        case 27:
            running = false;
            break;
        case 'm':
        case 'M':
Konstantin Matskevich's avatar
Konstantin Matskevich committed
117 118
            ocl::setUseOpenCL(!ocl::useOpenCL());
            cout << "Switched to " << (ocl::useOpenCL() ? "OpenCL enabled" : "CPU") << " mode\n";
Konstantin Matskevich's avatar
Konstantin Matskevich committed
119 120 121 122 123
            break;
        }
    }
    return EXIT_SUCCESS;
}