Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MixtureOfGaussians.hh
1 #ifndef _MIXTURE_OF_GAUSSIANS_HH_
2 #define _MIXTURE_OF_GAUSSIANS_HH_
3 
4 /*
5 This file is part of the BIAS library (Basic ImageAlgorithmS).
6 
7 Copyright (C) 2003-2009 (see file CONTACT for details)
8 Multimediale Systeme der Informationsverarbeitung
9 Institut fuer Informatik
10 Christian-Albrechts-Universitaet Kiel
11 
12 
13 BIAS is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17 
18 BIAS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public License
24 along with BIAS; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27 
28 #include "FeatureDetector/BlobDetectorBFS.hh"
29 #include <Base/Common/BIASpragmaStart.hh>
30 #include <Base/Common/W32Compat.hh>
31 #include <math.h>
32 #include <vector>
33 #include <Base/Image/Image.hh>
34 #include <Filter/Gauss.hh>
35 #ifdef BIAS_HAVE_PTHREADS
36  #include <pthread.h>
37 #endif
38 
39 namespace BIAS {
40 
42  MOG_RGB=0, //RGB
43  MOG_RGBD, //RGB with Depth
44  MOG_NRGB, //normalized RGB
45  MOG_NRGBD, //normalized RGB with Depth
46  MOG_HS, //first 2 channels of HSL (Hue and Saturation)
47  MOG_HSD //first 2 channels of HSL with Depth
48  };
49 
50  /** @class MixtureOfGaussians
51  @ingroup g_image
52  @brief class for detecting changes in image sequences
53 
54  Automatically find and visualize changes in image sequences with a
55  mixture of gaussians approach.
56 
57  After initialization of the algorithm it is possible to detect moving
58  objects in a static scene,
59  where the static scene is described as a background image.
60  Detection is done by calculating the expectancy value of each pixel
61  in the next frame given more than one gaussian distribution
62  for each pixel. If the expectancy is not matched, a change is detected.
63 
64  This class has been tested on RGB images but should work on
65  other color formats and grey value
66  images. One and three color channels supported.
67 
68  see also ExampleMixtureOfGaussiansFrame.cpp
69  @author fkellner */
70  template <class StorageType>
71  class BIASImage_EXPORT MixtureOfGaussians {
72  public:
73  /** @brief constructor
74  @param numDistributions number of gaussian distributions for every pixel.
75  @param sizeWindow window size for calculation of covariance
76  @param updateFactor expectancy values get updated weighted with this factor.
77  @param normalizeRGB. normalize RGB input images to rgb. */
78  MixtureOfGaussians(const unsigned int numDistributions,
79  const unsigned int sizeWindow=3,
80  const float updateFactor=0.1,
81  const MoGColorModel colorModel=MOG_NRGB);
82  /// @brief destructor
84 
85  /** @brief init weights for background/object distinction
86  @param maxWeight counter for how many times a pixel is matched can
87  not exceed this maximum. this is decreased if a pixel is not matched.
88  @param minWeight a pixel matched minWeight times is considered background
89  @param errorMargin a pixel is matched if:
90  pixelValue < errorMargin*errorMargin*expected value */
91  int InitWeights(const int maxWeight,
92  const int minWeight,
93  const float errorMargin);
94 
95  void SetSmoothing(const bool smooth);
96 
97  /**
98  * \brief set the used color model
99  * note that the images have to be given in the correct model
100  * no conversion will take place.
101  */
102  void SetColorModel(MoGColorModel model);
103 
104  /** @brief update calculations with new image
105  * the image has to be formatted correctly, e.g for MoG on HSL
106  * images give only the first 2 channels in the image.
107  * for MOG on HSLD give a three channel image with HSD
108  * @param in new image */
109  int Apply(BIAS::Image<StorageType>& in);
110 
111  /** @brief get normalized rgb image
112  @param normalized normalized rgb image (or unchanged grey image) */
113  int GetNormalizedImage(BIAS::Image<float> &normalized);
114 
115  /** @brief get image with current weights and distributions */
116  int GetWeightImage(BIAS::Image<float> &weights);
117  /** @brief get image with current max weights, only interesting for visualisation */
118  int GetMaxWeightImage(BIAS::Image<float> &weight);
119 
120  int GetMatchValueImage(BIAS::Image<float> &match);
121 
122  /** @brief get image of detected scene changes.
123  @param diff scene changes encoded 0 for no change, 255 for change (grey values) */
124  int GetDifferenceImage(BIAS::Image<unsigned char> &diff);
125 
126  /** @brief get image of detected scene changes with visualisations of detection
127  @param guiImage RGB Image with detected changes (white),
128  bounding boxes (green), centers of mass (red), largest box (yellow) */
129  int GetDifferenceImageWithVisuals(BIAS::Image<unsigned char> &guiImage);
130 
131  /** @brief get vector of corners for each bounding box of detected changes */
132  int GetBoundingBoxes(std::vector<BIAS::BIASBlob> &corners);
133 
134  /** @brief get largest bounding box of detected changes */
135  int GetLargestBoundingBox(BIAS::BIASBlob &corners);
136 
137  /** @brief get vector of images containing current guesses (distributions) */
138  int GetCurrentDistributions(std::vector<Image<float> > &distributionImages);
139 
140  void SetUpdateFactor(float factor = 0.1f) {
141  fUpdateFactor_ = factor;
142  }
143 
145  bAllowNewDistributionCreation_ = allow;
146  }
147 
148  void EnableCleanup( bool noise=false, bool holes=false, bool erosion=false) {
149  bCleanUp_ = noise;
150  bInverseCleanup_ = holes;
151  bErosion_ = erosion;
152  }
153 
154  protected:
155  int CreateNormalizedImage_(const BIAS::Image<StorageType> &in,
156  BIAS::Image<float> &out);
157  int Init_();
158  int InitOnePixel_(unsigned int x,unsigned int y,unsigned int c,unsigned int distrib);
159  int Update_();
160 
173  std::vector<BIAS::BIASBlob> corners_;
176 
177  unsigned int width_, height_, channels_, workChannels_;
178  unsigned int roiULx_, roiULy_, roiLRx_, roiLRy_;
179  unsigned int dDistributions_, dFrames_, dWindow_, dCurDistributions_, dFrameCounter_;
180  unsigned int dMaxWeight_, dMinWeight_;
181  float fUpdateFactor_, fError_;
182  bool bInited_, bSmooth_, bAllowNewDistributionCreation_;
184  bool bCleanUp_, bInverseCleanup_, bErosion_;
185  float maxDepth_;
186 #ifdef BIAS_HAVE_PTHREADS
187  pthread_mutex_t* guardimagemutex;
188 #endif
189  }; //end class
190 
191 }//end namespace
192 #include <Base/Common/BIASpragmaEnd.hh>
193 
194 
195 #endif
void SetUpdateFactor(float factor=0.1f)
BIAS::Image< float > weight_
BIAS::Image< float > matchValueImage_
BIAS::Gauss< unsigned char, unsigned char > smoothResultFilter_
BIAS::Image< float > workImage_
BIAS::Image< float > cov_
BIAS::BlobDetectorBFS< unsigned char > blobDetector_
BIAS::Image< unsigned char > differenceSmooth_
BIAS::Image< unsigned char > differenceSave_
class for detecting changes in image sequences
BIAS::Image< unsigned char > difference_
BIAS::Image< unsigned char > marker
Helper class to store blob corners.
pthread_mutex_t * guardimagemutex
BIAS::Image< unsigned char > differenceVisu_
void AllowCreationOfNewDistributions(bool allow)
The image template class for specific storage types.
Definition: Image.hh:78
void EnableCleanup(bool noise=false, bool holes=false, bool erosion=false)
BIAS::Image< float > maxWeight_
BIAS::Image< float > guess_
std::vector< BIAS::BIASBlob > corners_