-
Notifications
You must be signed in to change notification settings - Fork 91
/
FrameFilter.cpp
368 lines (312 loc) · 11.6 KB
/
FrameFilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/***********************************************************************
FrameFilter - Class to filter streams of depth frames arriving from a
depth camera, with code to detect unstable values in each pixel, and
fill holes resulting from invalid samples.
Copyright (c) 2012-2016 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "FrameFilter.h"
#include <Misc/FunctionCalls.h>
#include <Geometry/HVector.h>
#include <Geometry/Matrix.h>
/****************************
Methods of class FrameFilter:
****************************/
void* FrameFilter::filterThreadMethod(void)
{
unsigned int lastInputFrameVersion=0;
while(true)
{
Kinect::FrameBuffer frame;
{
Threads::MutexCond::Lock inputLock(inputCond);
/* Wait until a new frame arrives or the program shuts down: */
while(runFilterThread&&lastInputFrameVersion==inputFrameVersion)
inputCond.wait(inputLock);
/* Bail out if the program is shutting down: */
if(!runFilterThread)
break;
/* Work on the new frame: */
frame=inputFrame;
lastInputFrameVersion=inputFrameVersion;
}
/* Prepare a new output frame: */
Kinect::FrameBuffer& newOutputFrame=outputFrames.startNewValue();
/* Enter the new frame into the averaging buffer and calculate the output frame's pixel values: */
const RawDepth* ifPtr=inputFrame.getData<RawDepth>();
RawDepth* abPtr=averagingBuffer+averagingSlotIndex*size[1]*size[0];
unsigned int* sPtr=statBuffer;
float* ofPtr=validBuffer;
float* nofPtr=newOutputFrame.getData<float>();
const PixelDepthCorrection* pdcPtr=pixelDepthCorrection;
for(unsigned int y=0;y<size[1];++y)
{
float py=float(y)+0.5f;
for(unsigned int x=0;x<size[0];++x,++ifPtr,++pdcPtr,++abPtr,sPtr+=3,++ofPtr,++nofPtr)
{
float px=float(x)+0.5f;
unsigned int oldVal=*abPtr;
unsigned int newVal=*ifPtr;
/* Depth-correct the new value: */
float newCVal=pdcPtr->correct(newVal);
/* Plug the depth-corrected new value into the minimum and maximum plane equations to determine its validity: */
float minD=minPlane[0]*px+minPlane[1]*py+minPlane[2]*newCVal+minPlane[3];
float maxD=maxPlane[0]*px+maxPlane[1]*py+maxPlane[2]*newCVal+maxPlane[3];
if(minD>=0.0f&&maxD<=0.0f)
{
/* Store the new input value: */
*abPtr=newVal;
/* Update the pixel's statistics: */
++sPtr[0]; // Number of valid samples
sPtr[1]+=newVal; // Sum of valid samples
sPtr[2]+=newVal*newVal; // Sum of squares of valid samples
/* Check if the previous value in the averaging buffer was valid: */
if(oldVal!=2048U)
{
--sPtr[0]; // Number of valid samples
sPtr[1]-=oldVal; // Sum of valid samples
sPtr[2]-=oldVal*oldVal; // Sum of squares of valid samples
}
}
else if(!retainValids)
{
/* Store an invalid input value: */
*abPtr=2048U;
/* Check if the previous value in the averaging buffer was valid: */
if(oldVal!=2048U)
{
--sPtr[0]; // Number of valid samples
sPtr[1]-=oldVal; // Sum of valid samples
sPtr[2]-=oldVal*oldVal; // Sum of squares of valid samples
}
}
/* Check if the pixel is considered "stable": */
if(sPtr[0]>=minNumSamples&&sPtr[2]*sPtr[0]<=maxVariance*sPtr[0]*sPtr[0]+sPtr[1]*sPtr[1])
{
/* Check if the new depth-corrected running mean is outside the previous value's envelope: */
float newFiltered=pdcPtr->correct(float(sPtr[1])/float(sPtr[0]));
if(Math::abs(newFiltered-*ofPtr)>=hysteresis)
{
/* Set the output pixel value to the depth-corrected running mean: */
*nofPtr=*ofPtr=newFiltered;
}
else
{
/* Leave the pixel at its previous value: */
*nofPtr=*ofPtr;
}
}
else if(retainValids)
{
/* Leave the pixel at its previous value: */
*nofPtr=*ofPtr;
}
else
{
/* Assign default value to instable pixels: */
*nofPtr=instableValue;
}
}
}
/* Go to the next averaging slot: */
if(++averagingSlotIndex==numAveragingSlots)
averagingSlotIndex=0U;
/* Apply a spatial filter if requested: */
if(spatialFilter)
{
for(int filterPass=0;filterPass<2;++filterPass)
{
/* Low-pass filter the entire output frame in-place: */
for(unsigned int x=0;x<size[0];++x)
{
/* Get a pointer to the current column: */
float* colPtr=newOutputFrame.getData<float>()+x;
/* Filter the first pixel in the column: */
float lastVal=*colPtr;
*colPtr=(colPtr[0]*2.0f+colPtr[size[0]])/3.0f;
colPtr+=size[0];
/* Filter the interior pixels in the column: */
for(unsigned int y=1;y<size[1]-1;++y,colPtr+=size[0])
{
/* Filter the pixel: */
float nextLastVal=*colPtr;
*colPtr=(lastVal+colPtr[0]*2.0f+colPtr[size[0]])*0.25f;
lastVal=nextLastVal;
}
/* Filter the last pixel in the column: */
*colPtr=(lastVal+colPtr[0]*2.0f)/3.0f;
}
float* rowPtr=newOutputFrame.getData<float>();
for(unsigned int y=0;y<size[1];++y)
{
/* Filter the first pixel in the row: */
float lastVal=*rowPtr;
*rowPtr=(rowPtr[0]*2.0f+rowPtr[1])/3.0f;
++rowPtr;
/* Filter the interior pixels in the row: */
for(unsigned int x=1;x<size[0]-1;++x,++rowPtr)
{
/* Filter the pixel: */
float nextLastVal=*rowPtr;
*rowPtr=(lastVal+rowPtr[0]*2.0f+rowPtr[1])*0.25f;
lastVal=nextLastVal;
}
/* Filter the last pixel in the row: */
*rowPtr=(lastVal+rowPtr[0]*2.0f)/3.0f;
++rowPtr;
}
}
}
/* Finalize the new output frame in the output buffer: */
outputFrames.postNewValue();
/* Pass the new output frame to the registered receiver: */
if(outputFrameFunction!=0)
(*outputFrameFunction)(newOutputFrame);
}
return 0;
}
FrameFilter::FrameFilter(const unsigned int sSize[2],unsigned int sNumAveragingSlots,const FrameFilter::PixelDepthCorrection* sPixelDepthCorrection,const PTransform& depthProjection,const Plane& basePlane)
:pixelDepthCorrection(sPixelDepthCorrection),
averagingBuffer(0),
statBuffer(0),
outputFrameFunction(0)
{
/* Remember the frame size: */
for(int i=0;i<2;++i)
size[i]=sSize[i];
/* Initialize the input frame slot: */
inputFrameVersion=0;
/* Initialize the valid depth range: */
setValidDepthInterval(0U,2046U);
/* Initialize the averaging buffer: */
numAveragingSlots=sNumAveragingSlots;
averagingBuffer=new RawDepth[numAveragingSlots*size[1]*size[0]];
RawDepth* abPtr=averagingBuffer;
for(unsigned int i=0;i<numAveragingSlots;++i)
for(unsigned int y=0;y<size[1];++y)
for(unsigned int x=0;x<size[0];++x,++abPtr)
*abPtr=2048U; // Mark sample as invalid
averagingSlotIndex=0U;
/* Initialize the statistics buffer: */
statBuffer=new unsigned int[size[1]*size[0]*3];
unsigned int* sbPtr=statBuffer;
for(unsigned int y=0;y<size[1];++y)
for(unsigned int x=0;x<size[0];++x)
for(int i=0;i<3;++i,++sbPtr)
*sbPtr=0;
/* Initialize the stability criterion: */
minNumSamples=(numAveragingSlots+1)/2;
maxVariance=4;
hysteresis=0.1f;
retainValids=true;
instableValue=0.0;
/* Enable spatial filtering: */
spatialFilter=true;
/* Convert the base plane equation from camera space to depth-image space: */
PTransform::HVector basePlaneCc(basePlane.getNormal());
basePlaneCc[3]=-basePlane.getOffset();
PTransform::HVector basePlaneDic(depthProjection.getMatrix().transposeMultiply(basePlaneCc));
basePlaneDic/=Geometry::mag(basePlaneDic.toVector());
/* Initialize the valid buffer: */
validBuffer=new float[size[1]*size[0]];
float* vbPtr=validBuffer;
for(unsigned int y=0;y<size[1];++y)
for(unsigned int x=0;x<size[0];++x,++vbPtr)
*vbPtr=float(-((double(x)+0.5)*basePlaneDic[0]+(double(y)+0.5)*basePlaneDic[1]+basePlaneDic[3])/basePlaneDic[2]);
/* Initialize the output frame buffer: */
for(int i=0;i<3;++i)
outputFrames.getBuffer(i)=Kinect::FrameBuffer(size[0],size[1],size[1]*size[0]*sizeof(float));
/* Start the filtering thread: */
runFilterThread=true;
filterThread.start(this,&FrameFilter::filterThreadMethod);
}
FrameFilter::~FrameFilter(void)
{
/* Shut down the filtering thread: */
{
Threads::MutexCond::Lock inputLock(inputCond);
runFilterThread=false;
inputCond.signal();
}
filterThread.join();
/* Release all allocated buffers: */
delete[] averagingBuffer;
delete[] statBuffer;
delete[] validBuffer;
delete outputFrameFunction;
}
void FrameFilter::setValidDepthInterval(unsigned int newMinDepth,unsigned int newMaxDepth)
{
/* Set the equations for the minimum and maximum plane in depth image space: */
minPlane[0]=0.0f;
minPlane[1]=0.0f;
minPlane[2]=1.0f;
minPlane[3]=-float(newMinDepth)+0.5f;
maxPlane[0]=0.0f;
maxPlane[1]=0.0f;
maxPlane[2]=1.0f;
maxPlane[3]=-float(newMaxDepth)-0.5f;
}
void FrameFilter::setValidElevationInterval(const PTransform& depthProjection,const Plane& basePlane,double newMinElevation,double newMaxElevation)
{
/* Calculate the equations of the minimum and maximum elevation planes in camera space: */
PTransform::HVector minPlaneCc(basePlane.getNormal());
minPlaneCc[3]=-(basePlane.getOffset()+newMinElevation*basePlane.getNormal().mag());
PTransform::HVector maxPlaneCc(basePlane.getNormal());
maxPlaneCc[3]=-(basePlane.getOffset()+newMaxElevation*basePlane.getNormal().mag());
/* Transform the plane equations to depth image space and flip and swap the min and max planes because elevation increases opposite to raw depth: */
PTransform::HVector minPlaneDic(depthProjection.getMatrix().transposeMultiply(minPlaneCc));
double minPlaneScale=-1.0/Geometry::mag(minPlaneDic.toVector());
for(int i=0;i<4;++i)
maxPlane[i]=float(minPlaneDic[i]*minPlaneScale);
PTransform::HVector maxPlaneDic(depthProjection.getMatrix().transposeMultiply(maxPlaneCc));
double maxPlaneScale=-1.0/Geometry::mag(maxPlaneDic.toVector());
for(int i=0;i<4;++i)
minPlane[i]=float(maxPlaneDic[i]*maxPlaneScale);
}
void FrameFilter::setStableParameters(unsigned int newMinNumSamples,unsigned int newMaxVariance)
{
minNumSamples=newMinNumSamples;
maxVariance=newMaxVariance;
}
void FrameFilter::setHysteresis(float newHysteresis)
{
hysteresis=newHysteresis;
}
void FrameFilter::setRetainValids(bool newRetainValids)
{
retainValids=newRetainValids;
}
void FrameFilter::setInstableValue(float newInstableValue)
{
instableValue=newInstableValue;
}
void FrameFilter::setSpatialFilter(bool newSpatialFilter)
{
spatialFilter=newSpatialFilter;
}
void FrameFilter::setOutputFrameFunction(FrameFilter::OutputFrameFunction* newOutputFrameFunction)
{
delete outputFrameFunction;
outputFrameFunction=newOutputFrameFunction;
}
void FrameFilter::receiveRawFrame(const Kinect::FrameBuffer& newFrame)
{
Threads::MutexCond::Lock inputLock(inputCond);
/* Store the new buffer in the input buffer: */
inputFrame=newFrame;
++inputFrameVersion;
/* Signal the background thread: */
inputCond.signal();
}