-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFeatureExtractors.py
executable file
·140 lines (97 loc) · 5.27 KB
/
FeatureExtractors.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
from Data import *
from Utils import *
import cv2
class FeatureExtractors:
def moments_feature_extractor(self, image):
moments = get_moments(image)
feature_names = []
feature_moments = []
for label in moments:
feature_names.append(label)
feature_moments.append(moments[label])
return (array(feature_names), array(feature_moments))
def solidity_feature_extractor(self, image):
solidity = get_solidity(image)
return (array(['solidity']), array([solidity]))
def hu_momments_feature_extractor(self, image):
feature_names = array(['hu1', 'hu2','hu3', 'hu4','hu5', 'hu6','hu7'])
moments = cv2.moments(image)
hu_moments = cv2.HuMoments(moments).flatten()
return (feature_names, hu_moments)
def length_width_ratio_feature_extractor(self, image):
ratio = 0
x_diff = max_x_diff(image)
y_diff = max_y_diff(image)
if y_diff > x_diff:
ratio = y_diff/x_diff
else:
ratio = x_diff/y_diff
features = array([ratio])
feature_names = array(['length_width_ratio'])
return (feature_names, features)
def corner_count_feature_extractor(self, image):
corners = get_corner_points(image, 100)
features = array([len(corners)])
feature_names = array(['number_corners'])
return (feature_names, features)
def perimeter_area_ratio_feature_extractor(self, image):
area_points = get_image_area(image)
edge_points = get_edge_points(image)
perimeter = len(edge_points)
perimeter_area_ratio = 1.0 * perimeter / area_points
features = array([perimeter_area_ratio])
feature_names = array(['perimeter_area_ratio'])
return (feature_names, features)
def ratio_of_areas_feature_extractor(self, image):
area_points = get_image_area(image)
x_diff = max_x_diff(image)
y_diff = max_y_diff(image)
area_square = x_diff * y_diff
ratio_of_areas = 1.0* area_square / area_points
features = array([ratio_of_areas])
feature_names = array(['ratio_of_areas'])
return (feature_names, features)
def no_moments_feature_extractor(self, image):
(feature_names1, features1) = self.corner_count_feature_extractor(image)
(feature_names2, features2) = self.length_width_ratio_feature_extractor(image)
(feature_names3, features3) = self.perimeter_area_ratio_feature_extractor(image)
(feature_names4, features4) = self.ratio_of_areas_feature_extractor(image)
(feature_names5, features5) = self.solidity_feature_extractor(image)
features = array(list(features1)+list(features2)+list(features3)+list(features4)+list(features5))
feature_names = array(list(feature_names1)+list(feature_names2)+list(feature_names4)+list(feature_names4)+list(feature_names5))
return (feature_names, features)
def no_moments_feature_extractor_traning(self, image,id, label):
(feature_names1, features1) = self.corner_count_feature_extractor(image)
(feature_names2, features2) = self.length_width_ratio_feature_extractor(image)
(feature_names3, features3) = self.perimeter_area_ratio_feature_extractor(image)
(feature_names4, features4) = self.ratio_of_areas_feature_extractor(image)
(feature_names5, features5) = self.solidity_feature_extractor(image)
features = array(list([id, label])+list(features1)+list(features2)+list(features3)+list(features4)+list(features5))
feature_names = array( list(["id","label"])+list(feature_names1)+list(feature_names2)+list(feature_names4)+list(feature_names4)+list(feature_names5))
return (feature_names, features)
def all_feature_extractor(self, image):
(feature_names1, features1) = self.corner_count_feature_extractor(image)
(feature_names2, features2) = self.length_width_ratio_feature_extractor(image)
(feature_names3, features3) = self.perimeter_area_ratio_feature_extractor(image)
(feature_names4, features4) = self.ratio_of_areas_feature_extractor(image)
(feature_names5, features5) = self.solidity_feature_extractor(image)
(feature_names6, features6) = self.moments_feature_extractor(image)
(feature_names7, features7) = self.hu_momments_feature_extractor(image)
features = array(list(features1)+list(features2)+list(features3)+list(features4) + \
list(features5)+list(features6)+list(features7))
feature_names = array(list(feature_names1)+list(feature_names2)+list(feature_names3) + \
list(feature_names4) + list(feature_names5) + list(feature_names6) + list(feature_names7))
return (feature_names, features)
def all_feature_extractor_traning(self, image, id, label):
(feature_names1, features1) = self.corner_count_feature_extractor(image)
(feature_names2, features2) = self.length_width_ratio_feature_extractor(image)
(feature_names3, features3) = self.perimeter_area_ratio_feature_extractor(image)
(feature_names4, features4) = self.ratio_of_areas_feature_extractor(image)
(feature_names5, features5) = self.solidity_feature_extractor(image)
(feature_names6, features6) = self.moments_feature_extractor(image)
(feature_names7, features7) = self.hu_momments_feature_extractor(image)
features = array(list([id, label])+list(features1)+list(features2)+list(features3)+list(features4) + \
list(features5)+list(features6)+list(features7))
feature_names = array( list(["id","label"])+ list(feature_names1)+list(feature_names2)+list(feature_names3) + \
list(feature_names4) + list(feature_names5) + list(feature_names6) + list(feature_names7))
return (feature_names, features)