Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Signals Report

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Signals Project Report

NAME ID SEC

Mina Hany William 9220895 2

Nour Khaled 9220921 2

Presented To
Dr. Micheal Melek
Eng. Sayed Kamel
Contents

1- Imports

2- Read the image file

3- display each of its three color components.

4- Compressing Image Using Discrete Cosines


Transform (DCT)

5- Compare the size of the original and compressed


images.

6- Decompress the image by applying inverse 2D


DCT

7- Showing images after decompression

8- Computing PSNR

9- curve displaying the PSNR against m

10- Code

11- References

1
1- Imports

Image 1: Importing the used libraries in project.

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.

2- Read the image file

Image 2: Getting Image Data.

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.

Image 3: Red component of Image.

Image 4: Green component of Image.

Image 5: Blue component of Image.

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. .

Image 5: Code For Compressing Image using DCT.

5- Compare the size of the original and compressed


images.

Image Size (MB)


Original 5.93
Compressed with m = 1 0.19
Compressed with m = 2 0.74
Compressed with m = 3 1.67
Compressed with m = 4 2.97

4
6- Decompress the image by applying inverse 2D
DCT

To get the original image we decompress the compressed image using


IDCT but on the retained mxm block with ignoring the other values by
setting them to zeros.

Image 6: Code For Decompressing The Compressed Image.

7- Showing images after decompression

Image 7: Original Image

5
Image 8: Decompressed image using m = 1

Image 9: Decompressed image using m = 2

6
Image 10: Decompressed image using m = 3

Image 11: Decompressed image using m = 4

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

Image 12: Code For Calculating PSNR.

Image 13: Code For Printing PSNR.

8
9- curve displaying the PSNR against m

Image 14: curve displaying the PSNR against m

It’s obvious that by increasing m we take more effective frequencies,thus we enhance

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))

# Process each color component in blocks of 8x8 pixels


for i in range(0, img_array.shape[0], 8):
for j in range(0, img_array.shape[1], 8):
for channel in range(3):
block = img_array[i:i+8, j:j+8, channel]
# Apply 2D DCT
dct_block = dct(dct(block, axis=0, norm='ortho'), axis=1, norm='ortho')
# Retain only the top-left square of the DCT coefficients
compressed_block = dct_block[:m, :m]
compressed_image[m*(i//8):m*((i//8)+1),m*(j//8):m*((j//8)+1),channel] = compressed_block
return compressed_image

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))

# Process each color component in blocks of 8x8 pixels


for i in range(0, img_array.shape[0], 8):
for j in range(0, img_array.shape[1], 8):
for channel in range(3):
block = img_array[i:i+8, j:j+8, channel]
# Apply 2D DCT
dct_block = dct(dct(block, axis=0, norm='ortho'), axis=1, norm='ortho')
# Retain only the top-left square of the DCT coefficients
compressed_block = dct_block[:m, :m]
compressed_image[m*(i//8):m*((i//8)+1),m*(j//8):m*((j//8)+1),channel] = compressed_block
return compressed_image

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))

# Iterate over each block and each channel to decompress


for i in range(0, compressed_image.shape[0],m):
for j in range(0, compressed_image.shape[1],m):
for channel in range(3):
# Retrieve the compressed block
compressed_block = compressed_image[i:i+m, j:j+m, channel]

# 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

# Apply inverse 2D DCT


idct_block = idct(idct(dct_block.T, norm='ortho').T, norm='ortho')

# 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()

def calculate_psnr(original_image, decompressed_image):


max_pixel = 255.0 # Maximum pixel value for an 8-bit image

# 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()

Running on Google Colab

11- References
1. Matlab DCT explaination
2. Wikipedia DCT explaination

13

You might also like