forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHaarLikeFeatureExtractor.py
More file actions
116 lines (105 loc) · 3.87 KB
/
Copy pathHaarLikeFeatureExtractor.py
File metadata and controls
116 lines (105 loc) · 3.87 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
from SimpleCV.base import *
from SimpleCV.ImageClass import Image
from SimpleCV.Features.HaarLikeFeature import *
from SimpleCV.Features.FeatureExtractorBase import *
class HaarLikeFeatureExtractor(FeatureExtractorBase):
"""
This is used generate Haar like features from an image. These
Haar like features are used by a the classifiers of machine learning
to help identify objects or things in the picture by their features,
or in this case haar features.
For a more in-depth review of Haar Like features see:
http://en.wikipedia.org/wiki/Haar-like_features
"""
mFeatureSet = None
mDo45 = True
def __init__(self, fname=None, do45=True):
"""
fname - The feature file name
do45 - if this is true we use the regular integral image plus the
45 degree integral image
"""
#we define the black (positive) and white (negative) regions of an image
#to get our haar wavelet
self.mDo45 = True
self.mFeatureset=None;
if(fname is not None):
self.readWavelets(fname)
def readWavelets(self, fname,nfeats=-1):
"""
fname = file name
nfeats = number of features to load from file -1 -> All features
"""
# We borrowed the wavelet file from Chesnokov Yuriy
# He has a great windows tutorial here:
# http://www.codeproject.com/KB/audio-video/haar_detection.aspx
# SimpleCV Took a vote and we think he is an all around swell guy!
# nfeats = number of features to load
# -1 loads all
# otherwise loads min(nfeats,features in file)
self.mFeatureSet = []
f = open(fname,'r')
#line = f.readline()
#count = int(line)
temp = f.read()
f.close()
data = temp.split()
count = int(data.pop(0))
self.mFeatureset = []
if(nfeats > -1):
count = min(count,nfeats)
while len(data) > 0:
name = data.pop(0)
nRegions = int(data.pop(0))
region = []
for i in range(nRegions):
region.append(tuple(map(float,data[0:5])))
data = data[5:]
feat = HaarLikeFeature(name,region)
self.mFeatureSet.append(feat)
return None
def saveWavelets(self, fname):
"""
Save wavelets to file
"""
f = open(fname,'w')
f.write(str(len(self.mFeatureSet))+'\n\n')
for i in range(len(self.mFeatureSet)):
self.mFeatureSet[i].writeToFile(f)
f.close()
return None
def extract(self, img):
"""
This extractor takes in an image, creates the integral image, applies
the Haar cascades, and returns the result as a feature vector.
"""
regular = img.integralImage()
retVal = []
for i in range(len(self.mFeatureSet)):
retVal.append(self.mFeatureSet[i].apply(regular))
if(self.mDo45):
slant = img.integralImage(tilted=True)
for i in range(len(self.mFeatureSet)):
retVal.append(self.mFeatureSet[i].apply(regular))
return retVal
def getFieldNames(self):
"""
This method gives the names of each field in the feature vector in the
order in which they are returned. For example, 'xpos' or 'width'
"""
retVal = []
for i in range( len(self.mFeatureSet)):
retVal.append(self.mFeatureSet[i].mName)
if( self.mDo45 ):
for i in range( len(self.mFeatureSet)):
name = "Angle_"+self.mFeatureSet[i].mName
retVal.append(name)
return retVal
def getNumFields(self):
"""
This method returns the total number of fields in the feature vector.
"""
mult = 1
if(self.mDo45):
mult = 2
return mult*len(self.mFeatureset)