-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_coding.py
138 lines (79 loc) · 3.64 KB
/
advanced_coding.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
# python modules
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import os
# user defined modules
from utilities import *
# median function for advanced coding
def median(a, b, c):
v = [a, b, c]
v.sort()
return v[1]
# codes an image based on the guidelines directives
def advanced_coding(img):
# blank image
predicted_img = np.zeros_like(img)
# extracts the height and the width from the image
height, width = img.shape[0], img.shape[1]
# iterates through the rows (height)
for row in range(height):
# iterates through the cols (width)
for col in range(width):
if row == 0 and col == 0: # first pixel
predicted_img[row][col] = img[row][col] - 128
elif row == 0: # first row
predicted_img[row][col] = img[row][col - 1]
elif col == 0: # first col
predicted_img[row][col] = img[row - 1][col]
elif col == (width - 1): # last col
predicted_img[row][col] = median(img[row - 1][col], img[row][col - 1], img[row - 1][col - 1])
else: # other cases
predicted_img[row][col] = median(img[row - 1][col], img[row][col - 1], img[row - 1][col + 1])
return predicted_img
if __name__ == "__main__":
####### Setup #######
print_task(0, task_color="purple")
img_file_name = "spiderman"
img_extension = ".jpg"
current_dir = os.getcwd()
# path to reach the img
path_to_img = os.path.join(current_dir, "multimedia", "hw-1", "script", "imgs") + "/"
# loads the colored image
gray_img = mpimg.imread(path_to_img + img_file_name + img_extension).astype(np.int16)
# extracts the luminance if RGB
if gray_img[0][0].size > 1:
gray_img = np.dot(gray_img[..., :3], [1, 1, 1]) / 3
plot_figure(gray_img, 'Grayscale image', colorbar = False)
####### Task 1 #######
# Perform "advanced" predictive coding: first, the predictor 𝑝 is constructed by scanning the image
print_task(1, task_color="purple", number_color="red")
# performs advanced coding
predicted_img = advanced_coding(gray_img)
# calculates the prediction error
adv_coding_error_img = gray_img - predicted_img
# plots error
plot_figure(np.abs(adv_coding_error_img), 'Advanced coding prediction error', 'seismic')
####### Task 2 #######
# Compute entropy
print_task(2, task_color="purple", number_color="red")
occ, _ = np.histogram(adv_coding_error_img, bins = range(-255, 256))
# calculate the relative frequencies and remove any probability == 0
freqRel = occ / np.sum(occ)
p = freqRel[freqRel > 0]
# calculate the entropy
entropy_y = - np.sum(p * np.log2(p))
print(f"The entropy of the advanced prediction error of {img_file_name} is {entropy_y:.3f} bpp")
####### Task 3 #######
# Evaluate the number of bits required to encode the prediction error using Signed Exp-Golomb coding, and deduce the encoding bitrate.
print_task(3, task_color="purple", number_color="red")
# extracts image size
img_size = gray_img.shape[0] * gray_img.shape[1]
# calculates EG bits
exp_golomb_bit = exp_golomb_count(adv_coding_error_img.flatten())
# camputes the bitrate
exp_golomb_bpp = exp_golomb_bit / img_size
print(f"The number of bits for the advanced coding is {exp_golomb_bit}")
print(f"The bitrate of the advanced coding is {exp_golomb_bpp:.3f}\n")
### Show all the figures ###
plt.show()