forked from andrewrong/FaceDect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleClassifier.h
More file actions
104 lines (96 loc) · 2.78 KB
/
SimpleClassifier.h
File metadata and controls
104 lines (96 loc) · 2.78 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
#ifndef SIM_H
#define SIM_H
#include "IntImage.h"
#include <fstream>
#include "MyStruct.h"
#include "ocv.h"
struct SimpleClassifier
{
REAL thresh;
REAL error;
int parity;
int type; // which type of feature?
int x1,x2,x3,x4,y1,y2,y3,y4;
inline const REAL GetOneFeature(const IntImage& im) const;
inline const REAL GetOneFeatureTranslation(REAL** const data,const int y) const;
inline const int Apply(const REAL value) const;
inline const int Apply(const IntImage& im) const;
void WriteToFile(std::ofstream& f) const;
void ReadFromFile(std::ifstream& f);
};
const int SimpleClassifier::Apply(const IntImage& im) const
{
if(parity == 1)
return (GetOneFeature(im)<thresh)?1:0;
else
return (GetOneFeature(im)>=thresh)?1:0;
}
const int SimpleClassifier::Apply(const REAL value) const
{
if(parity == 1)
return (value<thresh)?1:0;
else
return (value>=thresh)?1:0;
}
const REAL SimpleClassifier::GetOneFeature(const IntImage& im) const
{
REAL f1;
REAL** data = im.data;
switch(type)
{
case 0:
f1 = data[x1][y3] - data[x1][y1] + data[x3][y3] - data[x3][y1]
+ 2*(data[x2][y1] - data[x2][y3]);
break;
case 1:
f1 = data[x3][y1] + data[x3][y3] - data[x1][y1] - data[x1][y3]
+ 2*(data[x1][y2] - data[x3][y2]);
break;
case 2:
f1 = data[x1][y1] -data[x1][y3] + data[x4][y3] - data[x4][y1]
+ 3*(data[x2][y3] - data[x2][y1] + data[x3][y1] - data[x3][y3]);
break;
case 3:
f1 = data[x1][y1] - data[x1][y4] + data[x3][y4] - data[x3][y1]
+ 3*(data[x3][y2] - data[x3][y3] + data[x1][y3] - data[x1][y2] );
break;
case 4:
f1 = data[x1][y1] + data[x1][y3] + data[x3][y1] + data[x3][y3]
- 2*(data[x2][y1] + data[x2][y3] + data[x1][y2] + data[x3][y2])
+ 4*data[x2][y2];
break;
}
return f1/im.variance;
}
const REAL SimpleClassifier::GetOneFeatureTranslation(REAL** const data,const int y) const
{
REAL f1=0;
switch(type)
{
case 0:
f1 = data[x1][y+y3] - data[x1][y+y1] + data[x3][y+y3] - data[x3][y+y1]
+ 2*(data[x2][y+y1] - data[x2][y+y3]);
break;
case 1:
f1 = data[x3][y+y1] + data[x3][y+y3] - data[x1][y+y1] - data[x1][y+y3]
+ 2*(data[x1][y+y2] - data[x3][y+y2]);
break;
case 2:
f1 = data[x1][y+y1] - data[x1][y+y3] + data[x4][y+y3] - data[x4][y+y1]
+ 3*(data[x2][y+y3] - data[x2][y+y1] + data[x3][y+y1] - data[x3][y+y3]);
break;
case 3:
f1 = data[x1][y+y1] - data[x1][y+y4] + data[x3][y+y4] - data[x3][y+y1]
+ 3*(data[x3][y+y2] - data[x3][y+y3] + data[x1][y+y3] - data[x1][y+y2]);
break;
case 4:
f1 = data[x1][y+y1] + data[x1][y+y3] + data[x3][y+y1] + data[x3][y+y3]
- 2*(data[x2][y+y1] + data[x2][y+y3] + data[x1][y+y2] + data[x3][y+y2])
+ 4*data[x2][y+y2];
break;
default:
break;
}
return f1;
}
#endif