Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Created on Tue Oct 27 12:31:49 2020.
- @author: manoj
- """
- import numpy as np
- import matplotlib.pyplot as plt
- import scipy.linalg as scipy_linalg
- import skimage.io
- def compress_svd(image, bw, color):
- U, s, Vt = scipy_linalg.svd(image, full_matrices=False)
- inspect_svalues(s, color)
- recon_image = U[:, 0:bw] @ scipy_linalg.diagsvd(s[0:bw], bw, bw) @ Vt[0:bw, :]
- return recon_image
- def inspect_svalues(svals, color):
- plt.plot(svals)
- plt.xticks(list(range(0, 501, 40)))
- plt.xlabel("Index of Singular Value")
- plt.ylabel("Magnitude of Singular Value")
- plt.title("Singular Values of {0} Channel".format(color))
- plt.show()
- def see_imgs(img1, img2, rank, color='gray'):
- fig, axs = plt.subplots(1, 2)
- axs[0].imshow(img1, cmap=color)
- axs[0].set_title("Original image")
- axs[1].imshow(img2, cmap=color)
- axs[1].set_title("Compressed image to rank = {0}".format(rank))
- plt.suptitle("Image compression using Singular Value Decomposition", y=0.82)
- plt.show()
- eff_rank = 40
- img = skimage.io.imread(fname="./img_for_q3.bmp")
- olayers = [img[:, :, i].astype(np.double) for i in range(3)]
- clayers = [compress_svd(olayers[i], eff_rank, c).astype(np.uint8) for i, c in enumerate(["Red", "Green", "Blue"])]
- re_img = np.dstack(clayers)
- see_imgs(img, re_img, eff_rank)
- [see_imgs(olayers[i], clayers[i], eff_rank, color=cl) for i, cl in enumerate(['Reds_r', 'Greens_r', 'Blues_r'])]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement