RtSVD Test

WNixalo 2018/3/8

Testing out some R-SVD on a burst of photos. From Unit 3 Lecture 4 of Fastai's Computational Linear Algebra course.

Photos were taken by me at breakfast. The steadier your hand and the more photos you have, the better. May need more work to get color photos working.


1. Setup

In [1]:
## Imports
%matplotlib inline

import cv2
import numpy as np
from sklearn import decomposition
import matplotlib.pyplot as plt
import os
In [2]:
## Loading Images from Disk
images_raw = [cv2.imread("data/burst_photos/"+impath) 
              for impath in os.listdir("data/burst_photos/") 
              if impath != '.DS_Store']

images_raw[0].shape
Out[2]:
(3024, 3024, 3)
In [3]:
## Converting Color and Resizing
# images = [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in images_raw]  # color
images = [cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for img in images_raw]  # gray
images = [cv2.resize(img, (400,400)) for img in images]
In [4]:
## Sample Image in Grayscale
# plt.figure(figsize=(12,12))
plt.imshow(images[0], cmap='gray');

2. Randomized Truncated Singular Value Decomposition

In [5]:
## Stack Images into 1 Matrix
M = np.vstack((img.flatten() for img in images)).T
In [6]:
M.shape
Out[6]:
(160000, 40)
In [7]:
## Displaying a reassembled image from the Matrix
# plt.figure(figsize=(10,10))
# plt.imshow(np.reshape(M[:,0], (400,400,3)));  # color
plt.imshow(np.reshape(M[:,0], (400,400)), cmap='gray');  # gray
In [42]:
U, Σ, Vt = decomposition.randomized_svd(M, 1)
In [43]:
SVD_Matrix = U @ np.diag(Σ) @ Vt
In [40]:
SVD_Matrix.shape
Out[40]:
(160000, 40)
In [113]:
idx = 0
# SVD_image = np.reshape(SVD_Matrix[:,idx], (400,400,3))  # color
SVD_image = np.reshape(SVD_Matrix[:,idx], (400,400))  # gray

# plt.figure(figsize=(12,12))
plt.imshow(SVD_image, cmap='gray');

3. Demo

In [11]:
def show_random_image_pair(Matrix, SVD_Matrix):
    n = Matrix.shape[-1]
    idx = np.random.randint(0,n)
    dims = (400,400)  # (400,400,3) for color
    
    plt.figure(figsize=(15,15))
    plt.subplot(1,2,1)
    plt.imshow(np.reshape(Matrix[:,idx], dims), cmap='gray')
    plt.subplot(1,2,2)
    plt.imshow(np.reshape(SVD_Matrix[:,idx], dims), cmap='gray')
In [105]:
show_random_image_pair(M, SVD_Matrix)
In [106]:
show_random_image_pair(M, SVD_Matrix)