|
1 | 1 | import numpy as np
|
2 | 2 | import matplotlib.pyplot as plt
|
| 3 | +import plotly.graph_objects as go |
3 | 4 | from itertools import groupby
|
4 | 5 | import time
|
5 | 6 | from dahuffman import HuffmanCodec
|
@@ -348,65 +349,47 @@ def PSNR(original_image, compressed_image):
|
348 | 349 | return 100
|
349 | 350 | max_pixel = 255.0
|
350 | 351 | psnr = 10 * np.log10(max_pixel ** 2 / mse)
|
351 |
| - return psnr |
| 352 | + return round(psnr, 2) |
352 | 353 |
|
353 |
| -# (PSNR vs quantization scale) |
354 |
| -def rate_distortion_curve(gray_image, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals): |
| 354 | +# PSNR rate-distortion curve |
| 355 | +def rate_distortion_curve(gray_image, type, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals): |
355 | 356 | # Create an empty array to store the PSNR values
|
356 | 357 | psnr_values = []
|
| 358 | + x_values = [] |
| 359 | + quantization_levels = np.arange(0.1, 1.1, 0.1) |
357 | 360 |
|
358 |
| - # Iterate over the quantization matrix |
359 |
| - for i in range(1, 100): |
360 |
| - # Print the current quantization scale |
361 |
| - print(f"Quantization scale: {i}") |
362 |
| - |
363 |
| - # Encode the image |
364 |
| - encoded_image, codec = encode(gray_image, block_size, quantization_matrix * i, num_blocks_height, num_blocks_width, block_size) |
365 |
| - |
366 |
| - # Decode the image |
367 |
| - decoded_image = decode(encoded_image, codec, quantization_matrix * i, num_blocks_height, num_blocks_width, block_size) |
368 |
| - |
369 |
| - # Calculate the PSNR |
370 |
| - psnr = PSNR(gray_image, decoded_image) |
371 |
| - |
372 |
| - # Append the PSNR value |
373 |
| - psnr_values.append(psnr) |
374 |
| - |
375 |
| - # Plot the rate-distortion curve |
376 |
| - plt.plot(range(1, 100), psnr_values) |
377 |
| - plt.xlabel('Compression Rate') |
378 |
| - plt.ylabel('PSNR') |
379 |
| - plt.show() |
380 |
| - |
381 |
| -# (PSNR vs data size) |
382 |
| -def rate_distortion_curve_2(gray_image, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals): |
383 |
| - # Create an empty array to store the PSNR values |
384 |
| - psnr_values = [] |
385 |
| - data_size = [] |
386 |
| - |
387 |
| - # Iterate over the quantization matrix |
388 |
| - for i in range(1, 100): |
389 |
| - # Print the current quantization scale |
390 |
| - print(f"Quantization scale: {i}") |
| 361 | + # Control of the compression rate |
| 362 | + for i in quantization_levels: |
| 363 | + |
| 364 | + print(f"Quantization level: {i}") |
391 | 365 |
|
392 | 366 | # Encode the image
|
393 |
| - encoded_image, codec = encode(gray_image, quantization_matrix * i, num_blocks_height, num_blocks_width, block_size) |
| 367 | + bitstream, codec = encode(gray_image, quantization_matrix * i, num_blocks_height, num_blocks_width, block_size, decimals) |
394 | 368 |
|
395 | 369 | # Decode the image
|
396 |
| - decoded_image = decode(encoded_image, codec, quantization_matrix * i, num_blocks_height, num_blocks_width, block_size) |
| 370 | + decoded_image = decode(bitstream, codec, quantization_matrix * i, num_blocks_height, num_blocks_width, block_size, decimals) |
397 | 371 |
|
398 | 372 | # Calculate the PSNR
|
399 | 373 | psnr = PSNR(gray_image, decoded_image)
|
400 | 374 |
|
401 |
| - # Append the PSNR value |
| 375 | + # Append the values |
| 376 | + x_values.append(len(bitstream)) |
402 | 377 | psnr_values.append(psnr)
|
403 |
| - data_size.append(len(encoded_image) * 8) |
404 | 378 |
|
405 |
| - # Plot the rate-distortion curve |
406 |
| - plt.plot(data_size, psnr_values) |
407 |
| - plt.xlabel('Data Size') |
408 |
| - plt.ylabel('PSNR') |
409 |
| - plt.show() |
| 379 | + if type == 'bpp': |
| 380 | + x_values = np.array(x_values) * 8 / (gray_image.shape[0] * gray_image.shape[1]) |
| 381 | + label = 'Bit per pixel (BPP)' |
| 382 | + elif type == 'scale': |
| 383 | + x_values = quantization_levels |
| 384 | + label = 'Quantization Scale' |
| 385 | + elif type == 'size': |
| 386 | + label = 'File Size (bytes)' |
| 387 | + else: |
| 388 | + raise Exception('Invalid type') |
| 389 | + |
| 390 | + fig = go.Figure(data=go.Scatter(x=x_values, y=psnr_values, mode='lines+markers', name='lines+markers', text=np.round(quantization_levels, 1))) |
| 391 | + fig.update_layout(title='Rate-Distortion Curve (PSNR vs. Quantization Scale)', xaxis_title=label, yaxis_title='PSNR (dB)') |
| 392 | + fig.show() |
410 | 393 |
|
411 | 394 | def display_images(original_image, compressed_image):
|
412 | 395 | # Create a figure
|
@@ -445,15 +428,14 @@ def display_images(original_image, compressed_image):
|
445 | 428 |
|
446 | 429 | quantization_matrix *= 1
|
447 | 430 | gray_image = open_raw_image(filename, image_width, image_height)
|
448 |
| - t = time.time() |
449 |
| - encoded_image, codec = encode(gray_image, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
450 |
| - print(f"Encoding time: {round(time.time() - t, 2)}s") |
451 |
| - t1 = time.time() |
452 |
| - decoded_image = decode(encoded_image, codec, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
453 |
| - print(f"Decoding time: {round(time.time() - t1, 2)}s") |
454 |
| - |
455 |
| - compression_quality(gray_image, encoded_image) |
456 |
| - display_images(gray_image, decoded_image) |
457 |
| - |
458 |
| - # rate_distortion_curve(gray_image, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
459 |
| - # rate_distortion_curve_2(gray_image, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
| 431 | + # t = time.time() |
| 432 | + # encoded_image, codec = encode(gray_image, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
| 433 | + # print(f"Encoding time: {round(time.time() - t, 2)}s") |
| 434 | + # t1 = time.time() |
| 435 | + # decoded_image = decode(encoded_image, codec, quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
| 436 | + # print(f"Decoding time: {round(time.time() - t1, 2)}s") |
| 437 | + |
| 438 | + # compression_quality(gray_image, encoded_image) |
| 439 | + # display_images(gray_image, decoded_image) |
| 440 | + |
| 441 | + rate_distortion_curve(gray_image, "size", quantization_matrix, num_blocks_height, num_blocks_width, block_size, decimals) |
0 commit comments