Compressed Sensing Image Lasso


Reading time: about 7 minutes
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:
<Figure size 432x288 with 1 Axes>
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:
<Figure size 432x288 with 1 Axes>
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.
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:
<Figure size 432x288 with 1 Axes>

The following pages link here

Citation

If you find this work useful, please cite it as:
@article{yaltirakliwikicompressedsensingimagelasso,
  title   = "Compressed Sensing Image Lasso",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/wiki/compressed-sensing-image-lasso/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Compressed Sensing Image Lasso", December, 2024. [Online]. Available: https://www.gkbrk.com/wiki/compressed-sensing-image-lasso/. [Accessed Dec. 17, 2024].
APA Style
Yaltirakli, G. (2024, December 17). Compressed Sensing Image Lasso. https://www.gkbrk.com/wiki/compressed-sensing-image-lasso/
Bluebook Style
Gokberk Yaltirakli, Compressed Sensing Image Lasso, GKBRK.COM (Dec. 17, 2024), https://www.gkbrk.com/wiki/compressed-sensing-image-lasso/

Comments

© 2024 Gokberk Yaltirakli