-
Notifications
You must be signed in to change notification settings - Fork 12
/
feature.py
48 lines (40 loc) · 1.66 KB
/
feature.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
import numpy
class NPDFeature():
"""It is a tool class to extract the NPD features.
Attributes:
image: A two-dimension ndarray indicating grayscale image.
n_pixels: An integer indicating the number of image total pixels.
features: A one-dimension ndarray to store the extracted NPD features.
"""
__NPD_table__ = None
def __init__(self, image):
'''Initialize NPDFeature class with an image.'''
if NPDFeature.__NPD_table__ is None:
NPDFeature.__NPD_table__ = NPDFeature.__calculate_NPD_table()
assert isinstance(image, numpy.ndarray)
self.image = image.ravel()
self.n_pixels = image.size
self.features = numpy.empty(shape=self.n_pixels * (self.n_pixels - 1) // 2, dtype=float)
def extract(self):
'''Extract features from given image.
Returns:
A one-dimension ndarray to store the extracted NPD features.
'''
count = 0
for i in range(self.n_pixels - 1):
for j in range(i + 1, self.n_pixels, 1):
self.features[count] = NPDFeature.__NPD_table__[self.image[i]][self.image[j]]
count += 1
return self.features
@staticmethod
def __calculate_NPD_table():
'''Calculate all situations table to accelerate feature extracting.'''
print("Calculating the NPD table...")
table = numpy.empty(shape=(1 << 8, 1 << 8), dtype=float)
for i in range(1 << 8):
for j in range(1 << 8):
if i == 0 and j == 0:
table[i][j] = 0
else:
table[i][j] = (i - j) / (i + j)
return table