forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheigenobjects.cpp
More file actions
1810 lines (1584 loc) · 64.9 KB
/
eigenobjects.cpp
File metadata and controls
1810 lines (1584 loc) · 64.9 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
static CvStatus
icvJacobiEigens_32f(float *A, float *V, float *E, int n, float eps)
{
int i, j, k, ind;
float *AA = A, *VV = V;
double Amax, anorm = 0, ax;
if( A == NULL || V == NULL || E == NULL )
return CV_NULLPTR_ERR;
if( n <= 0 )
return CV_BADSIZE_ERR;
if( eps < 1.0e-7f )
eps = 1.0e-7f;
/*-------- Prepare --------*/
for( i = 0; i < n; i++, VV += n, AA += n )
{
for( j = 0; j < i; j++ )
{
double Am = AA[j];
anorm += Am * Am;
}
for( j = 0; j < n; j++ )
VV[j] = 0.f;
VV[i] = 1.f;
}
anorm = sqrt( anorm + anorm );
ax = anorm * eps / n;
Amax = anorm;
while( Amax > ax )
{
Amax /= n;
do /* while (ind) */
{
int p, q;
float *V1 = V, *A1 = A;
ind = 0;
for( p = 0; p < n - 1; p++, A1 += n, V1 += n )
{
float *A2 = A + n * (p + 1), *V2 = V + n * (p + 1);
for( q = p + 1; q < n; q++, A2 += n, V2 += n )
{
double x, y, c, s, c2, s2, a;
float *A3, Apq = A1[q], App, Aqq, Aip, Aiq, Vpi, Vqi;
if( fabs( Apq ) < Amax )
continue;
ind = 1;
/*---- Calculation of rotation angle's sine & cosine ----*/
App = A1[p];
Aqq = A2[q];
y = 5.0e-1 * (App - Aqq);
x = -Apq / sqrt( (double)Apq * Apq + (double)y * y );
if( y < 0.0 )
x = -x;
s = x / sqrt( 2.0 * (1.0 + sqrt( 1.0 - (double)x * x )));
s2 = s * s;
c = sqrt( 1.0 - s2 );
c2 = c * c;
a = 2.0 * Apq * c * s;
/*---- Apq annulation ----*/
A3 = A;
for( i = 0; i < p; i++, A3 += n )
{
Aip = A3[p];
Aiq = A3[q];
Vpi = V1[i];
Vqi = V2[i];
A3[p] = (float) (Aip * c - Aiq * s);
A3[q] = (float) (Aiq * c + Aip * s);
V1[i] = (float) (Vpi * c - Vqi * s);
V2[i] = (float) (Vqi * c + Vpi * s);
}
for( ; i < q; i++, A3 += n )
{
Aip = A1[i];
Aiq = A3[q];
Vpi = V1[i];
Vqi = V2[i];
A1[i] = (float) (Aip * c - Aiq * s);
A3[q] = (float) (Aiq * c + Aip * s);
V1[i] = (float) (Vpi * c - Vqi * s);
V2[i] = (float) (Vqi * c + Vpi * s);
}
for( ; i < n; i++ )
{
Aip = A1[i];
Aiq = A2[i];
Vpi = V1[i];
Vqi = V2[i];
A1[i] = (float) (Aip * c - Aiq * s);
A2[i] = (float) (Aiq * c + Aip * s);
V1[i] = (float) (Vpi * c - Vqi * s);
V2[i] = (float) (Vqi * c + Vpi * s);
}
A1[p] = (float) (App * c2 + Aqq * s2 - a);
A2[q] = (float) (App * s2 + Aqq * c2 + a);
A1[q] = A2[p] = 0.0f;
} /*q */
} /*p */
}
while( ind );
Amax /= n;
} /* while ( Amax > ax ) */
for( i = 0, k = 0; i < n; i++, k += n + 1 )
E[i] = A[k];
/*printf(" M = %d\n", M); */
/* -------- ordering -------- */
for( i = 0; i < n; i++ )
{
int m = i;
float Em = (float) fabs( E[i] );
for( j = i + 1; j < n; j++ )
{
float Ej = (float) fabs( E[j] );
m = (Em < Ej) ? j : m;
Em = (Em < Ej) ? Ej : Em;
}
if( m != i )
{
int l;
float b = E[i];
E[i] = E[m];
E[m] = b;
for( j = 0, k = i * n, l = m * n; j < n; j++, k++, l++ )
{
b = V[k];
V[k] = V[l];
V[l] = b;
}
}
}
return CV_NO_ERR;
}
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: icvCalcCovarMatrixEx_8u32fR
// Purpose: The function calculates a covariance matrix for a group of input objects
// (images, vectors, etc.). ROI supported.
// Context:
// Parameters: nObjects - number of source objects
// objects - array of pointers to ROIs of the source objects
// imgStep - full width of each source object row in bytes
// avg - pointer to averaged object
// avgStep - full width of averaged object row in bytes
// size - ROI size of each source and averaged objects
// covarMatrix - covariance matrix (output parameter; must be allocated
// before call)
//
// Returns: CV_NO_ERR or error code
//
// Notes:
//F*/
static CvStatus CV_STDCALL
icvCalcCovarMatrixEx_8u32fR( int nObjects, void *input, int objStep1,
int ioFlags, int ioBufSize, uchar* buffer,
void *userData, float *avg, int avgStep,
CvSize size, float *covarMatrix )
{
int objStep = objStep1;
/* ---- TEST OF PARAMETERS ---- */
if( nObjects < 2 )
return CV_BADFACTOR_ERR;
if( ioFlags < 0 || ioFlags > 3 )
return CV_BADFACTOR_ERR;
if( ioFlags && ioBufSize < 1024 )
return CV_BADFACTOR_ERR;
if( ioFlags && buffer == NULL )
return CV_NULLPTR_ERR;
if( input == NULL || avg == NULL || covarMatrix == NULL )
return CV_NULLPTR_ERR;
if( size.width > objStep || 4 * size.width > avgStep || size.height < 1 )
return CV_BADSIZE_ERR;
avgStep /= 4;
if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK ) /* ==== USE INPUT CALLBACK ==== */
{
int nio, ngr, igr, n = size.width * size.height, mm = 0;
CvCallback read_callback = ((CvInput *) & input)->callback;
uchar *buffer2;
objStep = n;
nio = ioBufSize / n; /* number of objects in buffer */
ngr = nObjects / nio; /* number of io groups */
if( nObjects % nio )
mm = 1;
ngr += mm;
buffer2 = (uchar *)cvAlloc( sizeof( uchar ) * n );
if( buffer2 == NULL )
return CV_OUTOFMEM_ERR;
for( igr = 0; igr < ngr; igr++ )
{
int k, l;
int io, jo, imin = igr * nio, imax = imin + nio;
uchar *bu1 = buffer, *bu2;
if( imax > nObjects )
imax = nObjects;
/* read igr group */
for( io = imin; io < imax; io++, bu1 += n )
{
CvStatus r;
r = (CvStatus)read_callback( io, (void *) bu1, userData );
if( r )
return r;
}
/* diagonal square calc */
bu1 = buffer;
for( io = imin; io < imax; io++, bu1 += n )
{
bu2 = bu1;
for( jo = io; jo < imax; jo++, bu2 += n )
{
float w = 0.f;
float *fu = avg;
int ij = 0;
for( k = 0; k < size.height; k++, fu += avgStep )
for( l = 0; l < size.width; l++, ij++ )
{
float f = fu[l], u1 = bu1[ij], u2 = bu2[ij];
w += (u1 - f) * (u2 - f);
}
covarMatrix[io * nObjects + jo] = covarMatrix[jo * nObjects + io] = w;
}
}
/* non-diagonal elements calc */
for( jo = imax; jo < nObjects; jo++ )
{
CvStatus r;
bu1 = buffer;
bu2 = buffer2;
/* read jo object */
r = (CvStatus)read_callback( jo, (void *) bu2, userData );
if( r )
return r;
for( io = imin; io < imax; io++, bu1 += n )
{
float w = 0.f;
float *fu = avg;
int ij = 0;
for( k = 0; k < size.height; k++, fu += avgStep )
{
for( l = 0; l < size.width - 3; l += 4, ij += 4 )
{
float f = fu[l];
uchar u1 = bu1[ij];
uchar u2 = bu2[ij];
w += (u1 - f) * (u2 - f);
f = fu[l + 1];
u1 = bu1[ij + 1];
u2 = bu2[ij + 1];
w += (u1 - f) * (u2 - f);
f = fu[l + 2];
u1 = bu1[ij + 2];
u2 = bu2[ij + 2];
w += (u1 - f) * (u2 - f);
f = fu[l + 3];
u1 = bu1[ij + 3];
u2 = bu2[ij + 3];
w += (u1 - f) * (u2 - f);
}
for( ; l < size.width; l++, ij++ )
{
float f = fu[l], u1 = bu1[ij], u2 = bu2[ij];
w += (u1 - f) * (u2 - f);
}
}
covarMatrix[io * nObjects + jo] = covarMatrix[jo * nObjects + io] = w;
}
}
} /* igr */
cvFree( &buffer2 );
} /* if() */
else
/* ==== NOT USE INPUT CALLBACK ==== */
{
int i, j;
uchar **objects = (uchar **) (((CvInput *) & input)->data);
for( i = 0; i < nObjects; i++ )
{
uchar *bu = objects[i];
for( j = i; j < nObjects; j++ )
{
int k, l;
float w = 0.f;
float *a = avg;
uchar *bu1 = bu;
uchar *bu2 = objects[j];
for( k = 0; k < size.height;
k++, bu1 += objStep, bu2 += objStep, a += avgStep )
{
for( l = 0; l < size.width - 3; l += 4 )
{
float f = a[l];
uchar u1 = bu1[l];
uchar u2 = bu2[l];
w += (u1 - f) * (u2 - f);
f = a[l + 1];
u1 = bu1[l + 1];
u2 = bu2[l + 1];
w += (u1 - f) * (u2 - f);
f = a[l + 2];
u1 = bu1[l + 2];
u2 = bu2[l + 2];
w += (u1 - f) * (u2 - f);
f = a[l + 3];
u1 = bu1[l + 3];
u2 = bu2[l + 3];
w += (u1 - f) * (u2 - f);
}
for( ; l < size.width; l++ )
{
float f = a[l];
uchar u1 = bu1[l];
uchar u2 = bu2[l];
w += (u1 - f) * (u2 - f);
}
}
covarMatrix[i * nObjects + j] = covarMatrix[j * nObjects + i] = w;
}
}
} /* else */
return CV_NO_ERR;
}
/*======================== end of icvCalcCovarMatrixEx_8u32fR ===========================*/
static int
icvDefaultBufferSize( void )
{
return 10 * 1024 * 1024;
}
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: icvCalcEigenObjects_8u32fR
// Purpose: The function calculates an orthonormal eigen basis and a mean (averaged)
// object for a group of input objects (images, vectors, etc.). ROI supported.
// Context:
// Parameters: nObjects - number of source objects
// input - pointer either to array of pointers to input objects
// or to read callback function (depending on ioFlags)
// imgStep - full width of each source object row in bytes
// output - pointer either to array of pointers to output eigen objects
// or to write callback function (depending on ioFlags)
// eigStep - full width of each eigenobject row in bytes
// size - ROI size of each source object
// ioFlags - input/output flags (see Notes)
// ioBufSize - input/output buffer size
// userData - pointer to the structure which contains all necessary
// data for the callback functions
// calcLimit - determines the calculation finish conditions
// avg - pointer to averaged object (has the same size as ROI)
// avgStep - full width of averaged object row in bytes
// eigVals - pointer to corresponding eigenvalues (array of <nObjects>
// elements in descending order)
//
// Returns: CV_NO_ERR or error code
//
// Notes: 1. input/output data (that is, input objects and eigen ones) may either
// be allocated in the RAM or be read from/written to the HDD (or any
// other device) by read/write callback functions. It depends on the
// value of ioFlags paramater, which may be the following:
// CV_EIGOBJ_NO_CALLBACK, or 0;
// CV_EIGOBJ_INPUT_CALLBACK;
// CV_EIGOBJ_OUTPUT_CALLBACK;
// CV_EIGOBJ_BOTH_CALLBACK, or
// CV_EIGOBJ_INPUT_CALLBACK | CV_EIGOBJ_OUTPUT_CALLBACK.
// The callback functions as well as the user data structure must be
// developed by the user.
//
// 2. If ioBufSize = 0, or it's too large, the function dermines buffer size
// itself.
//
// 3. Depending on calcLimit parameter, calculations are finished either if
// eigenfaces number comes up to certain value or the relation of the
// current eigenvalue and the largest one comes down to certain value
// (or any of the above conditions takes place). The calcLimit->type value
// must be CV_TERMCRIT_NUMB, CV_TERMCRIT_EPS or
// CV_TERMCRIT_NUMB | CV_TERMCRIT_EPS. The function returns the real
// values calcLimit->max_iter and calcLimit->epsilon.
//
// 4. eigVals may be equal to NULL (if you don't need eigen values in further).
//
//F*/
static CvStatus CV_STDCALL
icvCalcEigenObjects_8u32fR( int nObjects, void* input, int objStep,
void* output, int eigStep, CvSize size,
int ioFlags, int ioBufSize, void* userData,
CvTermCriteria* calcLimit, float* avg,
int avgStep, float *eigVals )
{
int i, j, n, iev = 0, m1 = nObjects - 1, objStep1 = objStep, eigStep1 = eigStep / 4;
CvSize objSize, eigSize, avgSize;
float *c = 0;
float *ev = 0;
float *bf = 0;
uchar *buf = 0;
void *buffer = 0;
float m = 1.0f / (float) nObjects;
CvStatus r;
if( m1 > calcLimit->max_iter && calcLimit->type != CV_TERMCRIT_EPS )
m1 = calcLimit->max_iter;
/* ---- TEST OF PARAMETERS ---- */
if( nObjects < 2 )
return CV_BADFACTOR_ERR;
if( ioFlags < 0 || ioFlags > 3 )
return CV_BADFACTOR_ERR;
if( input == NULL || output == NULL || avg == NULL )
return CV_NULLPTR_ERR;
if( size.width > objStep || 4 * size.width > eigStep ||
4 * size.width > avgStep || size.height < 1 )
return CV_BADSIZE_ERR;
if( !(ioFlags & CV_EIGOBJ_INPUT_CALLBACK) )
for( i = 0; i < nObjects; i++ )
if( ((uchar **) input)[i] == NULL )
return CV_NULLPTR_ERR;
if( !(ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK) )
for( i = 0; i < m1; i++ )
if( ((float **) output)[i] == NULL )
return CV_NULLPTR_ERR;
avgStep /= 4;
eigStep /= 4;
if( objStep == size.width && eigStep == size.width && avgStep == size.width )
{
size.width *= size.height;
size.height = 1;
objStep = objStep1 = eigStep = eigStep1 = avgStep = size.width;
}
objSize = eigSize = avgSize = size;
if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
{
objSize.width *= objSize.height;
objSize.height = 1;
objStep = objSize.width;
objStep1 = size.width;
}
if( ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK )
{
eigSize.width *= eigSize.height;
eigSize.height = 1;
eigStep = eigSize.width;
eigStep1 = size.width;
}
n = objSize.height * objSize.width * (ioFlags & CV_EIGOBJ_INPUT_CALLBACK) +
2 * eigSize.height * eigSize.width * (ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK);
/* Buffer size determination */
if( ioFlags )
{
ioBufSize = MIN( icvDefaultBufferSize(), n );
}
/* memory allocation (if necesseay) */
if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
{
buf = (uchar *) cvAlloc( sizeof( uchar ) * objSize.width );
if( buf == NULL )
return CV_OUTOFMEM_ERR;
}
if( ioFlags )
{
buffer = (void *) cvAlloc( ioBufSize );
if( buffer == NULL )
{
if( buf )
cvFree( &buf );
return CV_OUTOFMEM_ERR;
}
}
/* Calculation of averaged object */
bf = avg;
for( i = 0; i < avgSize.height; i++, bf += avgStep )
for( j = 0; j < avgSize.width; j++ )
bf[j] = 0.f;
for( i = 0; i < nObjects; i++ )
{
int k, l;
uchar *bu = (ioFlags & CV_EIGOBJ_INPUT_CALLBACK) ? buf : ((uchar **) input)[i];
if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
{
CvCallback read_callback = ((CvInput *) & input)->callback;
r = (CvStatus)read_callback( i, (void *) buf, userData );
if( r )
{
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return r;
}
}
bf = avg;
for( k = 0; k < avgSize.height; k++, bf += avgStep, bu += objStep1 )
for( l = 0; l < avgSize.width; l++ )
bf[l] += bu[l];
}
bf = avg;
for( i = 0; i < avgSize.height; i++, bf += avgStep )
for( j = 0; j < avgSize.width; j++ )
bf[j] *= m;
/* Calculation of covariance matrix */
c = (float *) cvAlloc( sizeof( float ) * nObjects * nObjects );
if( c == NULL )
{
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return CV_OUTOFMEM_ERR;
}
r = icvCalcCovarMatrixEx_8u32fR( nObjects, input, objStep1, ioFlags, ioBufSize,
(uchar *) buffer, userData, avg, 4 * avgStep, size, c );
if( r )
{
cvFree( &c );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return r;
}
/* Calculation of eigenvalues & eigenvectors */
ev = (float *) cvAlloc( sizeof( float ) * nObjects * nObjects );
if( ev == NULL )
{
cvFree( &c );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return CV_OUTOFMEM_ERR;
}
if( eigVals == NULL )
{
eigVals = (float *) cvAlloc( sizeof( float ) * nObjects );
if( eigVals == NULL )
{
cvFree( &c );
cvFree( &ev );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return CV_OUTOFMEM_ERR;
}
iev = 1;
}
r = icvJacobiEigens_32f( c, ev, eigVals, nObjects, 0.0f );
cvFree( &c );
if( r )
{
cvFree( &ev );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
if( iev )
cvFree( &eigVals );
return r;
}
/* Eigen objects number determination */
if( calcLimit->type != CV_TERMCRIT_NUMBER )
{
for( i = 0; i < m1; i++ )
if( fabs( eigVals[i] / eigVals[0] ) < calcLimit->epsilon )
break;
m1 = calcLimit->max_iter = i;
}
else
m1 = calcLimit->max_iter;
calcLimit->epsilon = (float) fabs( eigVals[m1 - 1] / eigVals[0] );
for( i = 0; i < m1; i++ )
eigVals[i] = (float) (1.0 / sqrt( (double)eigVals[i] ));
/* ----------------- Calculation of eigenobjects ----------------------- */
if( ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK )
{
int nio, ngr, igr;
nio = ioBufSize / (4 * eigSize.width); /* number of eigen objects in buffer */
ngr = m1 / nio; /* number of io groups */
if( nObjects % nio )
ngr += 1;
for( igr = 0; igr < ngr; igr++ )
{
int io, ie, imin = igr * nio, imax = imin + nio;
if( imax > m1 )
imax = m1;
for(int k = 0; k < eigSize.width * (imax - imin); k++ )
((float *) buffer)[k] = 0.f;
for( io = 0; io < nObjects; io++ )
{
uchar *bu = ioFlags & CV_EIGOBJ_INPUT_CALLBACK ? buf : ((uchar **) input)[io];
if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
{
CvCallback read_callback = ((CvInput *) & input)->callback;
r = (CvStatus)read_callback( io, (void *) buf, userData );
if( r )
{
cvFree( &ev );
if( iev )
cvFree( &eigVals );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return r;
}
}
for( ie = imin; ie < imax; ie++ )
{
int k, l;
uchar *bv = bu;
float e = ev[ie * nObjects + io] * eigVals[ie];
float *be = ((float *) buffer) + ((ie - imin) * eigStep);
bf = avg;
for( k = 0; k < size.height; k++, bv += objStep1,
bf += avgStep, be += eigStep1 )
{
for( l = 0; l < size.width - 3; l += 4 )
{
float f = bf[l];
uchar v = bv[l];
be[l] += e * (v - f);
f = bf[l + 1];
v = bv[l + 1];
be[l + 1] += e * (v - f);
f = bf[l + 2];
v = bv[l + 2];
be[l + 2] += e * (v - f);
f = bf[l + 3];
v = bv[l + 3];
be[l + 3] += e * (v - f);
}
for( ; l < size.width; l++ )
be[l] += e * (bv[l] - bf[l]);
}
}
} /* io */
for( ie = imin; ie < imax; ie++ ) /* calculated eigen objects writting */
{
CvCallback write_callback = ((CvInput *) & output)->callback;
float *be = ((float *) buffer) + ((ie - imin) * eigStep);
r = (CvStatus)write_callback( ie, (void *) be, userData );
if( r )
{
cvFree( &ev );
if( iev )
cvFree( &eigVals );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return r;
}
}
} /* igr */
}
else
{
int k, p, l;
for( i = 0; i < m1; i++ ) /* e.o. annulation */
{
float *be = ((float **) output)[i];
for( p = 0; p < eigSize.height; p++, be += eigStep )
for( l = 0; l < eigSize.width; l++ )
be[l] = 0.0f;
}
for( k = 0; k < nObjects; k++ )
{
uchar *bv = (ioFlags & CV_EIGOBJ_INPUT_CALLBACK) ? buf : ((uchar **) input)[k];
if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
{
CvCallback read_callback = ((CvInput *) & input)->callback;
r = (CvStatus)read_callback( k, (void *) buf, userData );
if( r )
{
cvFree( &ev );
if( iev )
cvFree( &eigVals );
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return r;
}
}
for( i = 0; i < m1; i++ )
{
float v = eigVals[i] * ev[i * nObjects + k];
float *be = ((float **) output)[i];
uchar *bu = bv;
bf = avg;
for( p = 0; p < size.height; p++, bu += objStep1,
bf += avgStep, be += eigStep1 )
{
for( l = 0; l < size.width - 3; l += 4 )
{
float f = bf[l];
uchar u = bu[l];
be[l] += v * (u - f);
f = bf[l + 1];
u = bu[l + 1];
be[l + 1] += v * (u - f);
f = bf[l + 2];
u = bu[l + 2];
be[l + 2] += v * (u - f);
f = bf[l + 3];
u = bu[l + 3];
be[l + 3] += v * (u - f);
}
for( ; l < size.width; l++ )
be[l] += v * (bu[l] - bf[l]);
}
} /* i */
} /* k */
} /* else */
cvFree( &ev );
if( iev )
cvFree( &eigVals );
else
for( i = 0; i < m1; i++ )
eigVals[i] = 1.f / (eigVals[i] * eigVals[i]);
if( buffer )
cvFree( &buffer );
if( buf )
cvFree( &buf );
return CV_NO_ERR;
}
/* --- End of icvCalcEigenObjects_8u32fR --- */
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: icvCalcDecompCoeff_8u32fR
// Purpose: The function calculates one decomposition coefficient of input object
// using previously calculated eigen object and the mean (averaged) object
// Context:
// Parameters: obj - input object
// objStep - its step (in bytes)
// eigObj - pointer to eigen object
// eigStep - its step (in bytes)
// avg - pointer to averaged object
// avgStep - its step (in bytes)
// size - ROI size of each source object
//
// Returns: decomposition coefficient value or large negative value (if error)
//
// Notes:
//F*/
static float CV_STDCALL
icvCalcDecompCoeff_8u32fR( uchar* obj, int objStep,
float *eigObj, int eigStep,
float *avg, int avgStep, CvSize size )
{
int i, k;
float w = 0.0f;
if( size.width > objStep || 4 * size.width > eigStep
|| 4 * size.width > avgStep || size.height < 1 )
return -1.0e30f;
if( obj == NULL || eigObj == NULL || avg == NULL )
return -1.0e30f;
eigStep /= 4;
avgStep /= 4;
if( size.width == objStep && size.width == eigStep && size.width == avgStep )
{
size.width *= size.height;
size.height = 1;
objStep = eigStep = avgStep = size.width;
}
for( i = 0; i < size.height; i++, obj += objStep, eigObj += eigStep, avg += avgStep )
{
for( k = 0; k < size.width - 4; k += 4 )
{
float o = (float) obj[k];
float e = eigObj[k];
float a = avg[k];
w += e * (o - a);
o = (float) obj[k + 1];
e = eigObj[k + 1];
a = avg[k + 1];
w += e * (o - a);
o = (float) obj[k + 2];
e = eigObj[k + 2];
a = avg[k + 2];
w += e * (o - a);
o = (float) obj[k + 3];
e = eigObj[k + 3];
a = avg[k + 3];
w += e * (o - a);
}
for( ; k < size.width; k++ )
w += eigObj[k] * ((float) obj[k] - avg[k]);
}
return w;
}
/*F///////////////////////////////////////////////////////////////////////////////////////
// Names: icvEigenDecomposite_8u32fR
// Purpose: The function calculates all decomposition coefficients for input object
// using previously calculated eigen objects basis and the mean (averaged)
// object
// Context:
// Parameters: obj - input object
// objStep - its step (in bytes)
// nEigObjs - number of eigen objects
// eigInput - pointer either to array of pointers to eigen objects
// or to read callback function (depending on ioFlags)
// eigStep - eigen objects step (in bytes)
// ioFlags - input/output flags
// iserData - pointer to the structure which contains all necessary
// data for the callback function
// avg - pointer to averaged object
// avgStep - its step (in bytes)
// size - ROI size of each source object
// coeffs - calculated coefficients (output data)
//
// Returns: icv status
//
// Notes: see notes for icvCalcEigenObjects_8u32fR function
//F*/
static CvStatus CV_STDCALL
icvEigenDecomposite_8u32fR( uchar * obj, int objStep, int nEigObjs,
void *eigInput, int eigStep, int ioFlags,
void *userData, float *avg, int avgStep,
CvSize size, float *coeffs )
{
int i;
if( nEigObjs < 2 )
return CV_BADFACTOR_ERR;
if( ioFlags < 0 || ioFlags > 1 )
return CV_BADFACTOR_ERR;
if( size.width > objStep || 4 * size.width > eigStep ||
4 * size.width > avgStep || size.height < 1 )
return CV_BADSIZE_ERR;
if( obj == NULL || eigInput == NULL || coeffs == NULL || avg == NULL )
return CV_NULLPTR_ERR;
if( !ioFlags )
for( i = 0; i < nEigObjs; i++ )
if( ((uchar **) eigInput)[i] == NULL )
return CV_NULLPTR_ERR;
if( ioFlags ) /* callback */
{
float *buffer;
CvCallback read_callback = ((CvInput *) & eigInput)->callback;
eigStep = 4 * size.width;
/* memory allocation */
buffer = (float *) cvAlloc( sizeof( float ) * size.width * size.height );
if( buffer == NULL )
return CV_OUTOFMEM_ERR;
for( i = 0; i < nEigObjs; i++ )
{
float w;
CvStatus r = (CvStatus)read_callback( i, (void *) buffer, userData );
if( r )