This MATLAB project implements lossless color image compression by combining the PNG format and Huffman coding for further optimization. The aim is to reduce the image size while maintaining the original quality. Additionally, the PSNR (Peak Signal-to-Noise Ratio) is calculated to measure the quality of the decoded image compared to the original.
- PNG Compression: Saves the image in PNG format, a widely used lossless compression method.
- Huffman Coding: Each color channel (Red, Green, Blue) is encoded separately using Huffman coding, further compressing the image data.
- Compression Ratio: Displays the ratio of the original image size to the compressed image size.
- PSNR Calculation: Provides the PSNR value to evaluate the quality of the decompressed image.
- Image Selection: The user selects an image file from their system (supports PNG, JPG, BMP, TIFF).
- PNG Compression: The selected image is compressed using the PNG format.
- Huffman Coding: Each color channel (R, G, B) of the image is encoded using Huffman coding.
- Huffman Decoding: The encoded image is decoded to restore the original image.
- Comparison: The script compares the original image, compressed PNG, and Huffman-encoded data sizes.
- PSNR Calculation: The Peak Signal-to-Noise Ratio is calculated to measure the similarity between the original and decoded images.
- Main Script: Contains the complete code for reading, compressing, encoding, decoding, and calculating the PSNR of the image.
- encoded_image.mat: Stores the Huffman-encoded data and dictionaries for each color channel.
- compressed_image.png: The PNG-compressed version of the selected image.
- decoded_image.png: The image after Huffman decoding, saved as PNG for comparison.
You need MATLAB installed on your system to run this project.
- Clone this repository:
git clone https://github.com/mrinankmj/lossless_image_compression.git
- Navigate to the project directory in MATLAB.
- Run the
LOSS.m
script:LOSS
- A file selector window will appear, prompting you to select an image from your system (PNG, JPG, BMP, or TIFF).
- After selecting the image, the script will:
- Display the original and decoded images.
- Show the compression ratio and the PSNR of the decoded image.
- Save the compressed and decoded images in the project folder.
Compression Ratio: 1.45
Original image size: 156.24 KB
Compressed image size (PNG): 108.75 KB
Encoded Huffman data size: 98.34 KB
Decoded image size: 156.22 KB
PSNR: 40.12 dB
- Read the Image: Allows the user to select an image file from their device.
- PNG Compression: Uses MATLAB’s
imwrite
function to save the image as a PNG. - Huffman Encoding: The script splits the image into Red, Green, and Blue channels and compresses each channel using Huffman coding.
- Decoding: The encoded image is decoded using the Huffman dictionaries and reshaped to its original size.
- PSNR Calculation: The quality of the decoded image is measured using the
psnr
function, comparing it to the original image.
- huffman_encode: Encodes a color channel using Huffman coding based on pixel intensity frequencies.
- huffman_decode: Decodes the Huffman-encoded data for a color channel and reshapes it to the original dimensions.
Feel free to submit pull requests or issues if you find any bugs or want to enhance the functionality.