get_ipython().ast_node_interactivity = 'all'
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import math
from PIL import Image
import scipy.fftpack
import scipy.optimize
import random
from sklearn.linear_model import Lasso
matplotlib.rcParams['figure.dpi'] = 150
def show(img):
_ = plt.imshow(img, cmap='gray')
#_ = plt.colorbar()
def img_open(path):
img = Image.open(path)
img = img.convert('L')
while img.width * img.height > 100 * 100:
img = img.resize((int(img.width / 1.1), int(img.height / 1.1)))
return np.array(img, dtype=np.uint8)target = img_open("/home/leo/Downloads/cat.jpg")
W = target.shape[1]
H = target.shape[0]show(target)received = int((W * H) * 0.25)
not_received = (W * H) - received
k = [True] * received + [False] * not_received
random.shuffle(k)
random.shuffle(k)
random.shuffle(k)
k = np.array(k)show(target * k.reshape(target.shape))b = target.T.flat[k]
b = np.expand_dims(b, axis=1)
b.shape
A = np.kron(
scipy.fftpack.idct(np.identity(W), norm='ortho', axis=0),
scipy.fftpack.idct(np.identity(H), norm='ortho', axis=0)
)
A = A[k,:] # same as phi times kron
A.shape
lasso = Lasso(alpha=0.001)
lasso.fit(A, b)(2180, 1)
(2180, 8720)
Lasso(alpha=0.001)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
Lasso(alpha=0.001)
def idct2(x):
return scipy.fftpack.idct(scipy.fftpack.idct(x.T, norm='ortho', axis=0).T, norm='ortho', axis=0)
Xat = np.array(lasso.coef_).reshape(W, H).T # stack columns
# Get the reconstructed image
Xa = idct2(Xat)
# normalize
_min = np.min(Xa)
_max = np.max(Xa)
Xa = np.interp(Xa, (_min, _max), (0, 1))
show(Xa)