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
#if !defined BGFGSeg
19
#define BGFGSeg
20
 
21
#include <opencv2/core/core.hpp>
22
#include <opencv2/highgui/highgui.hpp>
23
#include <opencv2/imgproc/imgproc.hpp>
24
 
25
#include "videoprocessor.h"
26
 
27
class BGFGSegmentor : public FrameProcessor {
28
 
29
        cv::Mat gray;                   // current gray-level image
30
        cv::Mat background;             // accumulated background
31
        cv::Mat backImage;              // background image
32
        cv::Mat foreground;             // foreground image
33
        double learningRate;    // learning rate in background accumulation
34
        int threshold;                  // threshold for foreground extraction
35
 
36
  public:
37
 
38
        BGFGSegmentor() : threshold(10), learningRate(0.01) {}
39
 
40
        // Set the threshold used to declare a foreground
41
        void setThreshold(int t) {
42
 
43
                threshold= t;
44
        }
45
 
46
        // Set the learning rate
47
        void setLearningRate(double r) {
48
 
49
                learningRate= r;
50
        }
51
 
52
        // processing method
53
        void process(cv:: Mat &frame, cv:: Mat &output) {
54
 
55
                // convert to gray-level image
56
                cv::cvtColor(frame, gray, CV_BGR2GRAY);
57
 
58
                // initialize background to 1st frame
59
                if (background.empty())
60
                        gray.convertTo(background, CV_32F);
61
 
62
                // convert background to 8U
63
                background.convertTo(backImage,CV_8U);
64
 
65
                // compute difference between current image and background
66
                cv::absdiff(backImage,gray,foreground);
67
 
68
                // apply threshold to foreground image
69
                cv::threshold(foreground,output,threshold,255,cv::THRESH_BINARY_INV);
70
 
71
                // accumulate background
72
                cv::accumulateWeighted(gray, background, learningRate, output);
73
        }
74
};
75
 
76
#endif