source: level2/competencies/MotionDirectionDetection/src/BackgroundSubtr.cpp @ 322

Revision 322, 2.9 KB checked in by ginevra, 10 years ago (diff)
Line 
1// AUTHOR: Ginevra Castellano
2// Queen Mary University of London
3// DATE: 03/2010
4// VERSION: 1.0
5
6// Copyright (C) 2009 Ginevra Castellano
7// Queen Mary University of London
8
9// This file is part of the MotionDirectionDetection program
10
11// MotionDirectionDetection is free software: you can redistribute it and/or modify
12// it under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19// GNU Lesser General Public License for more details.
20
21// You should have received a copy of the GNU Lesser General Public License
22// along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24// MotionDirectionDetection uses the OpenCV library
25// Copyright (C) 2000-2006, Intel Corporation, all rights reserved.
26// Third party copyrights are property of their respective owners.
27// See OpenCV_license.txt, in the program folder, for details.
28
29
30#include "cv.h"
31#include "highgui.h"
32#include "BackgroundSubtr.h"
33#include <stdio.h>
34
35
36BackgroundSubtr::BackgroundSubtr(void)
37{
38        this->storage = NULL;
39
40        this->backgrImgGray = NULL;
41        this->srcGray = NULL;
42        this->frameForeground = NULL;
43        this->ForegroundEroded = NULL;
44}
45
46BackgroundSubtr::~BackgroundSubtr(void)
47{
48        if(this->storage) cvReleaseMemStorage(&this->storage);
49}
50
51
52IplImage* BackgroundSubtr::extractBackground(IplImage *src, IplImage *backgrImg)
53
54{       
55        if(!this->storage)
56        this->storage = cvCreateMemStorage(0);
57    else
58        cvClearMemStorage(this->storage);
59
60        if(!backgrImgGray)
61       backgrImgGray = cvCreateImage(cvSize(src->width,src->height),
62                                            IPL_DEPTH_8U, 1);
63        if(!srcGray)
64       srcGray = cvCreateImage(cvSize(src->width,src->height),
65                                            IPL_DEPTH_8U, 1);
66        if(!frameForeground)
67       frameForeground = cvCreateImage(cvSize(src->width,src->height),
68                                            IPL_DEPTH_8U, 1);
69        if(!ForegroundEroded)
70       ForegroundEroded = cvCreateImage(cvSize(src->width,src->height),
71                                            IPL_DEPTH_8U, 1);
72
73        cvCvtColor(backgrImg, backgrImgGray, CV_RGB2GRAY);  // convert background image to grayscale
74        cvCvtColor(src, srcGray, CV_RGB2GRAY); // convert current frame to grayscale
75        cvAbsDiff(backgrImgGray, srcGray, frameForeground);  // subtract background grayscale image from grayscale current frame
76        cvThreshold(frameForeground, frameForeground, 50, 255, CV_THRESH_BINARY); // threshold to get a black and white image
77        cvErode(frameForeground, frameForeground, NULL, 1); // eliminate noise
78
79        return frameForeground;
80}
81
Note: See TracBrowser for help on using the repository browser.