-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjpeg_compression.py
460 lines (397 loc) · 15.9 KB
/
jpeg_compression.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# Import the required modules
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
import os
# Define the quantization matrix
quantization_matrix = np.array(
[
[16, 11, 10, 16, 24, 40, 51, 61],
[12, 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, 101],
[72, 92, 95, 98, 112, 100, 103, 99],
],
dtype=np.float32,
)
def calculate_psnr(img1: np.ndarray[np.uint8], img2: np.ndarray[np.uint8]) -> float:
"""Calculate PSNR using formula: PSNR = 20 * log10(MAX_I) - 10 * log10(MSE)"""
mse = np.mean((img1 - img2) ** 2)
psnr = 20 * np.log10(255 / np.sqrt(mse))
return psnr
def number_of_elements(blocks: list[np.ndarray[np.int32]]) -> int:
"""Calculates the total number of elements in the grayscale JPEG encoded array"""
total_elements = 0
for block in blocks:
# Trim the trailing zeros from the 1D array
total_elements += np.trim_zeros(block, "b").size
return total_elements
def total_number_of_elements(
blocks: list[np.ndarray[np.int32]]
| tuple[
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
],
color: bool,
) -> int:
"""
Calculates the total number of elements for both color and grayscale JPEG encoded arrays
This is a utility function that will be used to calculate the compression ratio
"""
total_elements = 0
if color:
# Add the number of elements for each color channel
total_elements = (
number_of_elements(blocks[0])
+ number_of_elements(blocks[1])
+ number_of_elements(blocks[2])
)
else:
total_elements = number_of_elements(blocks)
return total_elements
def zigzag_scan(block: np.ndarray[np.int32]) -> np.ndarray[np.int32]:
"""
Scans a block in zigzag order and return a 1D array
Each block is assumed to be a square matrix
"""
block_size = block.shape[0]
zigzag_arr = np.concatenate(
[
np.diagonal(block[::-1, :], i)[:: (2 * (i % 2) - 1)]
for i in range(1 - block_size, block_size)
]
)
return zigzag_arr
def zigzag_unscan(
zigzag_arr: np.ndarray[np.int32], block_size: int
) -> np.ndarray[np.float32]:
"""Unscans a 1D array in zigzag order and return a 2D array"""
# Create an empty 2D array to store the unscanned values
block = np.zeros((block_size, block_size), dtype=np.float32)
x, y = 0, 0
for num in zigzag_arr:
# Set the current value in the corresponding coordinate of the 2D array
block[x, y] = num
# Determine the direction to move based on the current position
# if the sum of the coordinates is even
if (x + y) % 2 == 0:
# if at the last column, move down one row
if y == block_size - 1:
x += 1
# if at the first row, move right one column
elif x == 0:
y += 1
# otherwise move up one row and right one column
else:
x -= 1
y += 1
# if the sum of the coordinates is odd
else:
# if at the last row, move right one column
if x == block_size - 1:
y += 1
# if at the first column, move down one row
elif y == 0:
x += 1
# otherwise move down one row and left one column
else:
x += 1
y -= 1
# return the 2D array with the unscanned values
return block
def grayscale_jpeg_encoder(
img: np.ndarray[np.uint8], block_size: int, num_coefficients: int
) -> list[np.ndarray[np.int32]]:
"""
Encodes a grayscale image using JPEG compression
Returns a list of 1D arrays containing the first `num_coefficients`
coefficients after performing zigzag scanning on each quantized block
This is the JPEG encoded array
"""
# Pad the image to make it divisible by the block size
height, width = img.shape
padded_height = height + (block_size - height % block_size) % block_size
padded_width = width + (block_size - width % block_size) % block_size
padded_img = np.zeros((padded_height, padded_width), dtype=np.uint8)
padded_img[:height, :width] = img
# Subtract 128 from the image
padded_img = padded_img.astype(np.float32) - 128
# Split the image into blocks of the given size
blocks = [
padded_img[i : i + block_size, j : j + block_size]
for i in range(0, padded_height, block_size)
for j in range(0, padded_width, block_size)
]
# Apply the Discrete Cosine Transform (DCT) to each block
dct_blocks = [cv.dct(block) for block in blocks]
# Resize the quantization matrix to match the block size
resized_quantization_matrix = cv.resize(
quantization_matrix, (block_size, block_size), cv.INTER_CUBIC
)
# Quantize each DCT coefficient by dividing with the resized quantization matrix
quantized_blocks = [
np.round(block / resized_quantization_matrix).astype(np.int32)
for block in dct_blocks
]
# Perform zigzag scanning on each quantized block
zigzag_scanned_blocks = [zigzag_scan(block) for block in quantized_blocks]
# Retain only the first `num_coefficients` coefficients in each block
first_num_coefficients = [
block[:num_coefficients] for block in zigzag_scanned_blocks
]
return first_num_coefficients
def grayscale_jpeg_decoder(
blocks: list[np.ndarray[np.int32]], img: np.ndarray[np.uint8], block_size: int
) -> np.ndarray[np.uint8]:
"""
Decodes a grayscale image using JPEG compression from the JPEG encoded array
Returns a 2D array containing the compressed image
"""
# Calculated the padded height and width of the image
height, width = img.shape
padded_height = height + (block_size - height % block_size) % block_size
padded_width = width + (block_size - width % block_size) % block_size
# Resize the quantization matrix to match the block size
resized_quantization_matrix = cv.resize(
quantization_matrix, (block_size, block_size), cv.INTER_CUBIC
)
# Unscan the zigzag scanned blocks to get the quantized blocks
zigzag_unscanned_blocks = [zigzag_unscan(block, block_size) for block in blocks]
# Dequantize the quantized blocks using the resized quantization matrix
dequantized_blocks = [
block * resized_quantization_matrix for block in zigzag_unscanned_blocks
]
# Apply the Inverse Discrete Cosine Transform (IDCT) to each dequantized block
idct_blocks = [cv.idct(block) for block in dequantized_blocks]
# Reconstruct the compressed image from the IDCT blocks
compressed_img = np.zeros((padded_height, padded_width), dtype=np.float32)
block_index = 0
for i in range(0, padded_height, block_size):
for j in range(0, padded_width, block_size):
compressed_img[i : i + block_size, j : j + block_size] = idct_blocks[
block_index
]
block_index += 1
compressed_img += 128
# Crop the image back to its original size
compressed_img = np.clip(compressed_img, 0, 255)
return compressed_img[:height, :width].astype(np.uint8)
def color_jpeg_encoder(
img: np.ndarray[np.uint8], block_size: int, num_coefficients: int
) -> tuple[
list[np.ndarray[np.int32]], list[np.ndarray[np.int32]], list[np.ndarray[np.int32]]
]:
"""
Encodes a color image using JPEG compression
Returns a tuple of 3 lists, each containing
1D arrays containing the first `num_coefficients`
coefficients after performing zigzag scanning on each quantized block
This is the JPEG encoded array
The three lists correspond to the blue, green, and red channels respectively
"""
# Split the image into blue, green and red channels
blue_channel, green_channel, red_channel = cv.split(img)
# Encode each channel using grayscale_jpeg_encoder
return (
grayscale_jpeg_encoder(blue_channel, block_size, num_coefficients),
grayscale_jpeg_encoder(green_channel, block_size, num_coefficients),
grayscale_jpeg_encoder(red_channel, block_size, num_coefficients),
)
def color_jpeg_decoder(
blocks: tuple[
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
],
img: np.ndarray[np.uint8],
block_size: int,
) -> np.ndarray[np.uint8]:
"""
Decodes a JPEG encoded color image
Returns a 3D array containing the compressed image
"""
# Split the grayscale image into its color channels
blue_channel, green_channel, red_channel = cv.split(img)
# Decode each color channel using grayscale_jpeg_decoder
blue_channel = grayscale_jpeg_decoder(blocks[0], blue_channel, block_size)
green_channel = grayscale_jpeg_decoder(blocks[1], green_channel, block_size)
red_channel = grayscale_jpeg_decoder(blocks[2], red_channel, block_size)
# Merge the decoded color channels into a color image
return cv.merge((blue_channel, green_channel, red_channel))
def jpeg_encoder(
img_path: str,
block_size: int,
num_coefficients: int,
color: bool,
) -> (
list[np.ndarray[np.int32]]
| tuple[
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
]
):
"""
Encodes an image using JPEG compression
Returns the JPEG encoded array
"""
if color:
# Load color image and apply color JPEG encoder
img = cv.imread(img_path, cv.IMREAD_COLOR)
return color_jpeg_encoder(img, block_size, num_coefficients)
else:
# Load grayscale image and apply grayscale JPEG encoder
img = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
return grayscale_jpeg_encoder(img, block_size, num_coefficients)
def jpeg_decoder(
blocks: list[np.ndarray[np.int32]]
| tuple[
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
],
img_path: str,
block_size: int,
color: bool,
) -> np.ndarray[np.uint8]:
"""
Decodes an image using JPEG compression from its JPEG encoded array
Returns a 2D or 3D array containing the compressed image
"""
if color:
img = cv.imread(img_path, cv.IMREAD_COLOR)
return color_jpeg_decoder(blocks, img, block_size)
else:
img = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
return grayscale_jpeg_decoder(blocks, img, block_size)
def analyze_image(
img_path: str, block_size: int, num_coefficients: int, color: bool
) -> tuple[
np.ndarray[np.uint8],
np.ndarray[np.uint8],
float,
float,
list[np.ndarray[np.int32]]
| tuple[
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
],
bool,
]:
"""
Analyzes the input image by performing JPEG compression,
Returns the original and compressed images, and the PSNR and compression ratio
This can be used to compare the quality of the compressed image
"""
# Read the image
img: np.ndarray[np.uint8] = None
if color:
img = cv.imread(img_path, cv.IMREAD_COLOR)
else:
img = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
# Encode the image using JPEG compression
encoded_img = jpeg_encoder(img_path, block_size, num_coefficients, color)
# Decode the image using JPEG compression
compressed_img = jpeg_decoder(encoded_img, img_path, block_size, color)
# Calculate the PSNR between the original and compressed images
psnr = cv.PSNR(img, compressed_img)
# Calculate the compression ratio
n2 = total_number_of_elements(encoded_img, color)
if n2 == 0:
# In this case, the compression ratio is very high
# But, we set it to 0 to avoid division by 0 so that our analysis becomes easier
compression_ratio = 0
else:
compression_ratio = img.size / total_number_of_elements(encoded_img, color)
# Return the original image, compressed image, PSNR, and compression ratio
# Also return the encoded image and whether the image is color or not
# The encoded image is returned so that it can be written in a text file
return (img, compressed_img, psnr, compression_ratio, encoded_img, color)
def plot_images(
img: np.ndarray[np.uint8],
compressed_img: np.ndarray[np.uint8],
psnr: float,
compression_ratio: float,
encoded_img: list[np.ndarray[np.int32]]
| tuple[
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
list[np.ndarray[np.int32]],
],
color: bool,
) -> None:
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
fig.suptitle(
"PSNR = {:.2f}\nCompression Ratio = {:.2f}".format(psnr, compression_ratio)
)
with open("encoded_image.txt", "w") as f:
if color:
axs[0].imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
axs[1].imshow(cv.cvtColor(compressed_img, cv.COLOR_BGR2RGB))
for row in zip(*encoded_img):
for element in row:
f.write(str(element) + " ")
f.write("\n")
else:
axs[0].imshow(img, cmap="gray")
axs[1].imshow(compressed_img, cmap="gray")
for row in encoded_img:
for element in row:
f.write(str(element) + " ")
f.write("\n")
axs[0].set_title("Original Image")
axs[1].set_title("Compressed Image")
plt.show()
def plot_graph(
img_dir_path: str,
color: bool,
):
psnr_list = []
compression_ratio_list = []
for num_coefficients in [1, 3, 6, 10, 15, 28]:
psnr_values = []
compression_ratio_values = []
for img_file in os.listdir(img_dir_path):
img_path = os.path.join(img_dir_path, img_file)
_, _, psnr, compression_ratio, _, _ = analyze_image(
img_path, 8, num_coefficients, color
)
psnr_values.append(psnr)
compression_ratio_values.append(compression_ratio)
psnr_list.append(np.mean(psnr_values))
compression_ratio_list.append(np.mean(compression_ratio_values))
plt.plot(compression_ratio_list, psnr_list, "o")
plt.xlabel("Compression Ratio")
plt.ylabel("PSNR")
plt.title("PSNR vs Compression Ratio")
plt.show()
# ======================================== Uncomment the following lines to test the code ========================================
# ======================================== You can run both these functions one by one ========================================
if __name__ == "__main__":
"""
Replace the image path with the path to your image
plot_images function plots the original and compressed images
Also, it wries the encoded images to a text file encoded_image.txt
"""
# plot_images(*analyze_image(img_path="path/to/your/image", block_size=8, num_coefficients=10, color=True))
"""
Replaces the images folder with the path to your images folder
plot_graph function plots the PSNR vs Compression Ratio graph
for all the images in the images folder for different values of num_coefficients
"""
# plot_graph(img_dir_path="path/to/your/image/folder", color=False)
if input("Analyze a single image (y/n): ") == "y":
img_path = input("Enter the path to the image: ")
block_size = int(input("Enter the block size (even): "))
num_coefficients = int(input("Enter the number of coefficients passed: "))
color = input("Is the image color (y/n): ") == "y"
plot_images(*analyze_image(img_path, block_size, num_coefficients, color))
elif input("Analyzes all images in a folder (y/n): ") == "y":
img_dir_path = input("Enter the path to the images folder: ")
color = input("Are the images color (y/n): ") == "y"
plot_graph(img_dir_path, color)