Last active
September 16, 2021 13:39
-
-
Save Eliran-Turgeman/5278077e675202ced04efc62ed6a110a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from PIL import Image | |
MAX_RANK = 100 | |
FNAME = 'bcollie.jpg' | |
image = Image.open(FNAME).convert("L") | |
img_mat = np.asarray(image) | |
U, s, V = np.linalg.svd(img_mat, full_matrices=True) | |
s = np.diag(s) | |
for k in range(MAX_RANK + 1): | |
approx = U[:, :k] @ s[0:k, :k] @ V[:k, :] | |
img = plt.imshow(approx, cmap='gray') | |
plt.title(f'SVD approximation with degree of {k}') | |
plt.plot() | |
pause_length = 0.0001 if k < MAX_RANK else 5 | |
plt.pause(pause_length) | |
plt.clf() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment