In [1]:
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)
In [2]:
target = img_open("/home/leo/Downloads/cat.jpg") W = target.shape[1] H = target.shape[0]
In [3]:
show(target)
Out:
In [4]:
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)
In [5]:
show(target * k.reshape(target.shape))
Out:
In [11]:
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)
Out [11]:
(2180, 1)
Out [11]:
(2180, 8720)
Out [11]:
Lasso(alpha=0.001)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.Lasso(alpha=0.001)
In [7]:
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)
Out: