Subversion Repositories OpenCV2-Cookbook

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 10 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
6
   This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
18
#include <iostream>
19
#include <opencv2/core/core.hpp>
20
#include <opencv2/imgproc/imgproc.hpp>
21
#include <opencv2/highgui/highgui.hpp>
22
#include <opencv2/video/background_segm.hpp>
23
 
24
#include "videoprocessor.h"
25
#include "BGFGSegmentor.h"
26
 
27
int main()
28
{
29
        // Open the video file
30
    cv::VideoCapture capture("../bike.avi");
31
        // check if video successfully opened
32
        if (!capture.isOpened())
33
                return 0;
34
 
35
        // current video frame
36
        cv::Mat frame;
37
        // foreground binary image
38
        cv::Mat foreground;
39
 
40
        cv::namedWindow("Extracted Foreground");
41
 
42
        // The Mixture of Gaussian object
43
        // used with all default parameters
44
        cv::BackgroundSubtractorMOG mog;
45
 
46
        bool stop(false);
47
        // for all frames in video
48
        while (!stop) {
49
 
50
                // read next frame if any
51
                if (!capture.read(frame))
52
                        break;
53
 
54
                // update the background
55
                // and return the foreground
56
                mog(frame,foreground,0.01);
57
 
58
                // Complement the image
59
                cv::threshold(foreground,foreground,128,255,cv::THRESH_BINARY_INV);
60
 
61
                // show foreground
62
                cv::imshow("Extracted Foreground",foreground);
63
 
64
                // introduce a delay
65
                // or press key to stop
66
                if (cv::waitKey(10)>=0)
67
                                stop= true;
68
        }
69
 
70
        cv::waitKey();
71
 
72
        // Create video procesor instance
73
        VideoProcessor processor;
74
 
75
        // Create background/foreground segmentor 
76
        BGFGSegmentor segmentor;
77
        segmentor.setThreshold(25);
78
 
79
        // Open video file
80
        processor.setInput("../bike.avi");
81
 
82
        // set frame processor
83
        processor.setFrameProcessor(&segmentor);
84
 
85
        // Declare a window to display the video
86
        processor.displayOutput("Extracted Foreground");
87
 
88
        // Play the video at the original frame rate
89
        processor.setDelay(1000./processor.getFrameRate());
90
 
91
        // Start the process
92
        processor.run();
93
 
94
        cv::waitKey();
95
}