GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
MedianFilter.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #define GRT_DLL_EXPORTS
22 #include "MedianFilter.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Register the MedianFilter module with the PreProcessing base class
27 RegisterPreProcessingModule< MedianFilter > MedianFilter::registerModule("MedianFilter");
28 
29 MedianFilter::MedianFilter(UINT filterSize,UINT numDimensions){
30 
31  classType = "MedianFilter";
32  preProcessingType = classType;
33  debugLog.setProceedingText("[DEBUG MedianFilter]");
34  errorLog.setProceedingText("[ERROR MedianFilter]");
35  warningLog.setProceedingText("[WARNING MedianFilter]");
36  init(filterSize,numDimensions);
37 }
38 
40 
41  classType = "MedianFilter";
42  preProcessingType = classType;
43  debugLog.setProceedingText("[DEBUG MedianFilter]");
44  errorLog.setProceedingText("[ERROR MedianFilter]");
45  warningLog.setProceedingText("[WARNING MedianFilter]");
46 
47  //Zero this instance
48  this->filterSize = 0;
49  this->inputSampleCounter = 0;
50 
51  //Copy the settings from the rhs instance
52  *this = rhs;
53 }
54 
56 
57 }
58 
60  if(this!=&rhs){
61  //Clear this instance
62  this->filterSize = 0;
63  this->inputSampleCounter = 0;
64  this->dataBuffer.clear();
65 
66  //Copy from the rhs instance
67  if( rhs.initialized ){
68  this->init( rhs.filterSize, rhs.numInputDimensions );
69  this->dataBuffer = rhs.dataBuffer;
70  }
71 
72  //Copy the preprocessing base variables
74  }
75  return *this;
76 }
77 
78 bool MedianFilter::deepCopyFrom(const PreProcessing *preProcessing){
79 
80  if( preProcessing == NULL ) return false;
81 
82  if( this->getPreProcessingType() == preProcessing->getPreProcessingType() ){
83 
84  //Call the equals operator
85  *this = *(MedianFilter*)preProcessing;
86 
87  return true;
88  }
89 
90  errorLog << "clone(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
91 
92  return false;
93 }
94 
95 
96 bool MedianFilter::process(const VectorFloat &inputVector){
97 
98  if( !initialized ){
99  errorLog << "process(const VectorFloat &inputVector) - The filter has not been initialized!" << std::endl;
100  return false;
101  }
102 
103  if( inputVector.size() != numInputDimensions ){
104  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
105  return false;
106  }
107 
108  filter( inputVector );
109 
110  if( processedData.size() == numOutputDimensions ) return true;
111 
112  return false;
113 }
114 
116  if( initialized ) return init(filterSize,numInputDimensions);
117  return false;
118 }
119 
120 bool MedianFilter::save( std::fstream &file ) const{
121 
122  if( !file.is_open() ){
123  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
124  return false;
125  }
126 
127  file << "GRT_MEDIAN_FILTER_FILE_V1.0" << std::endl;
128 
129  file << "NumInputDimensions: " << numInputDimensions << std::endl;
130  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
131  file << "FilterSize: " << filterSize << std::endl;
132 
133  return true;
134 }
135 
136 bool MedianFilter::load( std::fstream &file ){
137 
138  if( !file.is_open() ){
139  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
140  return false;
141  }
142 
143  std::string word;
144 
145  //Load the header
146  file >> word;
147 
148  if( word != "GRT_MEDIAN_FILTER_FILE_V1.0" ){
149  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
150  return false;
151  }
152 
153  //Load the number of input dimensions
154  file >> word;
155  if( word != "NumInputDimensions:" ){
156  errorLog << "load(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
157  return false;
158  }
159  file >> numInputDimensions;
160 
161  //Load the number of output dimensions
162  file >> word;
163  if( word != "NumOutputDimensions:" ){
164  errorLog << "load(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
165  return false;
166  }
167  file >> numOutputDimensions;
168 
169  //Load the filter factor
170  file >> word;
171  if( word != "FilterSize:" ){
172  errorLog << "load(fstream &file) - Failed to read FilterSize header!" << std::endl;
173  return false;
174  }
175  file >> filterSize;
176 
177  //Init the filter module to ensure everything is initialized correctly
178  return init(filterSize,numInputDimensions);
179 }
180 
181 bool MedianFilter::init(UINT filterSize,UINT numDimensions){
182 
183  //Cleanup the old memory
184  initialized = false;
185  inputSampleCounter = 0;
186 
187  if( filterSize == 0 ){
188  errorLog << "init(UINT filterSize,UINT numDimensions) - Filter size can not be zero!" << std::endl;
189  return false;
190  }
191 
192  if( numDimensions == 0 ){
193  errorLog << "init(UINT filterSize,UINT numDimensions) - The number of dimensions must be greater than zero!" << std::endl;
194  return false;
195  }
196 
197  //Resize the filter
198  this->filterSize = filterSize;
199  this->numInputDimensions = numDimensions;
200  this->numOutputDimensions = numDimensions;
201  processedData.clear();
202  processedData.resize(numDimensions,0);
203  initialized = dataBuffer.resize( filterSize, VectorFloat(numInputDimensions,0) );
204 
205  if( !initialized ){
206  errorLog << "init(UINT filterSize,UINT numDimensions) - Failed to resize dataBuffer!" << std::endl;
207  }
208 
209  return initialized;
210 }
211 
212 Float MedianFilter::filter(const Float x){
213 
214  VectorFloat y = filter(VectorFloat(1,x));
215 
216  if( y.size() == 0 ) return 0;
217  return y[0];
218 }
219 
221 
222  //If the filter has not been initialised then return 0, otherwise filter x and return y
223  if( !initialized ){
224  errorLog << "filter(const VectorFloat &x) - The filter has not been initialized!" << std::endl;
225  return VectorFloat();
226  }
227 
228  if( x.getSize() != numInputDimensions ){
229  errorLog << "filter(const VectorFloat &x) - The size of the input Vector (" << x.getSize() << ") does not match that of the number of dimensions of the filter (" << numInputDimensions << ")!" << std::endl;
230  return VectorFloat();
231  }
232 
234 
235  //Add the new value to the buffer
236  dataBuffer.push_back( x );
237 
238  //Compute the median value for each dimension
240  for(unsigned int j=0; j<numInputDimensions; j++){
241  for(unsigned int i=0; i<inputSampleCounter; i++) {
242  tmp[i] = dataBuffer[i][j];
243  }
244  std::sort(tmp.begin(),tmp.end());
245 
246  //Get the median value
247  unsigned int medianIndex = (inputSampleCounter/2);
248  processedData[j] = tmp[ medianIndex ];
249  }
250 
251  return processedData;
252 }
253 
255 
256  if( !initialized ){
257  return Vector< VectorFloat >();
258  }
259 
260  Vector< VectorFloat > data(numInputDimensions,VectorFloat(inputSampleCounter));
261  for(unsigned int j=0; j<numInputDimensions; j++){
262  for(unsigned int i=0; i<inputSampleCounter; i++){
263  data[j][i] = dataBuffer[i][j];
264  }
265  }
266  return data;
267 }
268 
269 GRT_END_NAMESPACE
bool push_back(const T &value)
Vector< VectorFloat > getDataBuffer() const
virtual bool load(std::fstream &file)
MedianFilter & operator=(const MedianFilter &rhs)
virtual bool reset()
virtual bool process(const VectorFloat &inputVector)
Float filter(const Float x)
CircularBuffer< VectorFloat > dataBuffer
A buffer to store the previous N values, N = filterSize.
Definition: MedianFilter.h:171
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
UINT getSize() const
Definition: Vector.h:191
UINT filterSize
The size of the filter.
Definition: MedianFilter.h:169
The MedianFilter implements a simple median filter.
virtual ~MedianFilter()
UINT inputSampleCounter
A counter to keep track of the number of input samples.
Definition: MedianFilter.h:170
std::string getPreProcessingType() const
virtual bool save(std::fstream &file) const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
MedianFilter(UINT filterSize=5, UINT numDimensions=1)
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
bool resize(const unsigned int newBufferSize)