This project implements predictive coding algorithms for image compression, comparing lossless and lossy approaches. The implementation processes an X-ray image and evaluates the compression performance using Signal-to-Noise Ratio (SNR) metrics.
Predictive coding is a technique used in image compression that predicts pixel values based on neighboring pixels and encodes only the prediction error (residual). This approach exploits spatial redundancy in images to achieve compression.
- Lossless Predictive Coding: Preserves all image information by encoding exact prediction errors
- Lossy Predictive Coding: Uses quantization to reduce data size while maintaining acceptable image quality
- SNR Analysis: Compares the performance of both methods using Signal-to-Noise Ratio calculations
- Uses a simple predictor:
predicted_value = (pixel_right + pixel_below) / 2 - Encodes the exact prediction error:
error = actual_value - predicted_value - No information loss - perfect reconstruction possible
- Uses an adaptive predictor that chooses between:
- Horizontal prediction:
predicted_value = pixel_left - Vertical prediction:
predicted_value = pixel_above
- Horizontal prediction:
- Selection is based on which direction has smaller gradient
- Quantizes the prediction error using a 6-level quantizer:
- Error ranges:
[-∞, -40/255],[-40/255, -10/255],[-10/255, 0],[0, 10/255],[10/255, 40/255],[40/255, +∞] - Quantized values:
-64/255,-16/255,-4/255,4/255,16/255,64/255
- Error ranges:
- Python 3.x
- NumPy
- Matplotlib
pip install numpy matplotlib- Ensure
x-ray.pngis in the same directory as the notebook - Open
PredictiveCoding.ipynbin Jupyter Notebook or JupyterLab - Run all cells sequentially
The notebook generates several visualizations:
- Original Image: Displays the input X-ray image and its histogram
- Lossless Results: Shows the prediction image and error histogram
- Lossy Results: Shows the quantized prediction image and error histogram
- SNR Comparison: Prints Signal-to-Noise Ratio for both methods
- SNR Lossless: ~79.59 dB (higher is better, lossless has perfect reconstruction)
- SNR Lossy: ~65.73 dB (lower due to quantization, but still acceptable quality)
ImageCoding_PredictiveCoding/
├── PredictiveCoding.ipynb # Main notebook with implementation
├── x-ray.png # Input image
├── README.md # This file
└── LICENSE # License file
- The lossless method provides perfect reconstruction but requires storing all prediction errors
- The lossy method achieves better compression at the cost of some image quality
- SNR values may vary slightly depending on the input image characteristics
This project is licensed under the MIT License - see the LICENSE file for details.
Predictive coding is a fundamental technique in image compression, used in standards like JPEG-LS and as a component in more complex codecs.