This project implements two different algorithms for image compression: JPEG and LZW. JPEG is a lossy compression in which some of the data from the original image is lost, while LZW is lossless, meaning that the compressed image can be reconstructed exactly to the original without any loss of information.
LZW is a compression algorithm that exploit the spatial and statistical redundancies in the data by replacing frequently occurring patterns with shorter codes. The LZW encoder in this project takes a grayscale image, block size and the maximum allowed dictionary size as input and applies the following steps:
-
Divide the image into blocks of the specified size (or treat the whole image as a single block if the block size is set to -1).
-
Flatten each block into a 1D array of pixel values.
-
Apply the LZW compression algorithm on each block to obtain the corresponding encoded output.
-
Store the encoded outputs
The LZW decoder in this project reverses the above steps to reconstruct the original image from the encoded data. It applies the LZW decoding algorithm to obtain the decoded output for each block and concatenates them to obtain the reconstructed image. The RMSE between the original and the reconstructed image is zero for lossless compression.
JPEG (Joint Photographic Experts Group) is a popular image compression standard that achieves high compression ratios by applying a series of mathematical transformations on the image data. The JPEG encoder in this project takes a color/grayscale image as input and applies the following steps:
-
Divide the image into blocks.
-
Apply a discrete cosine transform (DCT) on each block to convert it into a frequency domain representation.
-
Quantize the DCT coefficients by dividing by the quantization matrix.
-
Perform zigzag scanning for each block.
-
Truncate each encode block to the specified number of coefficient parameters and return the encoded image
The JPEG decoder reverses the above steps to reconstruct the original image from the compressed data.
The algorithms were tested for a data set of color and grayscale images.
Sample output:
JPEG Compression
LZW Compression