libvibe++ : a generic C++ library for the ViBe algorithm
ViBeBase.cpp
Go to the documentation of this file.
1 /* Copyright - Benjamin Laugraud <blaugraud@ulg.ac.be> - 2016
2  * Copyright - Marc Van Droogenbroeck <m.vandroogenbroeck@ulg.ac.be> - 2016
3  *
4  * ViBe is covered by a patent (see http://www.telecom.ulg.ac.be/research/vibe).
5  *
6  * Permission to use ViBe without payment of fee is granted for nonprofit
7  * educational and research purposes only.
8  *
9  * This work may not be copied or reproduced in whole or in part for any
10  * purpose.
11  *
12  * Copying, reproduction, or republishing for any purpose shall require a
13  * license. Please contact the authors in such cases. All the code is provided
14  * without any guarantee.
15  */
16 
17 /**
18  @file ViBeBase.cpp
19  @brief Constructor, seters and geters
20 
21  @author Benjamin Laungraud and Marc Van Droogenbroeck
22 */
23 
24 #include <algorithm>
25 #include <cstddef>
26 
27 #include "ViBeBase.h"
28 
29 using namespace std;
30 using namespace ViBe;
31 
32 namespace ViBe {
33  inline ostream& operator<<(ostream& os, const ViBeBase& vibe) {
34  vibe.print(os);
35  return os;
36  }
37 }
38 
39 /* ========================================================================== *
40  * ViBeBase *
41  * ========================================================================== */
42 
43 // TODO Replace rand()
44 // TODO Add noise using generator?
45 ViBeBase::ViBeBase(
46  int32_t height,
47  int32_t width,
48  int32_t channels,
49  const uint8_t* buffer
50 ) :
51  height(height),
52  width(width),
53  numberOfSamples(DEFAULT_NUMBER_OF_SAMPLES),
54  matchingThreshold(DEFAULT_MATCHING_THRESHOLD),
55  matchingNumber(DEFAULT_MATCHING_NUMBER),
56  updateFactor(DEFAULT_UPDATE_FACTOR),
57  stride(width * channels),
58  pixels(height * width),
59  numValues(height * width * channels),
60  historyImage(NULL),
61  historyBuffer(NULL),
62  lastHistoryImageSwapped(),
63  jump(NULL),
64  neighbor(NULL),
65  position(NULL) {
66 
67  if (height <= 0)
68  throw; // TODO Exception
69 
70  if (width <= 0)
71  throw; // TODO Exception
72 
73  if (channels <= 0)
74  throw; // TODO Exception
75 
76  if (buffer == NULL)
77  throw; // TODO Exception
78 
79  const uint32_t COLUMNS = width * channels;
80 
81  /* Creates the historyImage structure. */
82  historyImage = new uint8_t[NUMBER_OF_HISTORY_IMAGES * COLUMNS * height];
83 
84  for (uint32_t i = 0; i < NUMBER_OF_HISTORY_IMAGES; ++i) {
85  for (int32_t index = COLUMNS * height - 1; index >= 0; --index)
86  historyImage[i * COLUMNS * height + index] = buffer[index];
87  }
88 
89  /* Now creates and fills the history buffer. */
90  historyBuffer =
91  new uint8_t[COLUMNS * height * (numberOfSamples - NUMBER_OF_HISTORY_IMAGES)];
92 
93  for (int32_t index = COLUMNS * height - 1; index >= 0; --index) {
94  uint8_t value = buffer[index];
95 
96  for (uint32_t x = 0; x < numberOfSamples - NUMBER_OF_HISTORY_IMAGES; ++x) {
97  historyBuffer[index * (numberOfSamples - NUMBER_OF_HISTORY_IMAGES) + x] =
98  min(
99  max(
100  static_cast<int32_t>(value) + rand() % 20 - 10, // Add noise.
101  static_cast<int32_t>(BACKGROUND)
102  ),
103  static_cast<int32_t>(FOREGROUND)
104  );
105  }
106  }
107 
108  /* Fills the buffers with random values. */
109  int32_t size = (width > height) ? 2 * width + 1 : 2 * height + 1;
110 
111  jump = new uint32_t[size];
112  neighbor = new int32_t[size];
113  position = new uint32_t[size];
114 
115  for (int32_t i = 0; i < size; ++i) {
116  /* Values between 1 and 2 * updateFactor. */
117  jump[i] = (rand() % (2 * updateFactor)) + 1;
118  /* Values between { -width - 1, ... , width + 1 }. */
119  neighbor[i] = ((rand() % 3) - 1) + ((rand() % 3) - 1) * width;
120  /* Values between 0 and numberOfSamples - 1. */
121  position[i] = rand() % numberOfSamples;
122  }
123 }
124 
125 /******************************************************************************/
126 
127 ViBeBase::~ViBeBase() {
128  delete[] historyImage;
129  delete[] historyBuffer;
130  delete[] jump;
131  delete[] neighbor;
132  delete[] position;
133 }
134 
135 /******************************************************************************/
136 
137 uint32_t ViBeBase::getNumberOfSamples() const {
138  return numberOfSamples;
139 }
140 
141 /******************************************************************************/
142 
143 uint32_t ViBeBase::getMatchingThreshold() const {
144  return matchingThreshold;
145 }
146 
147 /******************************************************************************/
148 
149 void ViBeBase::setMatchingThreshold(int32_t matchingThreshold) {
150  if (matchingThreshold <= 0)
151  throw; // TODO Exception;
152 
153  this->matchingThreshold = matchingThreshold;
154 }
155 
156 /******************************************************************************/
157 
158 uint32_t ViBeBase::getMatchingNumber() const {
159  return matchingNumber;
160 }
161 
162 /******************************************************************************/
163 
164 void ViBeBase::setMatchingNumber(int32_t matchingNumber) {
165  if (matchingNumber <= 0)
166  throw; // TODO Exception;
167 
168  this->matchingNumber = matchingNumber;
169 }
170 
171 /******************************************************************************/
172 
173 uint32_t ViBeBase::getUpdateFactor() const {
174  return updateFactor;
175 }
176 
177 /******************************************************************************/
178 
179 void ViBeBase::setUpdateFactor(int32_t updateFactor) {
180  if (updateFactor <= 0)
181  throw; // TODO Exception;
182 
183  this->updateFactor = updateFactor;
184 
185  /* We also need to change the values of the jump buffer ! */
186  int32_t size = 2 * max(width, height) + 1;
187 
188  for (int32_t i = 0; i < size; ++i) {
189  // 1 or values between 1 and 2 * updateFactor.
190  jump[i] = (updateFactor == 1) ? 1 : (rand() % (2 * updateFactor)) + 1;
191  }
192 }
193 
194 /******************************************************************************/
195 
196 void ViBeBase::print(ostream& os) const {
197  os << " - Number of samples per pixel : " << numberOfSamples << endl;
198  os << " - Number of matches needed : " << matchingNumber << endl;
199  os << " - Matching threshold : " << matchingThreshold << endl;
200  os << " - Model update subsampling factor: " << updateFactor ;
201 }
Interface to the ViBe class.

License/Copyright

This code is copyrighted by the University of Liège, Belgium. 
It is only shared for research purposes. Please do not distribute it. 
B. Laugraud and M. Van Droogenbroeck, May 2016.