-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvhaartraining.cpp
More file actions
3065 lines (2604 loc) · 103 KB
/
Copy pathcvhaartraining.cpp
File metadata and controls
3065 lines (2604 loc) · 103 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*/
/*
* cvhaartraining.cpp
*
* training of cascade of boosted classifiers based on haar features
*/
#include "cvhaartraining.h"
#include "_cvhaartraining.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctype.h>
#include "highgui.h"
#ifdef CV_VERBOSE
#include <ctime>
#ifdef _WIN32
/* use clock() function insted of time() */
#define TIME( arg ) (((double) clock()) / CLOCKS_PER_SEC)
#else
#define TIME( arg ) (time( arg ))
#endif /* _WIN32 */
#endif /* CV_VERBOSE */
#if defined CV_OPENMP && (defined _MSC_VER || defined CV_ICC)
#define CV_OPENMP 1
#else
#undef CV_OPENMP
#endif
typedef struct CvBackgroundData
{
int count;
char** filename;
int last;
int round;
CvSize winsize;
} CvBackgroundData;
typedef struct CvBackgroundReader
{
CvMat src;
CvMat img;
CvPoint offset;
float scale;
float scalefactor;
float stepfactor;
CvPoint point;
} CvBackgroundReader;
/*
* Background reader
* Created in each thread
*/
CvBackgroundReader* cvbgreader = NULL;
#if defined CV_OPENMP
#pragma omp threadprivate(cvbgreader)
#endif
CvBackgroundData* cvbgdata = NULL;
/*
* get sum image offsets for <rect> corner points
* step - row step (measured in image pixels!) of sum image
*/
#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \
/* (x, y) */ \
(p0) = (rect).x + (step) * (rect).y; \
/* (x + w, y) */ \
(p1) = (rect).x + (rect).width + (step) * (rect).y; \
/* (x + w, y) */ \
(p2) = (rect).x + (step) * ((rect).y + (rect).height); \
/* (x + w, y + h) */ \
(p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);
/*
* get tilted image offsets for <rect> corner points
* step - row step (measured in image pixels!) of tilted image
*/
#define CV_TILTED_OFFSETS( p0, p1, p2, p3, rect, step ) \
/* (x, y) */ \
(p0) = (rect).x + (step) * (rect).y; \
/* (x - h, y + h) */ \
(p1) = (rect).x - (rect).height + (step) * ((rect).y + (rect).height);\
/* (x + w, y + w) */ \
(p2) = (rect).x + (rect).width + (step) * ((rect).y + (rect).width); \
/* (x + w - h, y + w + h) */ \
(p3) = (rect).x + (rect).width - (rect).height \
+ (step) * ((rect).y + (rect).width + (rect).height);
/*
* icvCreateIntHaarFeatures
*
* Create internal representation of haar features
*
* mode:
* 0 - BASIC = Viola
* 1 - CORE = All upright
* 2 - ALL = All features
*/
static
CvIntHaarFeatures* icvCreateIntHaarFeatures( CvSize winsize,
int mode,
int symmetric )
{
CvIntHaarFeatures* features = NULL;
CvTHaarFeature haarFeature;
CvMemStorage* storage = NULL;
CvSeq* seq = NULL;
CvSeqWriter writer;
int s0 = 36; /* minimum total area size of basic haar feature */
int s1 = 12; /* minimum total area size of tilted haar features 2 */
int s2 = 18; /* minimum total area size of tilted haar features 3 */
int s3 = 24; /* minimum total area size of tilted haar features 4 */
int x = 0;
int y = 0;
int dx = 0;
int dy = 0;
#if 0
float factor = 1.0F;
factor = ((float) winsize.width) * winsize.height / (24 * 24);
s0 = (int) (s0 * factor);
s1 = (int) (s1 * factor);
s2 = (int) (s2 * factor);
s3 = (int) (s3 * factor);
#else
s0 = 1;
s1 = 1;
s2 = 1;
s3 = 1;
#endif
/* CV_VECTOR_CREATE( vec, CvIntHaarFeature, size, maxsize ) */
storage = cvCreateMemStorage();
cvStartWriteSeq( 0, sizeof( CvSeq ), sizeof( haarFeature ), storage, &writer );
for( x = 0; x < winsize.width; x++ )
{
for( y = 0; y < winsize.height; y++ )
{
for( dx = 1; dx <= winsize.width; dx++ )
{
for( dy = 1; dy <= winsize.height; dy++ )
{
// haar_x2
if ( (x+dx*2 <= winsize.width) && (y+dy <= winsize.height) ) {
if (dx*2*dy < s0) continue;
if (!symmetric || (x+x+dx*2 <=winsize.width)) {
haarFeature = cvHaarFeature( "haar_x2",
x, y, dx*2, dy, -1,
x+dx, y, dx , dy, +2 );
/* CV_VECTOR_PUSH( vec, CvIntHaarFeature, haarFeature, size, maxsize, step ) */
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_y2
if ( (x+dx <= winsize.width) && (y+dy*2 <= winsize.height) ) {
if (dx*2*dy < s0) continue;
if (!symmetric || (x+x+dx <= winsize.width)) {
haarFeature = cvHaarFeature( "haar_y2",
x, y, dx, dy*2, -1,
x, y+dy, dx, dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_x3
if ( (x+dx*3 <= winsize.width) && (y+dy <= winsize.height) ) {
if (dx*3*dy < s0) continue;
if (!symmetric || (x+x+dx*3 <=winsize.width)) {
haarFeature = cvHaarFeature( "haar_x3",
x, y, dx*3, dy, -1,
x+dx, y, dx, dy, +3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_y3
if ( (x+dx <= winsize.width) && (y+dy*3 <= winsize.height) ) {
if (dx*3*dy < s0) continue;
if (!symmetric || (x+x+dx <= winsize.width)) {
haarFeature = cvHaarFeature( "haar_y3",
x, y, dx, dy*3, -1,
x, y+dy, dx, dy, +3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
if( mode != 0 /*BASIC*/ ) {
// haar_x4
if ( (x+dx*4 <= winsize.width) && (y+dy <= winsize.height) ) {
if (dx*4*dy < s0) continue;
if (!symmetric || (x+x+dx*4 <=winsize.width)) {
haarFeature = cvHaarFeature( "haar_x4",
x, y, dx*4, dy, -1,
x+dx, y, dx*2, dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_y4
if ( (x+dx <= winsize.width ) && (y+dy*4 <= winsize.height) ) {
if (dx*4*dy < s0) continue;
if (!symmetric || (x+x+dx <=winsize.width)) {
haarFeature = cvHaarFeature( "haar_y4",
x, y, dx, dy*4, -1,
x, y+dy, dx, dy*2, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
}
// x2_y2
if ( (x+dx*2 <= winsize.width) && (y+dy*2 <= winsize.height) ) {
if (dx*4*dy < s0) continue;
if (!symmetric || (x+x+dx*2 <=winsize.width)) {
haarFeature = cvHaarFeature( "haar_x2_y2",
x , y, dx*2, dy*2, -1,
x , y , dx , dy, +2,
x+dx, y+dy, dx , dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
if (mode != 0 /*BASIC*/) {
// point
if ( (x+dx*3 <= winsize.width) && (y+dy*3 <= winsize.height) ) {
if (dx*9*dy < s0) continue;
if (!symmetric || (x+x+dx*3 <=winsize.width)) {
haarFeature = cvHaarFeature( "haar_point",
x , y, dx*3, dy*3, -1,
x+dx, y+dy, dx , dy , +9);
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
}
if (mode == 2 /*ALL*/) {
// tilted haar_x2 (x, y, w, h, b, weight)
if ( (x+2*dx <= winsize.width) && (y+2*dx+dy <= winsize.height) && (x-dy>= 0) ) {
if (dx*2*dy < s1) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_x2",
x, y, dx*2, dy, -1,
x, y, dx , dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_y2 (x, y, w, h, b, weight)
if ( (x+dx <= winsize.width) && (y+dx+2*dy <= winsize.height) && (x-2*dy>= 0) ) {
if (dx*2*dy < s1) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_y2",
x, y, dx, 2*dy, -1,
x, y, dx, dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_x3 (x, y, w, h, b, weight)
if ( (x+3*dx <= winsize.width) && (y+3*dx+dy <= winsize.height) && (x-dy>= 0) ) {
if (dx*3*dy < s2) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_x3",
x, y, dx*3, dy, -1,
x+dx, y+dx, dx , dy, +3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_y3 (x, y, w, h, b, weight)
if ( (x+dx <= winsize.width) && (y+dx+3*dy <= winsize.height) && (x-3*dy>= 0) ) {
if (dx*3*dy < s2) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_y3",
x, y, dx, 3*dy, -1,
x-dy, y+dy, dx, dy, +3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_x4 (x, y, w, h, b, weight)
if ( (x+4*dx <= winsize.width) && (y+4*dx+dy <= winsize.height) && (x-dy>= 0) ) {
if (dx*4*dy < s3) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_x4",
x, y, dx*4, dy, -1,
x+dx, y+dx, dx*2, dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_y4 (x, y, w, h, b, weight)
if ( (x+dx <= winsize.width) && (y+dx+4*dy <= winsize.height) && (x-4*dy>= 0) ) {
if (dx*4*dy < s3) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_y4",
x, y, dx, 4*dy, -1,
x-dy, y+dy, dx, 2*dy, +2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
/*
// tilted point
if ( (x+dx*3 <= winsize.width - 1) && (y+dy*3 <= winsize.height - 1) && (x-3*dy>= 0)) {
if (dx*9*dy < 36) continue;
if (!symmetric || (x <= (winsize.width / 2) )) {
haarFeature = cvHaarFeature( "tilted_haar_point",
x, y, dx*3, dy*3, -1,
x, y+dy, dx , dy, +9 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
*/
}
}
}
}
}
seq = cvEndWriteSeq( &writer );
features = (CvIntHaarFeatures*) cvAlloc( sizeof( CvIntHaarFeatures ) +
( sizeof( CvTHaarFeature ) + sizeof( CvFastHaarFeature ) ) * seq->total );
features->feature = (CvTHaarFeature*) (features + 1);
features->fastfeature = (CvFastHaarFeature*) ( features->feature + seq->total );
features->count = seq->total;
features->winsize = winsize;
cvCvtSeqToArray( seq, (CvArr*) features->feature );
cvReleaseMemStorage( &storage );
icvConvertToFastHaarFeature( features->feature, features->fastfeature,
features->count, (winsize.width + 1) );
return features;
}
static
void icvReleaseIntHaarFeatures( CvIntHaarFeatures** intHaarFeatures )
{
if( intHaarFeatures != NULL && (*intHaarFeatures) != NULL )
{
cvFree( intHaarFeatures );
(*intHaarFeatures) = NULL;
}
}
void icvConvertToFastHaarFeature( CvTHaarFeature* haarFeature,
CvFastHaarFeature* fastHaarFeature,
int size, int step )
{
int i = 0;
int j = 0;
for( i = 0; i < size; i++ )
{
fastHaarFeature[i].tilted = haarFeature[i].tilted;
if( !fastHaarFeature[i].tilted )
{
for( j = 0; j < CV_HAAR_FEATURE_MAX; j++ )
{
fastHaarFeature[i].rect[j].weight = haarFeature[i].rect[j].weight;
if( fastHaarFeature[i].rect[j].weight == 0.0F )
{
break;
}
CV_SUM_OFFSETS( fastHaarFeature[i].rect[j].p0,
fastHaarFeature[i].rect[j].p1,
fastHaarFeature[i].rect[j].p2,
fastHaarFeature[i].rect[j].p3,
haarFeature[i].rect[j].r, step )
}
}
else
{
for( j = 0; j < CV_HAAR_FEATURE_MAX; j++ )
{
fastHaarFeature[i].rect[j].weight = haarFeature[i].rect[j].weight;
if( fastHaarFeature[i].rect[j].weight == 0.0F )
{
break;
}
CV_TILTED_OFFSETS( fastHaarFeature[i].rect[j].p0,
fastHaarFeature[i].rect[j].p1,
fastHaarFeature[i].rect[j].p2,
fastHaarFeature[i].rect[j].p3,
haarFeature[i].rect[j].r, step )
}
}
}
}
/*
* icvCreateHaarTrainingData
*
* Create haar training data used in stage training
*/
static
CvHaarTrainigData* icvCreateHaarTrainingData( CvSize winsize, int maxnumsamples )
{
CvHaarTrainigData* data;
CV_FUNCNAME( "icvCreateHaarTrainingData" );
__BEGIN__;
data = NULL;
uchar* ptr = NULL;
size_t datasize = 0;
datasize = sizeof( CvHaarTrainigData ) +
/* sum and tilted */
( 2 * (winsize.width + 1) * (winsize.height + 1) * sizeof( sum_type ) +
sizeof( float ) + /* normfactor */
sizeof( float ) + /* cls */
sizeof( float ) /* weight */
) * maxnumsamples;
CV_CALL( data = (CvHaarTrainigData*) cvAlloc( datasize ) );
memset( (void*)data, 0, datasize );
data->maxnum = maxnumsamples;
data->winsize = winsize;
ptr = (uchar*)(data + 1);
data->sum = cvMat( maxnumsamples, (winsize.width + 1) * (winsize.height + 1),
CV_SUM_MAT_TYPE, (void*) ptr );
ptr += sizeof( sum_type ) * maxnumsamples * (winsize.width+1) * (winsize.height+1);
data->tilted = cvMat( maxnumsamples, (winsize.width + 1) * (winsize.height + 1),
CV_SUM_MAT_TYPE, (void*) ptr );
ptr += sizeof( sum_type ) * maxnumsamples * (winsize.width+1) * (winsize.height+1);
data->normfactor = cvMat( 1, maxnumsamples, CV_32FC1, (void*) ptr );
ptr += sizeof( float ) * maxnumsamples;
data->cls = cvMat( 1, maxnumsamples, CV_32FC1, (void*) ptr );
ptr += sizeof( float ) * maxnumsamples;
data->weights = cvMat( 1, maxnumsamples, CV_32FC1, (void*) ptr );
data->valcache = NULL;
data->idxcache = NULL;
__END__;
return data;
}
static
void icvReleaseHaarTrainingDataCache( CvHaarTrainigData** haarTrainingData )
{
if( haarTrainingData != NULL && (*haarTrainingData) != NULL )
{
if( (*haarTrainingData)->valcache != NULL )
{
cvReleaseMat( &(*haarTrainingData)->valcache );
(*haarTrainingData)->valcache = NULL;
}
if( (*haarTrainingData)->idxcache != NULL )
{
cvReleaseMat( &(*haarTrainingData)->idxcache );
(*haarTrainingData)->idxcache = NULL;
}
}
}
static
void icvReleaseHaarTrainingData( CvHaarTrainigData** haarTrainingData )
{
if( haarTrainingData != NULL && (*haarTrainingData) != NULL )
{
icvReleaseHaarTrainingDataCache( haarTrainingData );
cvFree( haarTrainingData );
}
}
static
void icvGetTrainingDataCallback( CvMat* mat, CvMat* sampleIdx, CvMat*,
int first, int num, void* userdata )
{
int i = 0;
int j = 0;
float val = 0.0F;
float normfactor = 0.0F;
CvHaarTrainingData* training_data;
CvIntHaarFeatures* haar_features;
#ifdef CV_COL_ARRANGEMENT
assert( mat->rows >= num );
#else
assert( mat->cols >= num );
#endif
training_data = ((CvUserdata*) userdata)->trainingData;
haar_features = ((CvUserdata*) userdata)->haarFeatures;
if( sampleIdx == NULL )
{
int num_samples;
#ifdef CV_COL_ARRANGEMENT
num_samples = mat->cols;
#else
num_samples = mat->rows;
#endif
for( i = 0; i < num_samples; i++ )
{
for( j = 0; j < num; j++ )
{
val = cvEvalFastHaarFeature(
( haar_features->fastfeature
+ first + j ),
(sum_type*) (training_data->sum.data.ptr
+ i * training_data->sum.step),
(sum_type*) (training_data->tilted.data.ptr
+ i * training_data->tilted.step) );
normfactor = training_data->normfactor.data.fl[i];
val = ( normfactor == 0.0F ) ? 0.0F : (val / normfactor);
#ifdef CV_COL_ARRANGEMENT
CV_MAT_ELEM( *mat, float, j, i ) = val;
#else
CV_MAT_ELEM( *mat, float, i, j ) = val;
#endif
}
}
}
else
{
uchar* idxdata = NULL;
size_t step = 0;
int numidx = 0;
int idx = 0;
assert( CV_MAT_TYPE( sampleIdx->type ) == CV_32FC1 );
idxdata = sampleIdx->data.ptr;
if( sampleIdx->rows == 1 )
{
step = sizeof( float );
numidx = sampleIdx->cols;
}
else
{
step = sampleIdx->step;
numidx = sampleIdx->rows;
}
for( i = 0; i < numidx; i++ )
{
for( j = 0; j < num; j++ )
{
idx = (int)( *((float*) (idxdata + i * step)) );
val = cvEvalFastHaarFeature(
( haar_features->fastfeature
+ first + j ),
(sum_type*) (training_data->sum.data.ptr
+ idx * training_data->sum.step),
(sum_type*) (training_data->tilted.data.ptr
+ idx * training_data->tilted.step) );
normfactor = training_data->normfactor.data.fl[idx];
val = ( normfactor == 0.0F ) ? 0.0F : (val / normfactor);
#ifdef CV_COL_ARRANGEMENT
CV_MAT_ELEM( *mat, float, j, idx ) = val;
#else
CV_MAT_ELEM( *mat, float, idx, j ) = val;
#endif
}
}
}
#if 0 /*def CV_VERBOSE*/
if( first % 5000 == 0 )
{
fprintf( stderr, "%3d%%\r", (int) (100.0 * first /
haar_features->count) );
fflush( stderr );
}
#endif /* CV_VERBOSE */
}
static
void icvPrecalculate( CvHaarTrainingData* data, CvIntHaarFeatures* haarFeatures,
int numprecalculated )
{
CV_FUNCNAME( "icvPrecalculate" );
__BEGIN__;
icvReleaseHaarTrainingDataCache( &data );
numprecalculated -= numprecalculated % CV_STUMP_TRAIN_PORTION;
numprecalculated = MIN( numprecalculated, haarFeatures->count );
if( numprecalculated > 0 )
{
//size_t datasize;
int m;
CvUserdata userdata;
/* private variables */
#ifdef CV_OPENMP
CvMat t_data;
CvMat t_idx;
int first;
int t_portion;
int portion = CV_STUMP_TRAIN_PORTION;
#endif /* CV_OPENMP */
m = data->sum.rows;
#ifdef CV_COL_ARRANGEMENT
CV_CALL( data->valcache = cvCreateMat( numprecalculated, m, CV_32FC1 ) );
#else
CV_CALL( data->valcache = cvCreateMat( m, numprecalculated, CV_32FC1 ) );
#endif
CV_CALL( data->idxcache = cvCreateMat( numprecalculated, m, CV_IDX_MAT_TYPE ) );
userdata = cvUserdata( data, haarFeatures );
#ifdef CV_OPENMP
#pragma omp parallel for private(t_data, t_idx, first, t_portion)
for( first = 0; first < numprecalculated; first += portion )
{
t_data = *data->valcache;
t_idx = *data->idxcache;
t_portion = MIN( portion, (numprecalculated - first) );
/* indices */
t_idx.rows = t_portion;
t_idx.data.ptr = data->idxcache->data.ptr + first * ((size_t)t_idx.step);
/* feature values */
#ifdef CV_COL_ARRANGEMENT
t_data.rows = t_portion;
t_data.data.ptr = data->valcache->data.ptr +
first * ((size_t) t_data.step );
#else
t_data.cols = t_portion;
t_data.data.ptr = data->valcache->data.ptr +
first * ((size_t) CV_ELEM_SIZE( t_data.type ));
#endif
icvGetTrainingDataCallback( &t_data, NULL, NULL, first, t_portion,
&userdata );
#ifdef CV_COL_ARRANGEMENT
cvGetSortedIndices( &t_data, &t_idx, 0 );
#else
cvGetSortedIndices( &t_data, &t_idx, 1 );
#endif
#ifdef CV_VERBOSE
putc( '.', stderr );
fflush( stderr );
#endif /* CV_VERBOSE */
}
#ifdef CV_VERBOSE
fprintf( stderr, "\n" );
fflush( stderr );
#endif /* CV_VERBOSE */
#else
icvGetTrainingDataCallback( data->valcache, NULL, NULL, 0, numprecalculated,
&userdata );
#ifdef CV_COL_ARRANGEMENT
cvGetSortedIndices( data->valcache, data->idxcache, 0 );
#else
cvGetSortedIndices( data->valcache, data->idxcache, 1 );
#endif
#endif /* CV_OPENMP */
}
__END__;
}
static
void icvSplitIndicesCallback( int compidx, float threshold,
CvMat* idx, CvMat** left, CvMat** right,
void* userdata )
{
CvHaarTrainingData* data;
CvIntHaarFeatures* haar_features;
int i;
int m;
CvFastHaarFeature* fastfeature;
data = ((CvUserdata*) userdata)->trainingData;
haar_features = ((CvUserdata*) userdata)->haarFeatures;
fastfeature = &haar_features->fastfeature[compidx];
m = data->sum.rows;
*left = cvCreateMat( 1, m, CV_32FC1 );
*right = cvCreateMat( 1, m, CV_32FC1 );
(*left)->cols = (*right)->cols = 0;
if( idx == NULL )
{
for( i = 0; i < m; i++ )
{
if( cvEvalFastHaarFeature( fastfeature,
(sum_type*) (data->sum.data.ptr + i * data->sum.step),
(sum_type*) (data->tilted.data.ptr + i * data->tilted.step) )
< threshold * data->normfactor.data.fl[i] )
{
(*left)->data.fl[(*left)->cols++] = (float) i;
}
else
{
(*right)->data.fl[(*right)->cols++] = (float) i;
}
}
}
else
{
uchar* idxdata;
int idxnum;
size_t idxstep;
int index;
idxdata = idx->data.ptr;
idxnum = (idx->rows == 1) ? idx->cols : idx->rows;
idxstep = (idx->rows == 1) ? CV_ELEM_SIZE( idx->type ) : idx->step;
for( i = 0; i < idxnum; i++ )
{
index = (int) *((float*) (idxdata + i * idxstep));
if( cvEvalFastHaarFeature( fastfeature,
(sum_type*) (data->sum.data.ptr + index * data->sum.step),
(sum_type*) (data->tilted.data.ptr + index * data->tilted.step) )
< threshold * data->normfactor.data.fl[index] )
{
(*left)->data.fl[(*left)->cols++] = (float) index;
}
else
{
(*right)->data.fl[(*right)->cols++] = (float) index;
}
}
}
}
/*
* icvCreateCARTStageClassifier
*
* Create stage classifier with trees as weak classifiers
* data - haar training data. It must be created and filled before call
* minhitrate - desired min hit rate
* maxfalsealarm - desired max false alarm rate
* symmetric - if not 0 it is assumed that samples are vertically symmetric
* numprecalculated - number of features that will be precalculated. Each precalculated
* feature need (number_of_samples*(sizeof( float ) + sizeof( short ))) bytes of memory
* weightfraction - weight trimming parameter
* numsplits - number of binary splits in each tree
* boosttype - type of applied boosting algorithm
* stumperror - type of used error if Discrete AdaBoost algorithm is applied
* maxsplits - maximum total number of splits in all weak classifiers.
* If it is not 0 then NULL returned if total number of splits exceeds <maxsplits>.
*/
static
CvIntHaarClassifier* icvCreateCARTStageClassifier( CvHaarTrainingData* data,
CvMat* sampleIdx,
CvIntHaarFeatures* haarFeatures,
float minhitrate,
float maxfalsealarm,
int symmetric,
float weightfraction,
int numsplits,
CvBoostType boosttype,
CvStumpError stumperror,
int maxsplits )
{
#ifdef CV_COL_ARRANGEMENT
int flags = CV_COL_SAMPLE;
#else
int flags = CV_ROW_SAMPLE;
#endif
CvStageHaarClassifier* stage = NULL;
CvBoostTrainer* trainer;
CvCARTClassifier* cart = NULL;
CvCARTTrainParams trainParams;
CvMTStumpTrainParams stumpTrainParams;
//CvMat* trainData = NULL;
//CvMat* sortedIdx = NULL;
CvMat eval;
int n = 0;
int m = 0;
int numpos = 0;
int numneg = 0;
int numfalse = 0;
float sum_stage = 0.0F;
float threshold = 0.0F;
float falsealarm = 0.0F;
//CvMat* sampleIdx = NULL;
CvMat* trimmedIdx;
//float* idxdata = NULL;
//float* tempweights = NULL;
//int idxcount = 0;
CvUserdata userdata;
int i = 0;
int j = 0;
int idx;
int numsamples;
int numtrimmed;
CvCARTHaarClassifier* classifier;
CvSeq* seq = NULL;
CvMemStorage* storage = NULL;
CvMat* weakTrainVals;
float alpha;
float sumalpha;
int num_splits; /* total number of splits in all weak classifiers */
#ifdef CV_VERBOSE
printf( "+----+----+-+---------+---------+---------+---------+\n" );
printf( "| N |%%SMP|F| ST.THR | HR | FA | EXP. ERR|\n" );
printf( "+----+----+-+---------+---------+---------+---------+\n" );
#endif /* CV_VERBOSE */
n = haarFeatures->count;
m = data->sum.rows;
numsamples = (sampleIdx) ? MAX( sampleIdx->rows, sampleIdx->cols ) : m;
userdata = cvUserdata( data, haarFeatures );
stumpTrainParams.type = ( boosttype == CV_DABCLASS )
? CV_CLASSIFICATION_CLASS : CV_REGRESSION;
stumpTrainParams.error = ( boosttype == CV_LBCLASS || boosttype == CV_GABCLASS )
? CV_SQUARE : stumperror;
stumpTrainParams.portion = CV_STUMP_TRAIN_PORTION;
stumpTrainParams.getTrainData = icvGetTrainingDataCallback;
stumpTrainParams.numcomp = n;
stumpTrainParams.userdata = &userdata;
stumpTrainParams.sortedIdx = data->idxcache;
trainParams.count = numsplits;
trainParams.stumpTrainParams = (CvClassifierTrainParams*) &stumpTrainParams;
trainParams.stumpConstructor = cvCreateMTStumpClassifier;
trainParams.splitIdx = icvSplitIndicesCallback;
trainParams.userdata = &userdata;
eval = cvMat( 1, m, CV_32FC1, cvAlloc( sizeof( float ) * m ) );
storage = cvCreateMemStorage();
seq = cvCreateSeq( 0, sizeof( *seq ), sizeof( classifier ), storage );
weakTrainVals = cvCreateMat( 1, m, CV_32FC1 );
trainer = cvBoostStartTraining( &data->cls, weakTrainVals, &data->weights,
sampleIdx, boosttype );
num_splits = 0;
sumalpha = 0.0F;
do
{
#ifdef CV_VERBOSE
int v_wt = 0;
int v_flipped = 0;
#endif /* CV_VERBOSE */
trimmedIdx = cvTrimWeights( &data->weights, sampleIdx, weightfraction );
numtrimmed = (trimmedIdx) ? MAX( trimmedIdx->rows, trimmedIdx->cols ) : m;
#ifdef CV_VERBOSE
v_wt = 100 * numtrimmed / numsamples;
v_flipped = 0;
#endif /* CV_VERBOSE */
cart = (CvCARTClassifier*) cvCreateCARTClassifier( data->valcache,
flags,
weakTrainVals, 0, 0, 0, trimmedIdx,
&(data->weights),
(CvClassifierTrainParams*) &trainParams );
classifier = (CvCARTHaarClassifier*) icvCreateCARTHaarClassifier( numsplits );
icvInitCARTHaarClassifier( classifier, cart, haarFeatures );
num_splits += classifier->count;
cart->release( (CvClassifier**) &cart );
if( symmetric && (seq->total % 2) )
{
float normfactor = 0.0F;
CvStumpClassifier* stump;
/* flip haar features */
for( i = 0; i < classifier->count; i++ )
{
if( classifier->feature[i].desc[0] == 'h' )
{
for( j = 0; j < CV_HAAR_FEATURE_MAX &&
classifier->feature[i].rect[j].weight != 0.0F; j++ )
{
classifier->feature[i].rect[j].r.x = data->winsize.width -
classifier->feature[i].rect[j].r.x -
classifier->feature[i].rect[j].r.width;
}
}
else
{
int tmp = 0;
/* (x,y) -> (24-x,y) */
/* w -> h; h -> w */
for( j = 0; j < CV_HAAR_FEATURE_MAX &&
classifier->feature[i].rect[j].weight != 0.0F; j++ )
{
classifier->feature[i].rect[j].r.x = data->winsize.width -
classifier->feature[i].rect[j].r.x;
CV_SWAP( classifier->feature[i].rect[j].r.width,
classifier->feature[i].rect[j].r.height, tmp );
}
}
}
icvConvertToFastHaarFeature( classifier->feature,
classifier->fastfeature,
classifier->count, data->winsize.width + 1 );
stumpTrainParams.getTrainData = NULL;
stumpTrainParams.numcomp = 1;
stumpTrainParams.userdata = NULL;
stumpTrainParams.sortedIdx = NULL;
for( i = 0; i < classifier->count; i++ )
{
for( j = 0; j < numtrimmed; j++ )
{
idx = icvGetIdxAt( trimmedIdx, j );