-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_bugan.py
168 lines (134 loc) · 6.59 KB
/
feature_bugan.py
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
class Haar(object):
def __init__(self, img_width, img_height):
self.IMG_WIDTH = img_width
self.IMG_HEIGHT = img_height
self.WINDOW_WIDTH = self.IMG_WIDTH
self.WINDOW_HEIGHT = self.IMG_HEIGHT
self.HAAR_TYPES = (
'haartype1',
'haartype2',
'haartype3',
'haartype4',
'haartype5',
'haartype6',
'haartype7',
'haartype8',
'haartype9',
'haartype10',
'haartype11',
'haartype12',
'haartype13',
)
self.features = []
self._createFeatures()
def _createFeatures(self):
"""create all kinds of haar features in this window size
:return: [(h_type, x, y, w, h),
(h_type, x, y, w, h),
...]
notice: x,y are the coordinates in the image Matrix instead of the integral image Matrix
"""
WIDTH_LIMIT = {
"haartype1" : self.WINDOW_WIDTH,
"haartype2" : int(self.WINDOW_WIDTH/2),
"haartype3" : int(self.WINDOW_WIDTH/3),
"haartype4" : self.WINDOW_WIDTH,
"haartype5" : int(self.WINDOW_WIDTH/2)
}
HEIGHT_LIMIT = {
"haartype1" : int(self.WINDOW_HEIGHT/2),
"haartype2" : self.WINDOW_HEIGHT,
"haartype3" : self.WINDOW_HEIGHT,
"haartype4" : int(self.WINDOW_HEIGHT/3),
"haartype5" : int(self.WINDOW_HEIGHT/2)
}
for h_type in self.HAAR_TYPES:
for w in range(1, WIDTH_LIMIT[h_type]+1):
for h in range(1, HEIGHT_LIMIT[h_type]+1):
if h_type == "haartype1":
x_limit = self.WINDOW_WIDTH - w
y_limit = self.WINDOW_HEIGHT - 2*h
for x in range(0, x_limit+1):
for y in range(0, y_limit+1):
self.features.append([h_type, x, y, w, h])
if h_type == "haartype2":
x_limit = self.WINDOW_WIDTH - 2*w
y_limit = self.WINDOW_HEIGHT - h
for x in range(0, x_limit+1):
for y in range(0, y_limit+1):
self.features.append([h_type, x, y, w, h])
if h_type == "haartype3":
x_limit = self.WINDOW_WIDTH - 3*w
y_limit = self.WINDOW_HEIGHT - h
for x in range(0, x_limit+1):
for y in range(0, y_limit+1):
self.features.append([h_type, x, y, w, h])
if h_type == "haartype4":
x_limit = self.WINDOW_WIDTH - w
y_limit = self.WINDOW_HEIGHT - 3*h
for x in range(0, x_limit+1):
for y in range(0, y_limit+1):
self.features.append([h_type, x, y, w, h])
if h_type == "haartype5":
x_limit = self.WINDOW_WIDTH - 2*w
y_limit = self.WINDOW_HEIGHT - 2*h
for x in range(0, x_limit+1):
for y in range(0, y_limit+1):
self.features.append([h_type, x, y, w, h])
def calImgFeatureVal(self, IntegralMat, mat):
"""
:param IntegralMat: the integral value of the image
:return: a list including values of all features
"""
featureVal = np.zeros(len(self.features))
for feature_index in range(len(self.features)):
h_type, x, y, w, h = self.features[feature_index]
# #normalization
# sumVal = sum(sum(mat[y:y+h, x:x+w]))
# sqSumVal = sum(sum(mat[y:y+h, x:x+w] ** 2))
# meanVal = sumVal / (w * h)
# sqMeanVal = sqSumVal / (w * h)
#
# normFactorVal = np.sqrt(sqMeanVal - meanVal ** 2)
# if normFactorVal == 0:
# normFactorVal = 1
if h_type == "haartype1":
pos = self.getPixelValInIntegralMat(x, y, w, h, IntegralMat)
neg = self.getPixelValInIntegralMat(x, y+h, w, h, IntegralMat)
featureVal[feature_index] = (pos - neg) / (2 * w * h)
elif h_type == "haartype2":
neg = self.getPixelValInIntegralMat(x, y, w, h, IntegralMat)
pos = self.getPixelValInIntegralMat(x+w, y, w, h, IntegralMat)
featureVal[feature_index] = (pos - neg) / (2 * w * h)
elif h_type == "haartype3":
neg1 = self.getPixelValInIntegralMat(x, y, w, h, IntegralMat)
pos = self.getPixelValInIntegralMat(x+w, y, w, h, IntegralMat)
neg2 = self.getPixelValInIntegralMat(x+2*w, y, w, h, IntegralMat)
featureVal[feature_index] = (2*pos - neg1 - neg2)/ (3 * w * h)
elif h_type == "haartype4":
neg1 = self.getPixelValInIntegralMat(x, y, w, h, IntegralMat)
pos = self.getPixelValInIntegralMat(x, y+h, w, h, IntegralMat)
neg2 = self.getPixelValInIntegralMat(x, y+2*h, w, h, IntegralMat)
featureVal[feature_index] = (2*pos - neg1 - neg2) / (3 * w * h)
elif h_type == "haartype5":
neg1 = self.getPixelValInIntegralMat(x, y, w, h, IntegralMat)
pos1 = self.getPixelValInIntegralMat(x+w, y, w, h, IntegralMat)
pos2 = self.getPixelValInIntegralMat(x, y+h, w, h, IntegralMat)
neg2 = self.getPixelValInIntegralMat(x+w, y+h, w, h, IntegralMat)
featureVal[feature_index] = (pos1 + pos2 - neg1 - neg2) / (4 * w * h)
return featureVal
def getPixelValInIntegralMat(self, x, y, w, h, integralMat):
"""
x,y are the coordinates in the image matrix
:param integralMat:
:return:
"""
if x == 0 and y == 0:
return integralMat[y+h-1][x+w-1]
elif x == 0:
return integralMat[y+h-1][x+w-1] - integralMat[y-1][x+w-1]
elif y == 0:
return integralMat[y+h-1][x+w-1] - integralMat[y+h-1][x-1]
else:
return integralMat[y+h-1][x+w-1] + integralMat[y-1][x-1] \
- integralMat[y-1][x+w-1] - integralMat[y+h-1][x-1]