forked from WenDesi/lihang_book_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_features.py
177 lines (138 loc) · 4.49 KB
/
extract_features.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
169
170
171
172
173
#encoding=utf-8
import numpy as np
import cv2
import time
import struct
import matplotlib.pyplot as plt
def loadImageSet(which=0):
print "load image set"
binfile=None
if which==0:
binfile = open("data/train-images.idx3-ubyte", 'rb')
else:
binfile= open("data/t10k-images.idx3-ubyte", 'rb')
buffers = binfile.read()
head = struct.unpack_from('>IIII' , buffers ,0)
print "head,",head
offset=struct.calcsize('>IIII')
imgNum=head[1]
width=head[2]
height=head[3]
#[60000]*28*28
bits=imgNum*width*height
bitsString='>'+str(bits)+'B' #like '>47040000B'
imgs=struct.unpack_from(bitsString,buffers,offset)
binfile.close()
imgs=np.reshape(imgs,[imgNum,width,height])
print "load imgs finished"
return imgs
def loadLabelSet(which=0):
print "load label set"
binfile=None
if which==0:
binfile = open("data/train-labels.idx1-ubyte", 'rb')
else:
binfile= open("data/t10k-labels.idx1-ubyte", 'rb')
buffers = binfile.read()
head = struct.unpack_from('>II' , buffers ,0)
print "head,",head
imgNum=head[1]
offset = struct.calcsize('>II')
numString='>'+str(imgNum)+"B"
labels= struct.unpack_from(numString , buffers , offset)
binfile.close()
labels=np.reshape(labels,[imgNum,1])
#print labels
print 'load label finished'
return labels
def get_features(imgs):
features = []
hog = cv2.HOGDescriptor('hog.xml')
# 二值化
for i in range(len(imgs)):
cv_img = imgs[i].astype(np.uint8)
cv2.threshold(cv_img,25,255,cv2.cv.CV_THRESH_BINARY_INV,imgs[i])
for img in imgs:
cv_img = img.astype(np.uint8)
hog_feature = hog.compute(cv_img)
hog_feature = np.transpose(hog_feature)
features.append(hog_feature)
return np.array(features)
def get_hog_features():
# trainset features
features_filepath = 'features/train.vec.npy'
imgs = loadImageSet()
labels = loadLabelSet()
features = get_features(imgs)
np.save(features_filepath,features)
# testset features
features_filepath = 'features/test.vec.npy'
imgs = loadImageSet(1)
labels = loadLabelSet(1)
features = get_features(imgs)
np.save(features_filepath,features)
features = np.load(features_filepath)
def manul_features(imgs):
features = []
tt = 0
for img in imgs:
print tt
tt += 1
feature = []
cv_img = img.astype(np.uint8)
cv2.threshold(cv_img,25,255,cv2.cv.CV_THRESH_BINARY_INV,cv_img)
range_list = [[0,7,0,7],
[0,7,7,14],
[0,7,14,21],
[0,7,21,28],
[7,14,0,7],
[7,11,7,11],
[7,11,11,14],
[7,11,14,17],
[7,11,17,21],
[11,14,7,11],
[11,14,11,14],
[11,14,14,17],
[11,14,17,21],
[7,14,21,28],
[14,21,21,28],
[21,28,21,28],
[14,21,0,7],
[21,28,0,7],
[14,17,7,11],
[14,17,11,14],
[14,17,14,17],
[14,17,17,21],
[17,21,7,11],
[17,21,11,14],
[17,21,14,17],
[17,21,17,21],
[21,24,7,11],
[21,24,11,14],
[21,24,14,17],
[21,24,17,21],
[24,28,7,11],
[24,28,11,14],
[24,28,14,17],
[24,28,17,21]]
for range_ in range_list:
count = 0
for i in range(range_[0],range_[1]):
for j in range(range_[2],range_[3]):
if cv_img[i][j] < 50:
count += 1
feature.append(count)
features.append(feature)
return np.array(features)
def get_manual_features():
trainset_features_filepath = 'features/train.vec.npy'
testset_features_filepath = 'features/test.vec.npy'
imgs = loadImageSet()
features = manul_features(imgs)
np.save(trainset_features_filepath,features)
imgs = loadImageSet(1)
features = manul_features(imgs)
np.save(testset_features_filepath,features)
if __name__=="__main__":
get_manual_features()
# get_hog_features()