-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdaBoost.cpp
1049 lines (856 loc) · 41.4 KB
/
AdaBoost.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
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
#include "mainwindow.h"
#include "math.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <iostream>
#include "FeatureDetection.h"
#include "bilinearinterpolationClass.h"
#include "bestclassifier.h"
BilinearInterpolationClass* bilinearInterpolation = NULL;
BestClassifier* bestClassifierClass = NULL;
FILE *featureValue = fopen("featureValue.txt", "w");
/*******************************************************************************
The following are helper routines with code already written.
The routines you'll need to write for the assignment are below.
*******************************************************************************/
/*******************************************************************************
Open the training dataset
posdirectory - Directory containing face images
negdirectory - Directory containing non-face images
trainingData - Array used to store the data
trainingLabel - Label assigned to training data (1 = face, 0 = non-face)
numTrainingExamples - Number of training examples
patchSize - Size of training patches
*******************************************************************************/
void MainWindow::OpenDataSet(QDir posdirectory, QDir negdirectory, double *trainingData, int *trainingLabel, int numTrainingExamples, int patchSize)
{
int i, c, r;
QStringList imgNames;
QImage inImage;
QRgb pixel;
imgNames = posdirectory.entryList();
int idx = 0;
for(i=0;i<imgNames.length();i++)
if(idx < numTrainingExamples/2)
{
// use "\\" for windows machine
inImage.load(posdirectory.absolutePath() + "/" + imgNames.at(i));
if(!(inImage.isNull()))
{
for(r=0;r<patchSize;r++)
for(c=0;c<patchSize;c++)
{
pixel = inImage.pixel(c, r);
trainingData[idx*patchSize*patchSize + r*patchSize + c] = (double) qGreen(pixel);
}
trainingLabel[idx] = 1;
idx++;
}
}
imgNames = negdirectory.entryList();
for(i=0;i<imgNames.length();i++)
if(idx < numTrainingExamples)
{
// use "\\" for windows machine
inImage.load(negdirectory.absolutePath() + "/" + imgNames.at(i));
if(!(inImage.isNull()))
{
for(r=0;r<patchSize;r++)
for(c=0;c<patchSize;c++)
{
pixel = inImage.pixel(c, r);
trainingData[idx*patchSize*patchSize + r*patchSize + c] = (double) qGreen(pixel);
}
trainingLabel[idx] = 0;
idx++;
}
}
}
/*******************************************************************************
DisplayTrainingDataset - Display example patches from training dataset
displayImage - Display image
trainingData - Array used to store the data
trainingLabel - Label assigned to training data (1 = face, 0 = non-face)
numTrainingExamples - Number of training examples
patchSize - Size of training patches
*******************************************************************************/
void MainWindow::DisplayTrainingDataset(QImage *displayImage, double *trainingData, int *trainingLabel, int numTrainingExamples, int patchSize)
{
int w = displayImage->width();
int h = displayImage->height();
int r, c;
int rOffset = 0;
int cOffset = 0;
bool inBounds = true;
int ct = 0;
while(inBounds)
{
int idx = rand()%numTrainingExamples;
for(r=0;r<patchSize;r++)
for(c=0;c<patchSize;c++)
{
if(trainingLabel[idx] == 1)
{
int val = (int) trainingData[idx*patchSize*patchSize + r*patchSize + c];
displayImage->setPixel(c + cOffset, r + rOffset, qRgb(val, val, val));
}
else
{
int val = (int) trainingData[idx*patchSize*patchSize + r*patchSize + c];
displayImage->setPixel(c + cOffset, r + rOffset, qRgb(val, val, val));
}
}
cOffset += patchSize;
if(cOffset + patchSize >= w)
{
cOffset = 0;
rOffset += patchSize;
if(rOffset + patchSize >= h)
inBounds = false;
}
ct++;
}
}
/*******************************************************************************
SaveClassifier - Save the computed AdaBoost classifier
fileName - Name of file
*******************************************************************************/
void MainWindow::SaveClassifier(QString fileName)
{
int i, j;
FILE *out;
out = fopen(fileName.toLatin1(), "w");
fprintf(out, "%d\n", m_NumWeakClassifiers);
for(i=0;i<m_NumWeakClassifiers;i++)
{
fprintf(out, "%d\n", m_WeakClassifiers[i].m_NumBoxes);
for(j=0;j<m_WeakClassifiers[i].m_NumBoxes;j++)
fprintf(out, "%lf\t%lf\t%lf\t%lf\t%lf\n", m_WeakClassifiers[i].m_BoxSign[j], m_WeakClassifiers[i].m_Box[j][0][0], m_WeakClassifiers[i].m_Box[j][0][1],
m_WeakClassifiers[i].m_Box[j][1][0], m_WeakClassifiers[i].m_Box[j][1][1]);
fprintf(out, "%lf\n", m_WeakClassifiers[i].m_Area);
fprintf(out, "%lf\n", m_WeakClassifiers[i].m_Polarity);
fprintf(out, "%lf\n", m_WeakClassifiers[i].m_Threshold);
fprintf(out, "%lf\n", m_WeakClassifiers[i].m_Weight);
}
fclose(out);
}
/*******************************************************************************
OpenClassifier - Open the computed AdaBoost classifier
fileName - Name of file
*******************************************************************************/
void MainWindow::OpenClassifier(QString fileName)
{
int i, j;
FILE *in;
in = fopen(fileName.toLatin1(), "r");
fscanf(in, "%d\n", &m_NumWeakClassifiers);
m_WeakClassifiers = new CWeakClassifiers [m_NumWeakClassifiers];
for(i=0;i<m_NumWeakClassifiers;i++)
{
fscanf(in, "%d\n", &(m_WeakClassifiers[i].m_NumBoxes));
m_WeakClassifiers[i].m_Box = new double [m_WeakClassifiers[i].m_NumBoxes][2][2];
m_WeakClassifiers[i].m_BoxSign = new double [m_WeakClassifiers[i].m_NumBoxes];
for(j=0;j<m_WeakClassifiers[i].m_NumBoxes;j++)
fscanf(in, "%lf\t%lf\t%lf\t%lf\t%lf\n", &(m_WeakClassifiers[i].m_BoxSign[j]), &(m_WeakClassifiers[i].m_Box[j][0][0]), &(m_WeakClassifiers[i].m_Box[j][0][1]),
&(m_WeakClassifiers[i].m_Box[j][1][0]), &(m_WeakClassifiers[i].m_Box[j][1][1]));
fscanf(in, "%lf\n", &(m_WeakClassifiers[i].m_Area));
fscanf(in, "%lf\n", &(m_WeakClassifiers[i].m_Polarity));
fscanf(in, "%lf\n", &(m_WeakClassifiers[i].m_Threshold));
fscanf(in, "%lf\n", &(m_WeakClassifiers[i].m_Weight));
}
fclose(in);
}
/*******************************************************************************
DisplayClassifiers - Display the Haar wavelets for the classifier
displayImage - Display image
weakClassifiers - The weak classifiers used in AdaBoost
numWeakClassifiers - Number of weak classifiers
*******************************************************************************/
void MainWindow::DisplayClassifiers(QImage *displayImage, CWeakClassifiers *weakClassifiers, int numWeakClassifiers)
{
int w = displayImage->width();
int h = displayImage->height();
int i, j, r, c;
int rOffset = 0;
int cOffset = 0;
int size = 50;
bool inBounds = true;
displayImage->fill(qRgb(0,0,0));
for(i=0;i<numWeakClassifiers & inBounds;i++)
{
for(r=0;r<size;r++)
for(c=0;c<size;c++)
{
displayImage->setPixel(c + cOffset, r + rOffset, qRgb(128, 128, 128));
}
for(j=0;j<weakClassifiers[i].m_NumBoxes;j++)
for(r=(int) ((double) size*weakClassifiers[i].m_Box[j][0][1]);r<(int) ((double) size*weakClassifiers[i].m_Box[j][1][1]);r++)
for(c=(int) ((double) size*weakClassifiers[i].m_Box[j][0][0]);c<(int) ((double) size*weakClassifiers[i].m_Box[j][1][0]);c++)
{
if(weakClassifiers[i].m_BoxSign[j] > 0.0)
displayImage->setPixel(c + cOffset, r + rOffset, qRgb(255, 255, 255));
else
displayImage->setPixel(c + cOffset, r + rOffset, qRgb(0, 0, 0));
}
cOffset += size+1;
if(cOffset + size >= w)
{
cOffset = 0;
rOffset += size + 1;
if(rOffset + size >= h)
inBounds = false;
}
}
}
/*******************************************************************************
DisplayIntegralImage - Display the integral image
displayImage - Display image
integralImage - Output integral image
w, h - Width and height of image
*******************************************************************************/
void MainWindow::DisplayIntegralImage(QImage *displayImage, double *integralImage, int w, int h)
{
int r, c;
double maxVal = integralImage[(h-1)*w + w-1];
for(r=0;r<h;r++)
for(c=0;c<w;c++)
{
int val = (int) (255.0*integralImage[r*w + c]/maxVal);
displayImage->setPixel(c, r, qRgb(val, val, val));
}
}
/*******************************************************************************
InitializeFeatures - Randomly initialize the candidate weak classifiers
weakClassifiers - Candidate weak classifiers
numWeakClassifiers - Number of candidate weak classifiers
*******************************************************************************/
void MainWindow::InitializeFeatures(CWeakClassifiers *weakClassifiers, int numWeakClassifiers)
{
int i;
for(i=0;i<numWeakClassifiers;i++)
{
double x, y, w, h;
// We don't know these values yet, so just initialize to 0
weakClassifiers[i].m_Polarity = 0.0;
weakClassifiers[i].m_Threshold = 0.0;
weakClassifiers[i].m_Weight = 0.0;
// The Haar wavelet's corners can range in the area of 0.02 to 0.98, with a minimum size of 0.25
// We limit the range to [0.2, 0.98], instead of [0, 1] so we don't need to worry about checking
// out of bounds errors later on, i.e. in the BilinearInterpolation function.
// x position of box and width
w = 0.25 + 0.71*(double) rand()/(double) RAND_MAX;
x = 0.02 + (0.96 - w)*(double) rand()/(double) RAND_MAX;
// y position of box and height
h = 0.25 + 0.71*(double) rand()/(double) RAND_MAX;
y = 0.02 + (0.96 - h)*(double) rand()/(double) RAND_MAX;
int boxType = rand()%4;
if(boxType == 0)
{
// Vertical boxes
weakClassifiers[i].m_NumBoxes = 2;
weakClassifiers[i].m_Box = new double [weakClassifiers[i].m_NumBoxes][2][2];
weakClassifiers[i].m_BoxSign = new double [weakClassifiers[i].m_NumBoxes];
weakClassifiers[i].m_BoxSign[0] = 1.0;
weakClassifiers[i].m_Box[0][0][0] = x;
weakClassifiers[i].m_Box[0][0][1] = y;
weakClassifiers[i].m_Box[0][1][0] = x + w/2;
weakClassifiers[i].m_Box[0][1][1] = y + h;
weakClassifiers[i].m_BoxSign[1] = -1.0;
weakClassifiers[i].m_Box[1][0][0] = x + w/2;
weakClassifiers[i].m_Box[1][0][1] = y;
weakClassifiers[i].m_Box[1][1][0] = x + w;
weakClassifiers[i].m_Box[1][1][1] = y + h;
}
if(boxType == 1)
{
// 2 Horizontal boxes
weakClassifiers[i].m_NumBoxes = 2;
weakClassifiers[i].m_Box = new double [weakClassifiers[i].m_NumBoxes][2][2];
weakClassifiers[i].m_BoxSign = new double [weakClassifiers[i].m_NumBoxes];
weakClassifiers[i].m_BoxSign[0] = 1.0;
weakClassifiers[i].m_Box[0][0][0] = x;
weakClassifiers[i].m_Box[0][0][1] = y;
weakClassifiers[i].m_Box[0][1][0] = x + w;
weakClassifiers[i].m_Box[0][1][1] = y + h/2;
weakClassifiers[i].m_BoxSign[1] = -1.0;
weakClassifiers[i].m_Box[1][0][0] = x;
weakClassifiers[i].m_Box[1][0][1] = y + h/2;
weakClassifiers[i].m_Box[1][1][0] = x + w;
weakClassifiers[i].m_Box[1][1][1] = y + h;
}
if(boxType == 2)
{
// 3 Vertical boxes
weakClassifiers[i].m_NumBoxes = 3;
weakClassifiers[i].m_Box = new double [weakClassifiers[i].m_NumBoxes][2][2];
weakClassifiers[i].m_BoxSign = new double [weakClassifiers[i].m_NumBoxes];
weakClassifiers[i].m_BoxSign[0] = 1.0;
weakClassifiers[i].m_Box[0][0][0] = x;
weakClassifiers[i].m_Box[0][0][1] = y;
weakClassifiers[i].m_Box[0][1][0] = x + w/3;
weakClassifiers[i].m_Box[0][1][1] = y + h;
weakClassifiers[i].m_BoxSign[1] = -2.0;
weakClassifiers[i].m_Box[1][0][0] = x + w/3;
weakClassifiers[i].m_Box[1][0][1] = y;
weakClassifiers[i].m_Box[1][1][0] = x + 2*w/3;
weakClassifiers[i].m_Box[1][1][1] = y + h;
weakClassifiers[i].m_BoxSign[2] = 1.0;
weakClassifiers[i].m_Box[2][0][0] = x + 2*w/3;
weakClassifiers[i].m_Box[2][0][1] = y;
weakClassifiers[i].m_Box[2][1][0] = x + w;
weakClassifiers[i].m_Box[2][1][1] = y + h;
}
if(boxType == 4)
{
// 4 diagonal boxes
weakClassifiers[i].m_NumBoxes = 4;
weakClassifiers[i].m_Box = new double [weakClassifiers[i].m_NumBoxes][2][2];
weakClassifiers[i].m_BoxSign = new double [weakClassifiers[i].m_NumBoxes];
weakClassifiers[i].m_BoxSign[0] = 1.0;
weakClassifiers[i].m_Box[0][0][0] = x;
weakClassifiers[i].m_Box[0][0][1] = y;
weakClassifiers[i].m_Box[0][1][0] = x + w/2;
weakClassifiers[i].m_Box[0][1][1] = y + h/2;
weakClassifiers[i].m_BoxSign[1] = -1.0;
weakClassifiers[i].m_Box[1][0][0] = x + w/2;
weakClassifiers[i].m_Box[1][0][1] = y;
weakClassifiers[i].m_Box[1][1][0] = x + w;
weakClassifiers[i].m_Box[1][1][1] = y + h/2;
weakClassifiers[i].m_BoxSign[2] = 1.0;
weakClassifiers[i].m_Box[2][0][0] = x ;
weakClassifiers[i].m_Box[2][0][1] = y + h/2;
weakClassifiers[i].m_Box[2][1][0] = x + w/2;
weakClassifiers[i].m_Box[2][1][1] = y + h;
weakClassifiers[i].m_BoxSign[3] = -1.0;
weakClassifiers[i].m_Box[3][0][0] = x + w/2;
weakClassifiers[i].m_Box[3][0][1] = y + h/2;
weakClassifiers[i].m_Box[3][1][0] = x + w;
weakClassifiers[i].m_Box[3][1][1] = y + h;
}
weakClassifiers[i].m_Area = w*h;
}
}
/*******************************************************************************
ConvertColorToDouble - Simple helper function to convert from RGB to double
image - Input image
dImage - Output double image
w, h - Image width and height
*******************************************************************************/
void MainWindow::ConvertColorToDouble(QImage image, double *dImage, int w, int h)
{
QRgb pixel;
int r, c;
for(r=0;r<h;r++)
for(c=0;c<w;c++)
{
pixel = image.pixel(c, r);
dImage[r*w + c] = qGreen(pixel);
}
}
/*******************************************************************************
ComputeTrainingSetFeatures - Compute all of the features for the training dataset
trainingData - Array used to store the data
features - Array holding feature values
numTrainingExamples - Number of training examples
patchSize - Size of training patches
weakClassifiers - Candidate weak classifiers
numWeakClassifiers - Number of candidate weak classifiers
*******************************************************************************/
void MainWindow::ComputeTrainingSetFeatures(double *trainingData, double *features,
int numTrainingExamples, int patchSize, CWeakClassifiers *weakClassifiers, int numWeakClassifiers)
{
int i;
double *integralImage = new double [patchSize*patchSize];
for(i=0;i<numTrainingExamples;i++)
{
// Compute features for training examples
// First compute the integral image for each patch
IntegralImage(&(trainingData[i*patchSize*patchSize]), integralImage, patchSize, patchSize);
// Compute the Haar wavelets
ComputeFeatures(integralImage, 0, 0, patchSize, &(features[i*numWeakClassifiers]), weakClassifiers, numWeakClassifiers, patchSize);
}
// We shouldn't need the training data anymore so let's delete it.
delete [] trainingData;
delete [] integralImage;
fclose(featureValue);
}
/*******************************************************************************
DisplayFeatures - Display the computed features (green = faces, red = background)
displayImage - Display image
features - Array holding feature values
trainingLabel - Label assigned to training data (1 = face, 0 = non-face)
numFeatures - Number of features
numTrainingExamples - Number of training examples
*******************************************************************************/
void MainWindow::DisplayFeatures(QImage *displayImage, double *features, int *trainingLabel, int numFeatures, int numTrainingExamples)
{
int r, c;
int w = displayImage->width();
int h = displayImage->height();
int posCt = 0;
int negCt = 0;
double mean = 0.0;
double meanCt = 0.0;
for(r=0;r<numTrainingExamples;r+=10)
{
for(c=0;c<numFeatures;c++)
{
mean += fabs(features[r*numFeatures + c]);
meanCt++;
}
}
mean /= meanCt;
for(r=0;r<numTrainingExamples;r++)
{
if(trainingLabel[r] == 1 && posCt < h/2)
{
for(c=0;c<numFeatures;c++)
if(c < w)
{
int val = 255.0*(features[r*numFeatures + c]/(4.0*mean)) + 128.0;
val = min(255, max(0, val));
displayImage->setPixel(c, posCt, qRgb(0, val, 0));
}
posCt++;
}
if(trainingLabel[r] == 0 && negCt < h/2)
{
for(c=0;c<numFeatures;c++)
if(c < w)
{
int val = 255.0*(features[r*numFeatures + c]/(4.0*mean)) + 128.0;
val = min(255, max(0, val));
displayImage->setPixel(c, negCt + h/2, qRgb(val, 0, 0));
}
negCt++;
}
}
}
/*******************************************************************************
AdaBoost - Computes and AdaBoost classifier using a set of candidate weak classifiers
features - Array of feature values pre-computed for the training dataset
trainingLabel - Ground truth labels for the training examples (1 = face, 0 = background)
numTrainingExamples - Number of training examples
candidateWeakClassifiers - Set of candidate weak classifiers
numCandidateWeakClassifiers - Number of candidate weak classifiers
weakClassifiers - Set of weak classifiers selected by AdaBoost
numWeakClassifiers - Number of selected weak classifiers
*******************************************************************************/
void MainWindow::AdaBoost(double *features, int *trainingLabel, int numTrainingExamples,
CWeakClassifiers *candidateWeakClassifiers, int numCandidateWeakClassifiers, CWeakClassifiers *weakClassifiers, int numWeakClassifiers)
{
FILE *out;
out = fopen("AdaBoost.txt", "w");
double *scores = new double [numTrainingExamples];
double weightSum = 0.0;
int *featureSortIdx = new int [numTrainingExamples*numCandidateWeakClassifiers];
double *featureTranspose = new double [numTrainingExamples*numCandidateWeakClassifiers];
// Record the classification socres for each training example
memset(scores, 0, numTrainingExamples*sizeof(double));
int i, j;
// The weighting for each training example
double *dataWeights = new double [numTrainingExamples];
// Begin with uniform weighting
for(i=0;i<numTrainingExamples;i++)
dataWeights[i] = 1.0/(double) (numTrainingExamples);
// Let's sort the feature values for each candidate weak classifier
for(i=0;i<numCandidateWeakClassifiers;i++)
{
QMap<double, int> featureSort;
QMap<double, int>::const_iterator iterator;
for(j=0;j<numTrainingExamples;j++)
{
featureSort.insertMulti(features[j*numCandidateWeakClassifiers + i], j);
// For ease later on we'll store a transposed version of the feature array
featureTranspose[i*numTrainingExamples + j] = features[j*numCandidateWeakClassifiers + i];
}
j = 0;
iterator = featureSort.constBegin();
// Let's remember the indices of the sorted features for later.
while (iterator != featureSort.constEnd())
{
featureSortIdx[i*numTrainingExamples + j] = iterator.value();
iterator++;
j++;
}
}
// We shouldn't need the features anymore so let's delete it.
delete [] features;
// Find a set of weak classifiers using AdaBoost
for(i=0;i<numWeakClassifiers;i++)
{
double bestError = 99999.0;
int bestIdx = 0;
// For each potential weak classifier
for(j=0;j<numCandidateWeakClassifiers;j++)
{
CWeakClassifiers bestClassifier;
// Find the best threshold, polarity and weight for the candidate weak classifier
double error = FindBestClassifier(&(featureSortIdx[j*numTrainingExamples]),
&(featureTranspose[j*numTrainingExamples]),
trainingLabel, dataWeights, numTrainingExamples,
candidateWeakClassifiers[j], &bestClassifier);
// Is this the best classifier found so far?
if(error < bestError)
{
bestError = error;
bestIdx = j;
// Remember the best classifier
bestClassifier.copy(&(weakClassifiers[i]));
}
}
// Given the best weak classifier found, update the weighting of the training data.
UpdateDataWeights(&(featureTranspose[bestIdx*numTrainingExamples]), trainingLabel, weakClassifiers[i], dataWeights, numTrainingExamples);
// Let's compute the current error for the training dataset
weightSum += weakClassifiers[i].m_Weight;
double error = 0.0;
for(j=0;j<numTrainingExamples;j++)
{
if(featureTranspose[bestIdx*numTrainingExamples + j] > weakClassifiers[i].m_Threshold)
{
scores[j] += weakClassifiers[i].m_Weight*weakClassifiers[i].m_Polarity;
}
else
{
scores[j] += weakClassifiers[i].m_Weight*(1.0 - weakClassifiers[i].m_Polarity);
}
if((scores[j] > 0.5*weightSum && trainingLabel[j] == 0) ||
(scores[j] < 0.5*weightSum && trainingLabel[j] == 1))
error++;
}
// Output information that you might find useful for debugging
fprintf(out, "Count: %d\tIdx: %d\tWeight: %lf\tError: %lf\n", i, bestIdx,
weakClassifiers[i].m_Weight, error/(double) numTrainingExamples);
fflush(out);
}
delete [] dataWeights;
delete [] scores;
delete [] featureSortIdx;
delete [] featureTranspose;
fclose(out);
// bestClassifierClass->release();
}
/*******************************************************************************
FindFaces - Find faces in an image
weakClassifiers - Set of weak classifiers
numWeakClassifiers - Number of weak classifiers
threshold - Classifier must be above Threshold to return detected face.
minScale, maxScale - Minimum and maximum scale to search for faces.
faceDetections - Set of face detections
displayImage - Display image showing detected faces.
*******************************************************************************/
void MainWindow::FindFaces(QImage inImage, CWeakClassifiers *weakClassifiers, int numWeakClassifiers, double threshold, double minScale, double maxScale,
QMap<double, CDetection> *faceDetections, QImage *displayImage)
{
int w = inImage.width();
int h = inImage.height();
double *integralImage = new double [w*h];
double *dImage = new double [w*h];
double scaleMulti = 1.26;
double scale;
int r, c;
ConvertColorToDouble(inImage, dImage, w, h);
// Compute the integral image
IntegralImage(dImage, integralImage, w, h);
// Serach in scale space
for(scale=minScale;scale<maxScale;scale*=scaleMulti)
{
// Find size of bounding box, and the step size between neighboring bounding boxes.
int faceSize = (int) scale;
int stepSize = max(2, faceSize/8);
// For every possible position
for(r=0;r<h-faceSize;r+=stepSize)
for(c=0;c<w-faceSize;c+=stepSize)
{
// Compute the score of the classifier
double score = ClassifyBox(integralImage, c, r, faceSize, weakClassifiers, numWeakClassifiers, w);
// Is the score above threshold?
if(score > threshold)
{
CDetection detection;
detection.m_Score = score;
detection.m_Scale = scale;
detection.m_X = (double) c;
detection.m_Y = (double) r;
// Remember the detection
faceDetections->insertMulti(score, detection);
}
}
}
// Draw face bounding boxes
DrawFace(displayImage, faceDetections);
delete [] dImage;
delete [] integralImage;
}
/*******************************************************************************
DrawFace - Draw the detected faces.
displayImage - Display image
faceDetections - Set of face detections
*******************************************************************************/
void MainWindow::DrawFace(QImage *displayImage, QMap<double, CDetection> *faceDetections)
{
int r, c;
QMap<double, CDetection>::const_iterator iterator = faceDetections->constBegin();
while(iterator != faceDetections->constEnd())
{
CDetection detection = iterator.value();
int c0 = (int) detection.m_X;
int r0 = (int) detection.m_Y;
int size = (int) detection.m_Scale;
for(r=r0;r<r0+size;r++)
displayImage->setPixel(c0, r, qRgb(255, 0, 0));
for(r=r0;r<r0+size;r++)
displayImage->setPixel(c0 + size, r, qRgb(255, 0, 0));
for(c=c0;c<c0+size;c++)
displayImage->setPixel(c, r0, qRgb(255, 0, 0));
for(c=c0;c<c0+size;c++)
displayImage->setPixel(c, r0 + size, qRgb(255, 0, 0));
iterator++;
}
}
/*******************************************************************************
*******************************************************************************
*******************************************************************************
The routines you need to implement are below
*******************************************************************************
*******************************************************************************
*******************************************************************************/
/*******************************************************************************int *featureSortIdx, double *features,
DisplayAverageFace - Display the average face and non-face image
displayImage - Display image, draw the average images on this image
trainingData - Array used to store the data
trainingLabel - Label assigned to training data (1 = face, 0 = non-face)
numTrainingExamples - Number of training examples
patchSize - Size of training patches in one dimension (patches have patchSize*patchSize pixels)
*******************************************************************************/
void MainWindow::DisplayAverageFace(QImage *displayImage, double *trainingData, int *trainingLabel, int numTrainingExamples, int patchSize)
{
// Add your code here.
FeatureDetection* featureDetection = new FeatureDetection();
featureDetection->GetAveragePatch(trainingData, trainingLabel, numTrainingExamples, patchSize);
featureDetection->DisplayAverageImage(displayImage);
}
/*******************************************************************************
IntegralImage - Compute the integral image
image - Input double image
integralImage - Output integral image
w, h - Width and height of image
*******************************************************************************/
void MainWindow::IntegralImage(double *image, double *integralImage, int width, int height)
{
// Add your code here.
// every pixel should be sum of pixel to it upper left.
double** SumOfColumns = new double*[height];
for(int row = 0; row < height; row++)
SumOfColumns[row] = new double[width+1];
for(int row = 0; row < height; row++)
SumOfColumns[row][0] = 0.0;
for(int colPixel = 1; colPixel <= width; colPixel++) {
SumOfColumns[0][colPixel] = image[0*width + colPixel] + SumOfColumns[0][colPixel-1];
integralImage[0*width + colPixel-1] = SumOfColumns[0][colPixel];
}
for(int rowPixel = 1; rowPixel < height; rowPixel++ )
for(int colPixel = 0; colPixel < width; colPixel++) {
SumOfColumns[rowPixel][colPixel+1] = image[rowPixel*width + colPixel] + SumOfColumns[rowPixel][colPixel];
integralImage[rowPixel*width + colPixel] = integralImage[(rowPixel-1)* width + colPixel] + SumOfColumns[rowPixel][colPixel+1];
}
for(int row = 0; row < height; row++)
delete[] SumOfColumns[row];
}
/*******************************************************************************
SumBox - Helper function for SumBox - standard bilinear interpolation
image - image
x, y - Position to interpolate
w - Width of image (integralImage)
*******************************************************************************/
double MainWindow::BilinearInterpolation(double *image, double colPixel, double rowPixel, int width)
{
// Add your code here (or cut and paste from a previous assignment.)
return(bilinearInterpolation->GetBilinearInterpolatedPixelValue(colPixel, rowPixel));
}
/*******************************************************************************
SumBox - Helper function for ComputeFeatures - compute the sum of the pixels within a box.
integralImage - integral image
x0, y0 - Upper lefthand corner of box
x1, y1 - Lower righthand corner of box
w - Width of image (integralImage)
*******************************************************************************/
double MainWindow::SumBox(double *integralImage, double upperLeftCol, double upperLeftRow, double bottomRightCol, double bottomRightRow, int width)
{
// Add your code here, use BilinearInterpolation as a helper function.
double sum = 0.0;
double A = BilinearInterpolation(integralImage, upperLeftCol, upperLeftRow, width);
double B = BilinearInterpolation(integralImage, bottomRightCol, upperLeftRow, width);
double C = BilinearInterpolation(integralImage, upperLeftCol, bottomRightRow, width);
double D = BilinearInterpolation(integralImage, bottomRightCol, bottomRightRow, width);
sum = A + D - (B + C);
return sum;
}
/*******************************************************************************
ComputeFeatures - Compute all of the features for a specific bounding box
integralImage - integral image
c0, r0 - position of upper lefthand corner of bounding box
size - Size of bounding box
features - Array for storing computed feature values, access using features[i] for all i less than numWeakClassifiers.
weakClassifiers - Weak classifiers
numWeakClassifiers - Number of weak classifiers
w - Width of image (integralImage)
*******************************************************************************/
void MainWindow::ComputeFeatures(double *integralImage, int c0, int r0, int size, double *features, CWeakClassifiers *weakClassifiers, int numWeakClassifiers, int width)
{
int i, j;
double upperLeftCol, upperLeftRow, bottomRightCol, bottomRightRow;
bilinearInterpolation = BilinearInterpolationClass::getInstance(integralImage,width);
for(i=0;i<numWeakClassifiers;i++)
{
features[i] = 0.0;
for(j=0;j<weakClassifiers[i].m_NumBoxes;j++)
{
// Add your code to compute the sum of the pixels within each box weakClassifiers[i].m_Box[j]
upperLeftCol = weakClassifiers[i].m_Box[j][0][0] * (double)size + (double)c0;
upperLeftRow = weakClassifiers[i].m_Box[j][0][1] * (double)size + (double)r0;
bottomRightCol = weakClassifiers[i].m_Box[j][1][0] * (double)size + (double)c0;
bottomRightRow = weakClassifiers[i].m_Box[j][1][1] * (double)size + (double)r0;
double sum = SumBox(integralImage, upperLeftCol, upperLeftRow, bottomRightCol, bottomRightRow, width);
// Store the final feature value
features[i] += weakClassifiers[i].m_BoxSign[j]*sum/((double) (size*size));
}
}
}
/*******************************************************************************
FindBestClassifier - AdaBoost helper function. Find the best threshold for the candidate classifier
featureSortIdx - Indexes of the training examples sorted based on the feature responses (lowest to highest)
Use these indices to index into the other arrays, ifeatures.e. features, trainingLabel, dataWeights.
features - Array of feature values for the candidate classifier
trainingLabel - Ground truth labels for the training examples (1 = face, 0 = background)
dataWeights - Weights used to weight each training example
numTrainingExamples - Number of training examples
candidateWeakClassifier - Candidate classifier
bestClassifier - Returned best classifier (updated threshold, weight and polarity)
*******************************************************************************/
double MainWindow::FindBestClassifier(int *featureSortIdx, double *features, int *trainingLabel, double *dataWeights,
int numTrainingExamples, CWeakClassifiers candidateWeakClassifier, CWeakClassifiers *bestClassifier)
{
double bestError = 99999999.0;
candidateWeakClassifier.copy(bestClassifier); // Copy the weak classifiers params
//BestClassifier* bestClassifierClass = BestClassifier::getInstance(trainingLabel, numTrainingExamples);
bestClassifierClass = BestClassifier::getInstance(trainingLabel, numTrainingExamples);
bestClassifierClass->Initialize(featureSortIdx, features, dataWeights);
bestClassifierClass->FindOptimalClassifierParameter();
bestError = bestClassifierClass->classifierParameter.error;
bestClassifier->m_Polarity = bestClassifierClass->classifierParameter.polarity;
bestClassifier->m_Threshold = bestClassifierClass->classifierParameter.threshold;
bestClassifier->m_Weight = bestClassifierClass->classifierParameter.weight;
// Once you find the best weak classifier, you'll need to update the following member variables:
// bestClassifier->m_Polarity
// bestClassifier->m_Threshold
// bestClassifier->m_Weight - this is the alpha value in the course notes
return bestError;
}
/*******************************************************************************
UpdateDataWeights - AdaBoost helper function. Updates the weighting of the training examples
features - Array of feature values for the candidate classifier
trainingLabel - Ground truth labels for the training examples (1 = face, 0 = background)
weakClassifier - A weak classifier
dataWeights - Weights used to weight each training example. These are teh weights updated.
numTrainingExamples - Number of training examples
*******************************************************************************/
void MainWindow::UpdateDataWeights(double *features, int *trainingLabel, CWeakClassifiers weakClassifier, double *dataWeights, int numTrainingExamples)
{
// Add you code here.
double updatedWeightSum = 0.0;
int e = -1;
double beta = std::exp(-weakClassifier.m_Weight);
for(int dataIndex = 0; dataIndex < numTrainingExamples; dataIndex++) {
if(weakClassifier.m_Polarity == 0) {
if(features[dataIndex] <= weakClassifier.m_Threshold)
e = trainingLabel[dataIndex] == 1 ? 0 : 1;
else
e = trainingLabel[dataIndex] == 0 ? 0 : 1;
} else {
if(features[dataIndex] > weakClassifier.m_Threshold)
e = trainingLabel[dataIndex] == 1 ? 0 : 1;
else
e = trainingLabel[dataIndex] == 0 ? 0 : 1;
}
dataWeights[dataIndex] = dataWeights[dataIndex]* std::pow(beta,(1-e));
updatedWeightSum += dataWeights[dataIndex];
}
for (int dataIndex = 0; dataIndex < numTrainingExamples; ++dataIndex) {
dataWeights[dataIndex] /= updatedWeightSum;
}
}
/*******************************************************************************
ClassifyBox - FindFaces helper function. Return classification score for bounding box
integralImage - integral image
c0, r0 - position of upper lefthand corner of bounding box
size - Size of bounding box
weakClassifiers - Weak classifiers
numWeakClassifiers - Number of weak classifiers
w - Width of image (integralImage)
*******************************************************************************/
double MainWindow::ClassifyBox(double *integralImage, int c0, int r0, int size, CWeakClassifiers *weakClassifiers, int numWeakClassifiers, int width)
{
// Add your code here.
double score = 0.0;
double* features = new double[numWeakClassifiers];
ComputeFeatures(integralImage, c0, r0, size, features, weakClassifiers, numWeakClassifiers, width);
for(int index = 0; index < numWeakClassifiers; index++) {
if(features[index] > weakClassifiers[index].m_Threshold)
score += weakClassifiers[index].m_Weight * weakClassifiers[index].m_Polarity;
else
score += weakClassifiers[index].m_Weight * ( 1.0 - weakClassifiers[index].m_Polarity);
}
delete[] features;
return score;
}
/*******************************************************************************
NMS - Non-maximal suppression of face detections (neighboring face detections must be beyond
xyThreshold AND scaleThreshold in position and scale respectivitely.)
faceDetections - Set of face detections
xyThreshold - Minimum distance in position between neighboring detections
scaleThreshold - Minimum distance in scale between neighboring detections
displayImage - Display image
*******************************************************************************/
void MainWindow::NMS(QMap<double, CDetection> *faceDetections, double xyThreshold, double scaleThreshold, QImage *displayImage)