Lossy pylops wavelet compression seems independent of specified mother wavelet

28 Views Asked by At

I am trying to encode a given image and then threshold out the smallest magnitude coefficients, for different wavelet functions and using the python package pylops.

However, when I do this encoding for different mother wavelets, I notice that the result is exactly the same, independent of the specified mother wavelet. Here is my code:

from PIL import Image
import numpy as np
import pylops
import matplotlib.pyplot as plt

img = Image.open('tucan.jpg').convert('L')
img = np.array(img)

# Perform a wavelet transform using Haar wavelet with compression
Nx, Ny = img.shape
Wop = pylops.signalprocessing.DWT2D((Nx, Ny), wavelet="haar", level=0)
img_ravel = img.ravel()

# encode
enc = Wop * img_ravel

# threshhold out the smallest coefficients
abs_coeffs = np.abs(enc)

# Sort the coefficients in descending order
sorted_coeffs = np.sort(abs_coeffs)[::-1]

# Find the threshold value corresponding to the lowest q% of coefficients
thresh_idx = int(len(sorted_coeffs) * 0.3)
thresh = sorted_coeffs[thresh_idx]

# Set the coefficients below the threshold to zero
quant_coeffs = np.where(abs_coeffs > thresh, enc, 0)

dec_haar = (Wop.H * quant_coeffs).reshape((Nx, Ny))

########### same code again but this time with coif5 mother wavelet

Nx, Ny = img.shape
Wop_coif = pylops.signalprocessing.DWT2D((Nx, Ny), wavelet='coif5', level=0)
img_ravel = img.ravel()

# encode
enc_coif = Wop_coif * img_ravel
# threshhold out the smallest coefficients
abs_coeffs = np.abs(enc_coif)

# Sort the coefficients in descending order
sorted_coeffs = np.sort(abs_coeffs)[::-1]

# Find the threshold value corresponding to the lowest q% of coefficients
thresh_idx = int(len(sorted_coeffs) * 0.3)
thresh = sorted_coeffs[thresh_idx]

# Set the coefficients below the threshold to zero
quant_coeffs = np.where(abs_coeffs > thresh, enc_coif, 0)

# decode
dec_coif = (Wop_coif.H * quant_coeffs).reshape((Nx, Ny))

print(np.sum((dec_haar - dec_coif)**2))

output:

0

This is not what I would expect. Shouldn't the reconstruction of the image be different for different mother wavelets for a lossy compression?

0

There are 0 best solutions below