TP-4
ipynb6 kBPython · 2 cellsSolution of TP 4: Image compression
TP-4: Image Compression
1. Prediction-Based Coding (Lossless)
Based on lecture pages 10-13, prediction coding reduces image entropy by coding differences instead of absolute pixel values.
Objective
Students implement simple 1-D and 2-D predictors and compute the prediction error image.
Tasks
-
Load a grayscale image.
-
Predict each pixel using one of the predictors from the slides (page 10):
- Predictor : Left neighbor
- Predictor : Upper neighbor
- Predictor (), etc.
-
Compute the difference/error image:
-
Display:
- original image
- predicted image
- error image
Python functions to use
- cv2.imread()
- numpy.roll() or direct indexing
- Display using matplotlib.pyplot.imshow(
import cv2
import numpy as np
import matplotlib.pyplot as plt
# ==========================
# 1. Load grayscale image
# ==========================
# Change this to your actual path
image_path = "image.jpg"
# Read as grayscale
orig = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if orig is None:
raise FileNotFoundError(f"Could not load image at path: {image_path}")
orig = orig.astype(np.int16) # use int16 to avoid negative overflow in error
H, W = orig.shape
# ===================================
# 2. Build neighbor images A, B, C
# A = left, B = upper-left, C = up
# ===================================
# roll shifts, we fix borders manually
A = np.roll(orig, shift=1, axis=1) # left neighbor
C = np.roll(orig, shift=1, axis=0) # upper neighbor
B = np.roll(C, shift=1, axis=1) # upper-left (shift left from C)
# Fix first column for A and B (no left neighbor)
A[:, 0] = orig[:, 0]
B[:, 0] = C[:, 0]
# Fix first row for C and B (no upper neighbor)
C[0, :] = orig[0, :]
B[0, :] = orig[0, :]
# ===================================
# Choose a predictor
# Uncomment ONE of these blocks
# ===================================
# --- Predictor A: left neighbor ---
pred_A = A.copy()
# --- Predictor C: upper neighbor ---
pred_C = C.copy()
# --- Predictor A + B - C (2D) ---
pred_ABC = A + B - C
# Clip to valid pixel range [0, 255] just for visualization
pred_A_vis = np.clip(pred_A, 0, 255).astype(np.uint8)
pred_C_vis = np.clip(pred_C, 0, 255).astype(np.uint8)
pred_ABC_vis = np.clip(pred_ABC, 0, 255).astype(np.uint8)
# ===================================
# 3. Compute error image
# error = original - predicted
# ===================================
error_A = orig - pred_A
error_C = orig - pred_C
error_ABC = orig - pred_ABC
# For visualization: shift error to be visible
# (e.g., add 128 so that zero-error is mid gray)
error_A_vis = np.clip(error_A + 128, 0, 255).astype(np.uint8)
error_C_vis = np.clip(error_C + 128, 0, 255).astype(np.uint8)
error_ABC_vis = np.clip(error_ABC + 128, 0, 255).astype(np.uint8)
# ===================================
# 4. Display original, predicted, error
# ===================================
plt.figure(figsize=(12, 9))
plt.subplot(3, 3, 1)
plt.title("Original")
plt.imshow(orig.astype(np.uint8), cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 2)
plt.title("Predicted (A = left)")
plt.imshow(pred_A_vis, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 3)
plt.title("Error (A)")
plt.imshow(error_A_vis, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 4)
plt.title("Predicted (C = up)")
plt.imshow(pred_C_vis, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 5)
plt.title("Error (C)")
plt.imshow(error_C_vis, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 6)
plt.title("Predicted (A+B-C)")
plt.imshow(pred_ABC_vis, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 7)
plt.title("Error (A+B-C)")
plt.imshow(error_ABC_vis, cmap="gray")
plt.axis("off")
plt.tight_layout()
plt.show()