-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpredictive.py
More file actions
149 lines (109 loc) · 4.64 KB
/
predictive.py
File metadata and controls
149 lines (109 loc) · 4.64 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
"""
module:: PredictiveTransform
:synopsis: This transformer transforms (wow) an image using a predictive approach, decreasing entropy.
moduleauthor:: Edgar Duarte <edgarduarte@student.dei.uc.pt>
Several libs are imported here:
numpy
This transformer codec was made by Edgar Duarte.
"""
import numpy as np
# Predictive Transformer
class PredictiveTransform:
"""
This class encodes or decodes a 2D array using a predictive transformation. The main objective of this algorithm is to reduce entropy of the
given information.
Use only encode() or decode()
"""
def __init__(self):
"""
Constructor. It is empty
"""
pass
def horizontalEncode(self, initial_data):
"""
Encodes each line of the given data via the function f(i) = f(i) - f(i-1), with i ∈ [1, array_lenght], being i the index of the current pixel
Parameters:
initial_data (2D array): array that will be encoded
Returns:
2D array: encoded array
"""
final_data = np.copy(initial_data)
final_data = final_data.astype("int16")
_ , length = final_data.shape
final_data[:,1:] = np.subtract(final_data[:,1:], final_data[:,:length-1])
return final_data
def horizontalDecode(self, initial_data):
"""
Decodes each line of the given data (that was previously encoded) via the function f(i) = f(i) + f(i-1), with i ∈ [1, array_lenght],being
i the index of the current pixel
Parameters:
initial_data (2D array): array that will be decoded
Returns:
2D array: decoded array
"""
final_data = np.copy(initial_data)
_ , length = final_data.shape
for i in range(1,length):
final_data[:,i] += final_data[:,i-1]
final_data = final_data.astype("uint8")
return final_data
def verticalEncode(self, initial_data):
"""
Encodes each column of the given data via the function f(i) = f(i) - f(i-1), with i ∈ [1, array_heigth], being i the index of the current pixel
Parameters:
initial_data (2D array): array that will be encoded
Returns:
2D array: encoded array
"""
final_data = np.copy(initial_data)
final_data = final_data.astype("int16")
height , _ = final_data.shape
final_data[1:,:] = np.subtract(final_data[1:,:], final_data[:height-1 ,: ])
return final_data
def verticalDecode(self, initial_data):
"""
Decodes each column of the given data via the function f(i) = f(i) - f(i-1), with i ∈ [1, array_heigth], being i the index of the current pixel
Parameters:
initial_data (2D array): array that will be decoded
Returns:
2D array: decoded array
"""
final_data = np.copy(initial_data)
height , _ = final_data.shape
for i in range(1, height):
final_data[i,:] += final_data[i-1,:]
final_data = final_data.astype("uint8")
return final_data
# Transforms data using Predictive algorithm
def encode(self, data, vertical=False):
"""
Encodes data using a predictive transformer
It can encode both horizontaly and verticaly, being the method chosen by the user, although the default setting is to encode
horizontaly.
Parameters:
data (2D array): data to be encoded
vertical (boolean): if True it encodes using a vertical predictive encoder. Default: False, encodes horizontaly
Returns:
2D array: encoded data
"""
if (vertical):
encoded_data = self.verticalEncode(data)
else:
encoded_data = self.horizontalEncode(data)
return encoded_data
def decode(self, data, vertical=False):
"""
Decodes data using a predictive transformer
Should be decoded with the same transformation as it was encoded, which means, if you encode with a vertical encoder you have to decode with the
vertical decoder or else the result array will have unexpected results.
Parameters:
data (2D array): data to be decoded
vertical (boolean): if True it encodes using a vertical predictive encoder. Default: False, encodes horizontaly
Returns:
2D array: decoded data
"""
if (vertical):
decoded_data = self.verticalDecode(data)
else:
decoded_data = self.horizontalDecode(data)
return decoded_data