-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
217 lines (200 loc) · 6.34 KB
/
utils.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#%%
from math import pi
from PIL import Image
import numpy as np
#%%
def image_to_numpy(image_path: str) -> np.ndarray:
return np.array(Image.open(image_path))
#%%
_rgb_yuv_conv = np.array([
# R, G, B factors for
[ 0.299, 0.587, 0.114], # Y
[-0.169, -0.331, 0.5], # Cb
[ 0.5, -0.419, -0.081], # Cr
]).T
def rgb_to_yCbCr(img: np.ndarray) -> np.ndarray:
img = img.astype(np.float)
out = img.dot(_rgb_yuv_conv)
out[:, :, (1, 2)] += 128
return out.astype(np.uint8)
_yuv_rgb_conv = np.array([
#Y, Cb, Cr
[1, 0, 1.402], # R
[1, -0.334, -0.714], # G
[1, 1.772, 0], # B
]).T
def yCbCr_to_rgb(yuv: np.ndarray) -> np.ndarray:
yuv = yuv.astype(np.float)
yuv[:, :, (1, 2)] -= 128
out = yuv.dot(_yuv_rgb_conv)
return out.astype(np.uint8)
#%%
def chroma_subsampling(yuv: np.ndarray) -> np.ndarray:
''' 4:2:0 chroma subsampling '''
out = yuv.copy()
out[1::2, :, (1, 2)] = out[::2, :, (1, 2)]
out[:, 1::2, (1, 2)] = out[:, ::2, (1, 2)]
return out
#%%
def dct1d_n(inp: np.ndarray) -> np.ndarray:
N = inp.shape[-2]
k = np.arange(N)
n = np.arange(N)
t = pi/N * (n+0.5)
return inp @ np.cos(np.outer(k, t)).T
def inv_dct1d_n(inp: np.ndarray) -> np.ndarray:
N = inp.shape[-2]
k = np.arange(N)
n = np.arange(1, N)
t = pi/N * n
# The various transpose operations and the bit hacky `[0:1]` instead of `[0]`
# is used to support 2D arrays. The operation still operates on a 1D basis,
# i.e. for a 2D array the operation performs the DCT-III on each row
# individually.
dims = len(inp.shape)
dims_perm = [i for i in range(dims)]
tr = lambda x: np.transpose(x, (*dims_perm[:-2], dims-1, dims-2))
dct3 = (0.5*tr(tr(inp)[..., 0:1, :])) + np.dot(tr(tr(inp)[..., 1:, :]), np.cos(np.outer(k+0.5, t)).T)
# inverse of DCT-II is a scaled DCT-III
return 2/N * dct3
def dct2d_n(inp: np.ndarray, inverse=False) -> np.ndarray:
'''
Apply 2D DCT-II or 2D inverse DCT-II on `inp`
inp: multidimensional array. (inverse) DCT is applied to last two dimensions
'''
dims = len(inp.shape)
dims_perm = [i for i in range(dims)]
tr = lambda x: np.transpose(x, (*dims_perm[:-2], dims-1, dims-2))
if inverse:
return tr(inv_dct1d_n(tr(inv_dct1d_n(inp))))
return tr(dct1d_n(tr(dct1d_n(inp))))
#%%
def dct1d(inp: np.ndarray) -> np.ndarray:
assert len(inp.shape) in [1, 2]
N = inp.shape[0]
k = np.arange(N)
n = np.arange(N)
t = pi/N * (n+0.5)
return np.dot(inp, np.cos(np.outer(k, t)).T)
def inv_dct1d(inp: np.ndarray) -> np.ndarray:
N = inp.shape[0]
k = np.arange(N)
n = np.arange(1, N)
t = pi/N * n
# The various transpose operations and the bit hacky `[0:1]` instead of `[0]`
# is used to support 2D arrays. The operation still operates on a 1D basis,
# i.e. for a 2D array the operation performs the DCT-III on each row
# individually.
dct3 = (0.5*inp.T[0:1].T) + np.dot(inp.T[1:].T, np.cos(np.outer(k+0.5, t)).T)
# inverse of DCT-II is a scaled DCT-III
return 2/N * dct3
def dct2d(inp: np.ndarray, inverse=False) -> np.ndarray:
''' Apply 2D DCT-II or 2D inverse DCT-II on `inp` '''
assert len(inp.shape) == 2
if inverse:
return np.round(inv_dct1d(inv_dct1d(inp).T).T, decimals=10)
return np.round(dct1d(dct1d(inp).T).T, decimals=10)
#%%
precomputed_quantization_table = np.array([
[ 16, 12, 10, 16, 24, 40, 51, 61],
[ 11, 12, 14, 19, 26, 58, 60, 55],
[ 14, 13, 16, 24, 40, 57, 69, 56],
[ 14, 17, 22, 29, 51, 87, 80, 62],
[ 18, 22, 37, 56, 68, 109, 103, 77],
[ 24, 35, 55, 64, 81, 104, 113, 92],
[ 49, 64, 78, 87, 103, 121, 120, 103],
[ 72, 92, 95, 98, 112, 100, 101, 99]
])
def gen_quantization_table(quality: int) -> np.ndarray:
Q = np.empty((8, 8))
for i in range(8):
for j in range(8):
Q[i, j] = 1 + (1+i+j) * quality
return Q
def quantize(inp: np.ndarray, q: np.ndarray) -> np.ndarray:
return np.round(inp / q)
def dequantize(inp: np.ndarray, q: np.ndarray):
return inp * q
#%%
def entropy_encode(channel: np.ndarray):
# TODO
pass
def dpcm(inp: np.ndarray) -> np.ndarray:
out = inp.copy()
for i in range(inp.shape[0]-1):
dc = inp[i, ..., 0, 0]
out[i+1, ..., 0, 0] -= dc
return out
def un_dpcm(inp: np.ndarray) -> np.ndarray:
out = inp.copy()
for i in range(inp.shape[0]-1):
dc = out[i, ..., 0, 0]
out[i+1, ..., 0, 0] += dc
return out
_zigzag = (
(0, 0),
(0, 1), (1, 0),
(2, 0), (1, 1), (0, 2),
(0, 3), (1, 2), (2, 1), (3, 0),
(4, 0), (3, 1), (2, 2), (1, 3), (0, 4),
(0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0),
(6, 0), (5, 1), (4, 2), (3, 3), (2, 4), (1, 5), (0, 6),
(0, 7), (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1), (7, 0),
(7, 1), (6, 2), (5, 3), (4, 4), (3, 5), (2, 6), (1, 7),
(2, 7), (3, 6), (4, 5), (5, 4), (6, 3), (7, 2),
(7, 3), (6, 4), (5, 5), (4, 6), (3, 7),
(4, 7), (5, 6), (6, 5), (7, 4),
(7, 5), (6, 6), (5, 7),
(6, 7), (7, 6),
(7, 7),
)
_zigzag_rows = tuple(map(lambda x: x[0], _zigzag))
_zigzag_cols = tuple(map(lambda x: x[1], _zigzag))
def zigzag(inp: np.ndarray) -> np.ndarray:
return inp[..., _zigzag_rows, _zigzag_cols]
_unzigzag = (
0, 1, 5, 6, 14, 15, 27, 28,
2, 4, 7, 13, 16, 26, 29, 42,
3, 8, 12, 17, 25, 30, 41, 43,
9, 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63,
)
def unzigzag(inp: np.ndarray, shape=None) -> np.ndarray:
return inp[..., _unzigzag].reshape(shape)
def run_length_encode(inp):
# TODO
pass
def run_length_decode(inp):
# TODO
pass
def huffman_encode(inp):
# TODO
pass
def huffman_decode(inp):
# TODO
pass
#%%
def store(img):
# TODO
pass
# %%
def blocks_split(inp: np.ndarray, block_dim=8) -> np.ndarray:
row_blocks_indicies = [i for i in range(block_dim, inp.shape[0], block_dim)]
col_blocks_indicies = [i for i in range(block_dim, inp.shape[1], block_dim)]
out = np.array(list(map(
lambda a: np.array_split(a, col_blocks_indicies, axis=1),
np.array_split(inp, row_blocks_indicies)
)))
out = out.reshape(-1, *out.shape[-3:])
# blocks, channels, width, height
return np.moveaxis(out, 3, 1)
# %%
def de_blocks(inp: np.ndarray, width: int, height: int) -> np.ndarray:
B = width // 8
rows = [np.concatenate(inp[i*B:(i+1)*B], axis=2) for i in range(B)]
img = np.concatenate(rows, axis=1)
# width, height, channels
return np.moveaxis(img, 0, 2)