-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegralStructures.h
629 lines (508 loc) · 16.4 KB
/
IntegralStructures.h
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
Copyright (c) 2007-2010 ICG TU Graz
This file is part of CovSigmaPointsComp.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Contact: Stefan Kluckner, [email protected]
#ifndef _INTSTRUCTURE_H_
#define _INTSTRUCTURE_H_
#include <string.h>
#include <math.h>
// struct for rectangular region
struct myRect
{
public:
/** Rectangle Constructor */
myRect()
{
this->upper = -1;
this->left = -1;
this->height = -1;
this->width = -1;
}
~myRect()
{
}
/** Rectangle Constructor */
myRect( int upper, int left, int height, int width )
{
this->upper = upper;
this->left = left;
this->height = height;
this->width = width;
}
/** Defines the Upper Value */
int upper;
/** Defines the Left Value */
int left;
/** Defines the Height Value */
int height;
/** Defines the Width Value */
int width;
};
/**
* \class IntegralStructures
* \brief This class handles the Integral Structures
* \ingroup InputOutput
*/
template< typename INPUTDATATYPE, typename INTERNALDATATYPE >
class IntegralStructures
{
public:
/** \enum Integral Structures */
// Binary, Integer with 32 Bit
// 1: 00 ... 00001 Image
// 2: 00 ... 00010 Squared Image, Covariance Computation
enum IntegralImages
{
INT_IMAGE = 1, /**< Image Representation */
INT_SQIMAGE = 2, /**< Image Squared Representation */
};
/**
* Constructor
* \brief Default constructor
* \brief Format Input Image, row - wise
* \brief x1 .. layer 1, x2 .. layer 2, ..
* \brief [x11 x12 x13 ... , x21 x22 x23 .. , x31 x32 x33 .. ]
*
*/
IntegralStructures( INPUTDATATYPE *inputImage,
const unsigned int dimension,
const unsigned int nbRows,
const unsigned int nbCols,
const int intTypes )
{
m_Dimension = dimension;
m_NbElementsPerDimension = nbRows*nbCols;
m_NbExtendedElementsPerDimension = (nbRows+1)*(nbCols+1);
m_NbCovariances = ( m_Dimension*(m_Dimension+1) ) / 2;
m_NbRows = nbRows;
m_NbCols = nbCols;
m_NbColsExtended = nbCols + 1;
m_IntegralTypes = intTypes;
m_TmpSum = new float[m_Dimension];
m_TmpSquaredSum = new float[m_NbCovariances];
m_Assignment = new unsigned int[4*m_NbCovariances];
m_IntegralSquaredImage = 0;
m_IntegralImage= 0;
SetNewImage( inputImage );
}
/**
* Constructor
* \brief Default destructor
*/
~IntegralStructures()
{
if ( m_IntegralTypes & INT_IMAGE )
{
delete[] m_IntegralImage;
}
if ( m_IntegralTypes & INT_SQIMAGE )
{
delete[] m_IntegralSquaredImage;
}
delete[] m_TmpSum;
delete[] m_TmpSquaredSum;
delete[] m_Assignment;
}
/**
* \brief Set new image data with same dimension
*/
void SetNewImage( INPUTDATATYPE *inputImage )
{
if ( m_IntegralTypes & INT_IMAGE )
{
if ( m_IntegralImage != 0 )
{
delete[] m_IntegralImage;
}
BuildIntegralImage( inputImage );
}
if ( m_IntegralTypes & INT_SQIMAGE )
{
if ( m_IntegralSquaredImage != 0 )
{
delete[] m_IntegralSquaredImage;
}
BuildIntegralSquaredImage( inputImage );
}
}
/**
* \brief Returns the squared summed values in a given roi
*/
void GetSquaredSumIntImage( const myRect& roi, float* sum )
{
unsigned int pos1 = roi.width;
unsigned int pos2 = m_NbColsExtended*(roi.height);
unsigned int pos3 = pos1 + pos2;
unsigned int offset = roi.left + m_NbColsExtended*roi.upper;
for ( unsigned int currentDim = 0; currentDim < m_NbCovariances; ++currentDim )
{
const INTERNALDATATYPE *pt = &m_IntegralSquaredImage[ offset +
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE sumTmp = pt[0] + pt[pos3] - pt[ pos1 ] - pt[ pos2 ];
sum[currentDim] = (float)sumTmp;
}
}
/**
* \brief Returns the summed values in a given roi
*/
void GetSumIntImage( const myRect& roi, float *sum )
{
unsigned int pos1 = roi.width;
unsigned int pos2 = m_NbColsExtended*(roi.height);
unsigned int pos3 = pos1 + pos2;
unsigned int offset = roi.left + m_NbColsExtended*roi.upper;
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
const INTERNALDATATYPE *pt = &m_IntegralImage[ offset +
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE sumTmp = pt[0] + pt[pos3] - pt[ pos1 ] - pt[ pos2 ];
sum[currentDim] = (float)sumTmp;
}
}
/**
* \brief Returns the mean of a given roi
*/
void GetMeanIntImage( const myRect& roi, float* mean, bool& valid )
{
GetSumIntImage( roi, m_TmpSum );
float s = 1.0f/(roi.width*roi.height);
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
mean[currentDim] = m_TmpSum[currentDim]*s;
}
}
/**
* \brief Returns the variance values in a given roi
*/
void GetVarianceIntImage( const myRect& roi, float* variance )
{
// approximation
float area1 = 1.0f/( roi.width*roi.height-1.0f );
float area2 = 1.0f/( roi.width*roi.height );
GetSumIntImage( roi, m_TmpSum );
GetSquaredSumIntImage( roi, m_TmpSquaredSum );
unsigned int count = 0;
for ( unsigned int currentDim = 0; currentDim < m_NbCovariances; ++currentDim )
{
variance[ m_Assignment[count] ] =
variance[ m_Assignment[count + 1] ] =
area1*( m_TmpSquaredSum[currentDim] -
area2*m_TmpSum[ m_Assignment[count + 2]]*m_TmpSum[m_Assignment[count + 3]] );
count += 4;
}
}
/**
* \brief Returns the variance and mean values in a given roi in one step
*/
void GetMeanAndVariance( const myRect& roi, float* mean, float* variance )
{
int s = roi.width*roi.height;
float area1 = 1.0f/( s-1.0f );
float area2 = 1.0f/s;
unsigned int pos1 = roi.width;
unsigned int pos2 = m_NbColsExtended*roi.height;
unsigned int pos3 = pos1 + pos2;
unsigned int offset = roi.left + m_NbColsExtended*roi.upper;
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
const INTERNALDATATYPE *pt = &m_IntegralImage[ offset +
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE sum = pt[0] + pt[pos3] - pt[ pos1 ] - pt[ pos2 ];
m_TmpSum[currentDim] = (float)sum;
mean[currentDim] = m_TmpSum[currentDim]*area2;
}
for ( unsigned int currentDim = 0; currentDim < m_NbCovariances; ++currentDim )
{
const INTERNALDATATYPE *pt = &m_IntegralSquaredImage[ offset +
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE sum = pt[0] + pt[pos3] - pt[ pos1 ] - pt[ pos2 ];
m_TmpSquaredSum[currentDim] = static_cast<float>( sum );
}
unsigned int count = 0;
for ( unsigned int currentDim = 0; currentDim < m_NbCovariances; ++currentDim )
{
variance[ m_Assignment[count] ] = variance[ m_Assignment[count + 1] ] =
area1*( m_TmpSquaredSum[currentDim] - mean[ m_Assignment[count + 2] ]*m_TmpSum[ m_Assignment[count + 3] ] );
count += 4;
}
}
/**
* \brief Returns the sigma points in a given roi in one step
* sigmapoint vector size: DIM*(2*DIM+1)
*/
void GetSigmaPointFeature( const myRect& roi, float* sigmaPoints, bool& valid )
{
valid = true;
// first extract mean and variance
// *******************************
int s = roi.width*roi.height;
float area1 = 1.0f/( s-1.0f );
float area2 = 1.0f/( s );
unsigned int pos1 = roi.width;
unsigned int pos2 = m_NbColsExtended*roi.height;
unsigned int pos3 = pos1 + pos2;
unsigned int offset = roi.left + m_NbColsExtended*roi.upper;
float* mean = new float[m_Dimension];
float* variance = new float[m_Dimension*m_Dimension];
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
const INTERNALDATATYPE *pt = &m_IntegralImage[ offset +
currentDim*m_NbExtendedElementsPerDimension];
INTERNALDATATYPE sum = pt[0] + pt[pos3] - pt[ pos1 ] - pt[ pos2 ];
m_TmpSum[currentDim] = (float)sum;
mean[currentDim] = m_TmpSum[currentDim]*area2;
}
for ( unsigned int currentDim = 0; currentDim < m_NbCovariances; ++currentDim )
{
const INTERNALDATATYPE *pt = &m_IntegralSquaredImage[ offset +
currentDim*m_NbExtendedElementsPerDimension];
INTERNALDATATYPE sum = pt[0] + pt[pos3] - pt[ pos1 ] - pt[ pos2 ];
m_TmpSquaredSum[currentDim] = (float)sum;
}
unsigned int count = 0;
for ( unsigned int currentDim = 0; currentDim < m_NbCovariances; ++currentDim )
{
variance[ m_Assignment[count] ] = variance[ m_Assignment[count + 1] ] =
area1*( m_TmpSquaredSum[currentDim] -
mean[ m_Assignment[count + 2] ]*m_TmpSum[ m_Assignment[count + 3] ] );
count += 4;
}
// *******************************
// regularization for positive definite matrices,
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
variance[ currentDim*(m_Dimension+1) ] += 0.001f;
// *******************************
// add weight for gaussian signals
float weight = 2.0f*(m_Dimension+0.1f);
for ( unsigned int currentDim = 0; currentDim < m_Dimension*m_Dimension; ++currentDim )
variance[currentDim] *= weight;
// *******************************
// use m_TmpSum for cholesky
valid = ComputeCholeskyDecomposition( variance, m_TmpSum, m_Dimension );
// *******************************
// assign diagonal element
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
variance[ currentDim*(m_Dimension+1) ] = m_TmpSum[currentDim];
for ( unsigned int k = 0; k < m_Dimension; ++k )
for ( unsigned int i = k+1; i < m_Dimension; ++i )
variance[ i + k*m_Dimension ] = 0.0f;
memcpy(sigmaPoints, variance, m_Dimension*m_Dimension*sizeof(float));
// for(unsigned int cnt = 0; cnt < m_Dimension*m_Dimension; ++cnt)
// sigmaPoints[cnt] = varia
//// *******************************
//// sigma point generation, extract columns and +/- the variances
//for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
// sigmaPoints[currentDim] = mean[currentDim];
//for ( unsigned int currentDim = 0; currentDim < m_Dimension*m_Dimension; ++currentDim )
//{
// sigmaPoints[m_Dimension + currentDim ] =
// mean[currentDim % m_Dimension ] + variance[currentDim ];
// sigmaPoints[m_Dimension*m_Dimension + m_Dimension + currentDim ] =
// mean[currentDim % m_Dimension ] - variance[ currentDim ];
//}
delete[] variance;
delete[] mean;
}
/**
* \brief Returns the sigma points in a given roi in one step
* sigmapoint vector size: DIM*(2*DIM) ( without mean )
*/
void GetReducedSigmaPointFeature( const myRect& roi, float* sigmaPoints, bool& valid )
{
// ToDO
// compute sigma points without mean
}
/**
* \brief Returns the Number of Covariance Integral Images
*/
unsigned int GetNbOfCovIntegralStructures(){ return m_NbCovariances;};
/**
* \brief Returns the Number of Dimensons
*/
unsigned int GetDimension(){ return m_Dimension; };
/**
* \brief Returns the Number of Orientation Histogram Bins
*/
unsigned int GetNbOfRows(){ return m_NbRows; };
/**
* \brief Returns the Number of Orientation Histogram Bins
*/
unsigned int GetNbOfCols(){ return m_NbCols; };
private:
// general
unsigned int m_NbElementsPerDimension;
unsigned int m_NbExtendedElementsPerDimension;
unsigned int m_Dimension;
unsigned int m_NbRows;
unsigned int m_NbCols;
unsigned int m_NbColsExtended;
unsigned int m_NbCovariances;
// defines the compute integral types
int m_IntegralTypes;
// compute a cholesky decomposition
// taken from Numerical Recipes
bool ComputeCholeskyDecomposition( float *mat, float *p, int dim )
{
int i,j,k;
float sum;
for( i=0; i< dim; ++i )
{
for( j=i; j< dim; ++j )
{
sum = mat[j+dim*i];
k = i;
while ( --k >= 0 )
sum -= mat[k+dim*i]*mat[k+dim*j];
if ( i==j )
{
if (sum <= 0.0)
return false;
p[i] = sqrt( sum );
}
else
mat[i + dim*j] = sum/p[i];
}
}
return true;
}
// compute integral image
void BuildIntegralSquaredImage( INPUTDATATYPE *inputImage )
{
m_IntegralSquaredImage = new INTERNALDATATYPE[m_NbExtendedElementsPerDimension*m_NbCovariances];
memset( m_IntegralSquaredImage, 0x00, sizeof(INTERNALDATATYPE)*m_NbCovariances*
m_NbExtendedElementsPerDimension );
// compute assignment vector
unsigned int currentPosition = 0;
for ( unsigned int c1 = 0; c1 < m_Dimension; ++c1 )
{
for ( unsigned int c2 = c1; c2 < m_Dimension; ++c2 )
{
// store the assignment for efficient variance computation
m_Assignment[4*currentPosition + 0] = c1*m_Dimension + c2;
m_Assignment[4*currentPosition + 1] = c2*m_Dimension + c1;
m_Assignment[4*currentPosition + 2] = c1;
m_Assignment[4*currentPosition + 3] = c2;
INPUTDATATYPE *pt1 = &inputImage[ c1*m_NbElementsPerDimension ];
INPUTDATATYPE *pt2 = &inputImage[ c2*m_NbElementsPerDimension ];
INTERNALDATATYPE *ptImage = &m_IntegralSquaredImage[
currentPosition*m_NbExtendedElementsPerDimension + m_NbColsExtended + 1];
for( unsigned int y = 1 ; y < m_NbRows; ++y )
{
for( unsigned int x = 1 ; x < m_NbCols; ++x )
{
*ptImage++ = (*pt1++) * (*pt2++);
}
pt1++; pt2++;
ptImage++; ptImage++;
}
currentPosition++;
}
}
// accumulate
for ( unsigned int currentDim = 0; currentDim < currentPosition; ++currentDim )
{
INTERNALDATATYPE *pt1 = &m_IntegralSquaredImage[
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE *pt2 = &m_IntegralSquaredImage[
currentDim*m_NbExtendedElementsPerDimension + 1 ];
for( unsigned int y = 1 ; y < m_NbRows+1; ++y )
{
for( unsigned int x = 1 ; x < m_NbColsExtended; ++x )
{
*pt2 += *pt1;
pt1++; pt2++;
}
pt1++; pt2++;
}
}
for ( unsigned int currentDim = 0; currentDim < currentPosition; ++currentDim )
{
INTERNALDATATYPE *pt1 = &m_IntegralSquaredImage[
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE *pt2 = &m_IntegralSquaredImage[
currentDim*m_NbExtendedElementsPerDimension + m_NbColsExtended ];
for( unsigned int y = 1 ; y < m_NbRows+1; ++y )
{
for( unsigned int x = 1 ; x < m_NbColsExtended; ++x )
{
*pt2 += *pt1;
pt1++; pt2++;
}
pt1++; pt2++;
}
}
}
// computes the squared integral image
void BuildIntegralImage( INPUTDATATYPE *inputImage )
{
m_IntegralImage = new INTERNALDATATYPE[m_Dimension*m_NbExtendedElementsPerDimension];
memset( m_IntegralImage, 0x00, sizeof(INTERNALDATATYPE)*m_Dimension*
m_NbExtendedElementsPerDimension );
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
INPUTDATATYPE *ptImage = &inputImage[currentDim*m_NbElementsPerDimension];
INTERNALDATATYPE *ptIntImage = &m_IntegralImage[ currentDim*m_NbExtendedElementsPerDimension
+ m_NbColsExtended + 1 ];
for( unsigned int y = 1 ; y < m_NbRows; ++y )
{
for( unsigned int x = 1 ; x < m_NbCols; ++x )
{
*ptIntImage++ = *ptImage++;
}
// add for boundary
ptIntImage++; ptIntImage++; ptImage++;
}
}
// accumulate
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
INTERNALDATATYPE *pt1 = &m_IntegralImage[ currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE *pt2 = &m_IntegralImage[ currentDim*m_NbExtendedElementsPerDimension + 1 ];
for( unsigned int y = 1 ; y < m_NbRows+1; ++y )
{
for( unsigned int x = 1 ; x < m_NbColsExtended; ++x )
{
*pt2 += *pt1;
pt1++; pt2++;
}
pt1++; pt2++;
}
}
for ( unsigned int currentDim = 0; currentDim < m_Dimension; ++currentDim )
{
INTERNALDATATYPE *pt1 = &m_IntegralImage[
currentDim*m_NbExtendedElementsPerDimension ];
INTERNALDATATYPE *pt2 = &m_IntegralImage[
currentDim*m_NbExtendedElementsPerDimension + m_NbColsExtended ];
for( unsigned int y = 1 ; y < m_NbRows+1; ++y )
{
for( unsigned int x = 1 ; x < m_NbColsExtended; ++x )
{
*pt2 += *pt1;
pt1++; pt2++;
}
pt1++; pt2++;
}
}
}
// integral image structures
INTERNALDATATYPE *m_IntegralSquaredImage;
INTERNALDATATYPE *m_IntegralImage;
// sum and squared sum tmp structures
float *m_TmpSum;
float *m_TmpSquaredSum;
unsigned int *m_Assignment;
};
#endif