-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfeatures.py
More file actions
156 lines (134 loc) · 6.07 KB
/
Copy pathfeatures.py
File metadata and controls
156 lines (134 loc) · 6.07 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
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
# Copyright (C) 2012 Rafael Cunha de Almeida <rafael@kontesti.me>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Image
import ImageOps
import ImageFilter
import numpy
class compose_extractors(object):
def __init__(self, extractors):
self.extractors = extractors
def __call__(self, arg):
image_features = {}
if isinstance(arg, str):
file_path = arg
with open(file_path) as f:
image = Image.open(f).convert("L")
else:
image = arg
for extractor in self.extractors:
extractor(image, image_features)
return image_features
class border(object):
def __init__(self, callback):
self.callback = callback
def __border_detection(self, digit):
digit.image = digit.image.filter(ImageFilter.FIND_EDGES)
digit.pix = digit.image.load()
return digit
def __call__(self, digit, features):
return callback(self.__border_detection(digit), features, prefix='border-')
class scale_image_down(object):
def __init__(self, callback):
self.callback = callback
def __scale_down(self, digit):
digit.image = digit.image.resize((16,16), Image.BICUBIC)
digit.pix = digit.image.load()
return digit
def __call__(self, digit, features):
return self.callback(self.__scale_down(digit), features, prefix='scaled-')
def is_white(color):
return color > 230
def x_histogram(digit, features, prefix=''):
width,height = digit.image.size
pix = numpy.asarray(digit.image).transpose()
for x in range(width):
features[prefix+'x-histogram-'+str(x)] = numpy.add.reduce(pix[x])
def y_histogram(digit, features, prefix=''):
width,height = digit.image.size
pix = numpy.asarray(digit.image)
for y in range(height):
features[prefix+'y-histogram-'+str(y)] = numpy.add.reduce(pix[y])
def positions(digit, features, prefix=''):
width,height = digit.image.size
for x in range(width):
for y in range(height):
features[prefix+'pos-'+str(x*height + y)] = digit.pix[x,y]
def number_of_whites(digit, features, prefix=''):
width,height = digit.image.size
counter = 0
for x in range(width):
for y in range(height):
if is_white(digit.pix[x,y]):
counter += 1
features[prefix+'number_of_whites'] = counter
def number_of_pixels(digit, features, prefix=''):
width,height = digit.image.size
features[prefix+'number_of_pixels'] = width * height
def horizontal_silhouette(digit, features, prefix=''):
width,height = digit.image.size
for y in range(height):
for x in range(width):
if is_white(digit.get((x,y))):
features[prefix+'horizontal_silhouette'+str(y)] = x/float(width)
def reversed_horizontal_silhouette(digit, features, prefix=''):
width,height = digit.image.size
for y in range(height):
for x in reversed(range(width)):
if is_white(digit.get((x,y))):
features[prefix+'reversed_horizontal_silhouette'+str(y)] = x/float(width)
def vertical_silhouette(digit, features, prefix=''):
width,height = digit.image.size
for x in range(width):
for y in range(height):
if is_white(digit.get((x,y))):
features[prefix+'vertical_silhouette'+str(x)] = y/float(height)
def reversed_vertical_silhouette(digit, features, prefix=''):
width,height = digit.image.size
for x in range(width):
for y in reversed(range(height)):
if is_white(digit.get((x,y))):
features[prefix+'reversed_vertical_silhouette'+str(x)] = y/float(height)
def middle_silhouette(digit, features, prefix=''):
width,height = digit.image.size
x = width / 2
for i,y in enumerate(reversed(range(height/2))):
if is_white(digit.get((x,y))):
features[prefix+'middle_silhouette_a'] = i
for i,y in enumerate(range(height/2, height)):
if is_white(digit.get((x,y))):
features[prefix+'middle_silhouette_b'] = i
y = height/2
for i,x in enumerate(reversed(range(width/2))):
if is_white(digit.get((x,y))):
features[prefix+'middle_silhouette_c'] = i
for i,x in enumerate(range(width/2, width)):
if is_white(digit.get((x,y))):
features[prefix+'middle_silhouette_d'] = i
def vertical_symmetry(digit, features, prefix=''):
width,height = digit.image.size
first_half = numpy.array(digit.image.crop((0, 0, width/2, height)).getdata())
second_half = numpy.array(ImageOps.mirror(digit.image.crop((width/2, 0, width, height)).getdata()))
second_half = second_half[:len(first_half)]
features[prefix+'vertical_symmetry'] = numpy.linalg.norm(first_half-second_half)
def horizontal_symmetry(digit, features, prefix=''):
width,height = digit.image.size
first_half = numpy.array(digit.image.crop((0, 0, width, height/2)).getdata())
second_half = numpy.array(ImageOps.flip(digit.image.crop((0, height/2, width, height))).getdata())
second_half = second_half[:len(first_half)]
features[prefix+'horizontal_symmetry'] = numpy.linalg.norm(first_half-second_half)