PCA image color compression experiment

Pekka Väänänen | 30fps.net | March 5th, 2024

We can reduce an RGB image into two channels by finding the top two principal directions with Principal Component Analysis (PCA) and representing each pixels RGB values as a combination of those two. It's a bit like a continuous two-color palette.

The principal directions would also be need to be transmitted along the new two channeled image, just like in a paletted image.

In [18]:
from PIL import Image
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

# Load image with PIL:
image = Image.open('bliss.jpg') 
image = image.resize((image.size[0]//8, image.size[1]//8))
image_data = np.array(image)/255.

flattened_image_data = image_data.reshape(-1, 3)

colors = [tuple(flattened_image_data[i]) for i in range(flattened_image_data.shape[0])]

# Perform PCA:
pca = PCA(n_components=2)
model = pca.fit(flattened_image_data)
reduced_data = model.transform(flattened_image_data)
result = reduced_data.reshape((*image_data.shape[:2],2))
reconstruction = pca.inverse_transform(result).reshape((*image_data.shape[:2],3))

fig, ax = plt.subplots(3,2,figsize=(14,16))

ax_in, ax_reco, ax_out_a, ax_out_b, ax_pri1, ax_diff = ax.flatten()
ax_in.imshow(image_data)
ax_reco.imshow(reconstruction.clip(0,1))
ax_out_a.imshow(result[...,0])
ax_out_b.imshow(result[...,1])

ax_in.set_title("Input image")
ax_reco.set_title("Reconstruction")
ax_out_a.set_title("Encoded channel 1")
ax_out_b.set_title("Encoded channel 2")

ax_pri1.scatter(reduced_data[...,0], reduced_data[...,1], c=colors)
ax_pri1.set_xlabel("Principal direction 1")
ax_pri1.set_ylabel("Principal direction 2 ")
ax_pri1.set_title("Image data distribution in PCA space")

ax_diff.imshow(np.mean(np.abs(image_data - reconstruction), axis=2))
ax_diff.set_title("L1 error between input and reconstruction")

for a in ax.flatten()[:4]:
    a.axis('off')

ax_diff.axis('off')

plt.suptitle("Encoding image colors in two channels with PCA")

plt.show()

print('Principal components:\n', model.components_)