Signals Report
Signals Report
Signals Report
NAME ID SEC
Presented To
Dr. Micheal Melek
Eng. Sayed Kamel
Contents
1- Imports
8- Computing PSNR
10- Code
11- References
1
1- Imports
1- Numpy (np)
Library in python that deals with mathematical operations on vectors and
scalars.
2- Matplotlib
Library in python for plotting data.
3- Scipy
Library in python that deals with operations on signals like DCT and
IDCT.
4- PIL (Image)
Library in python that deals with images and processing it.
Reading The image using (PIL), we can get the image pixels as a 3D array.The first
dimension is the row of the pixel,The second one is the column of the pixel and the third is
the intensity of the color RGB which varies from 0 to 255.
2
3- display each of its three color components.
3
4- Compressing Image Using Discrete Cosines
Transform (DCT)
As stated in the project document , We deal with each color channel of image
matrix as group of blocks. Each block is 8x8 pixels. We apply to each block the
DCT such that we get the lower frequencies that has the higher impact on eye
(top left square of the 2D for different M) and neglect the other frequencies. .
4
6- Decompress the image by applying inverse 2D
DCT
5
Image 8: Decompressed image using m = 1
6
Image 10: Decompressed image using m = 3
7
8- Computing PSNR
The quality of the decompressed image is measured using the Peak
Signal-to-Noise Ratio (PSNR), which is defined by
Where peak is the max value of pixel data type and MSE is Mean square error
8
9- curve displaying the PSNR against m
its quality more and more and increasing the PSNR also. Thus the PSNR is directly
propotional with m.
9
10- Code
###############################Imports###########################################
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy.fft import dct
from scipy.fft import idct
#Reads image and convert to rgb
with Image.open("/content/image1.png").convert('RGB') as img:
img_array = np.array(img)
img_array.shape
colors = ["Reds","Greens","Blues"]
for i in range(3):
plt.figure(figsize=(14, 6))
plt.imshow(img_array[:,:,i],cmap=colors[i])
plt.axis("off")
plt.colorbar()
plt.savefig(f"a_{colors[i][:-1].lower()}.png")
plt.show()
def dct_compress(img_array, m):
# Calculate the size of the compressed image array
compressed_height = m * ((img_array.shape[0] + 7) // 8)
compressed_width = m * ((img_array.shape[1] + 7) // 8)
compressed_image = np.zeros((compressed_height, compressed_width, 3))
10
def dct_decompress(compressed_image, m):
# Initialize array for the decompressed image
decompressed_height = ((compressed_image.shape[0] + m - 1) // m) * 8
decompressed_width = ((compressed_image.shape[1] + m - 1) // m) * 8
decompressed_image_array = np.zeros((decompressed_height, decompressed_width, 3))
# Create a block of zeros with the same shape as the original block
dct_block = np.zeros((8, 8))
# Fill the top-left square of the block with the retained DCT coefficients
dct_block[:m, :m] = compressed_block
# Assign the decompressed block to the corresponding part of the decompressed image
decompressed_image_array[8*(i//m):8*((i//m)+1),8*(j//m):8*((j//m)+1), channel] = idct_block
return decompressed_image_array.astype(np.uint8)
def display_image(image_array,m):
plt.imshow(image_array)
plt.axis('off') # Turn off axis numbers
plt.savefig(f"decompressed_image_{m}.png")
plt.show()
# Calculate MSE
mse = np.mean((original_image - decompressed_image) ** 2)
if mse == 0:
return float('inf') # PSNR is infinite if MSE is zero
# Calculate PSNR
psnr = 10 * np.log10((max_pixel ** 2) / mse)
return psnr
11
#m->4
m=4
compressed_image_m_4 = dct_compress(img_array , m)
compressed_image_m_4
original_size = img_array.size
compressed_size = compressed_image_m_4.size
print(f"Original size: {original_size} ")
print(f"Compressed size: {compressed_size} ")
decompressed_image_array_m_4 = dct_decompress(compressed_image_m_4,m)
decompressed_image_array_m_4
decompressed_image_array_m_4.size
display_image(decompressed_image_array_m_4,m)
psnr_m_4 = calculate_psnr(img_array,decompressed_image_array_m_4)
print(f"PSNR for m=4: {psnr_m_4:.2f} dB")
#m->3
m=3
compressed_image_m_3 = dct_compress(img_array , m)
compressed_image_m_3
original_size = img_array.size
compressed_size = compressed_image_m_3.size
print(f"Original size: {original_size} ")
print(f"Compressed size: {compressed_size} ")
decompressed_image_array_m_3 = dct_decompress(compressed_image_m_3,m)
decompressed_image_array_m_3
decompressed_image_array_m_3.size
display_image(decompressed_image_array_m_3,m)
psnr_m_3 = calculate_psnr(img_array,decompressed_image_array_m_3)
print(f"PSNR for m=3: {psnr_m_3:.2f} dB")
#m->2
m=2
compressed_image_m_2 = dct_compress(img_array , m)
compressed_image_m_2
original_size = img_array.size
compressed_size = compressed_image_m_2.size
print(f"Original size: {original_size} ")
print(f"Compressed size: {compressed_size} ")
decompressed_image_array_m_2 = dct_decompress(compressed_image_m_2,m)
decompressed_image_array_m_2
decompressed_image_array_m_2.size
display_image(decompressed_image_array_m_2,m)
psnr_m_2 = calculate_psnr(img_array,decompressed_image_array_m_2)
print(f"PSNR for m=2: {psnr_m_2:.2f} dB")
12
#m->1
m=1
compressed_image_m_1 = dct_compress(img_array , m)
compressed_image_m_1
original_size = img_array.size
compressed_size = compressed_image_m_1.size
print(f"Original size: {original_size} ")
print(f"Compressed size: {compressed_size} ")
decompressed_image_array_m_1 = dct_decompress(compressed_image_m_1,m)
decompressed_image_array_m_1
decompressed_image_array_m_1.size
display_image(decompressed_image_array_m_1,m)
psnr_m_1 = calculate_psnr(img_array,decompressed_image_array_m_1)
print(f"PSNR for m=1: {psnr_m_1:.2f} dB")
#curve
m_values = [1, 2, 3, 4]
psnr_values = [psnr_m_1, psnr_m_2, psnr_m_3, psnr_m_4]
# Plot the graph
plt.figure(figsize=(8, 6))
plt.plot(m_values, psnr_values, marker='o', linestyle='-')
plt.xlabel('m')
plt.ylabel('PSNR (dB)')
plt.title('PSNR vs. m for Image Compression')
plt.grid(True)
plt.xticks(m_values)
plt.show()
11- References
1. Matlab DCT explaination
2. Wikipedia DCT explaination
13