-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.cpp
More file actions
75 lines (55 loc) · 2.12 KB
/
Copy pathimplementation.cpp
File metadata and controls
75 lines (55 loc) · 2.12 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
import numpy as np
from PIL import Image
from math import ceil
def create_images():
orig = Image.open('image.png')
orig = np.array(orig)
decoded = np.zeros_like(orig)
decoded[0, :] = orig[0,:]
decoded[:,0] = orig[:,0]
predict = np.zeros_like(orig)
err = np.zeros_like(orig)
diff = np.zeros_like(orig)
quan = np.zeros_like(orig)
dequan = np.zeros_like(orig)
return orig, decoded, predict, err, diff, quan, dequan
def uniform_quantizer(no_bits):
no_levels = 2 ** no_bits
ranges = np.linspace(0, 256, no_levels + 1)
quantization = []
dequantization = np.zeros(no_levels, dtype=float)
for i in range(no_levels):
quantization.append(bin(i)[2:])
dequantization[i] = (ranges[i] + ranges[i + 1]) / 2
return ranges, quantization, dequantization
def predictF(decode, pred, diff, original, quan, err, dequan, dequantized, rang, no_levels):
rows, cols = decode.shape
for row in range(rows):
for col in range(cols):
if row == 0 or col == 0:
pred[row, col] = original[row, col]
else:
# Prediction
a = decode[row, col - 1]
b = decode[row - 1, col - 1]
c = decode[row - 1, col]
if b <= min(a, c):
pred[row, col] = max(a, c)
elif b >= max(a, c):
pred[row, col] = min(a, c)
else:
pred[row, col] = a + c - b
# Difference
diff[row, col] = original[row, col] - pred[row, col]
# Quantization
magnitude = abs(diff[row, col])
for j in range(no_levels):
if rang[j] <= magnitude < rang[j + 1]:
quan[row, col] = j
dequan[row, col] = dequantized[j] * np.sign(diff[row, col])
break
# Reconstruction
decode[row, col] = pred[row, col] + dequan[row, col]
decode[row, col] = np.clip(decode[row, col], 0, 255)
# Error
err[row, col] = original[row, col] - decode[row, col]