SVD and PCA
import cv2
from sklearn.preprocessing import StandardScaler
import numpy as np
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
def openImage(imagePath):
A = plt.imread(imagePath)
X = np.mean(A,-1)
#img = plt.imshow(X)
#img.set_cmap('gray')
#plt.axis('off')
#plt.show()
return X
arr = openImage('The-Lena-face-Test-Image.ppm')
print(arr.shape)
def svd(k,arr):
A = np.dot(arr.T,arr)
rank = np.linalg.matrix_rank(A)
val2,vec2 = np.linalg.eig(A)
idx = val2.argsort()[::-1]
val2 = val2[idx]
vec2 = vec2[:,idx]
sigma = np.zeros(arr.shape)
sqrt_eval = np.sqrt(np.round_(val2.real,2))
for i in range(min(arr.shape)):
sigma[i][i] = sqrt_eval[i]
u = np.zeros((arr.shape[0], arr.shape[0]))
i =0
while sqrt_eval[i]>0:
temp = (np.dot(arr,vec2[:,i]))/sqrt_eval[i]
u[:,i] = temp.real
i +=1
re_construct = u[:,:k] @ sigma[0:k,:k] @ vec2.T[:k, :]
print(u.shape,sigma.shape,vec.T.shape)
return re_construct
SVD EXPLAINED!
def pca(k,img):
cov_mat = np.cov(img , rowvar = False)
eigen_values, eigen_vectors = np.linalg.eigh(cov_mat)
idx = np.argsort(eigen_values)[::-1]
eigenvalues = eigen_values[idx]
eigenvectors = eigen_vectors[:,idx]
eigenvector_subset = eigenvectors[:,0:k]
imgreduced = np.dot(eigenvector_subset.T , img.T ).T
imgreconstr = np.dot(imgreduced,eigenvector_subset.T)
img = scaler.inverse_transform(img)
imgreconstr = scaler.inverse_transform(imgreconstr)
return imgreconstr
PCA EXPLAINED!
def plot(k,img,arr):
pca_img = pca(k,img)
svd_img = svd(k,arr)
pca_img = pca_img/255
fig = plt.figure(figsize = (10, 5))
s = 'K : ' + str(k) + ' Frobenius norm :' + str(np.linalg.norm(svd_img - pca_img,'fro'))
fig.suptitle(s)
fig.add_subplot(121)
plt.text(15, -0.01,"PCA Image")
plt.imshow(pca_img, cmap='gray')
plt.axis('off')
fig.add_subplot(122)
plt.text(15, -0.01,"SVD Image")
plt.imshow(svd_img, cmap='gray')
plt.axis("off")
plt.show()
FINAL RESULTS
import cv2
from sklearn.preprocessing import StandardScaler
import numpy as np
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
def openImage(imagePath):
A = plt.imread(imagePath)
X = np.mean(A,-1)
#img = plt.imshow(X)
#img.set_cmap('gray')
#plt.axis('off')
#plt.show()
return XA
arr = openImage('The-Lena-face-Test-Image.ppm')
def svd(k,arr):
A = np.dot(arr.T,arr)
rank = np.linalg.matrix_rank(A)
val2,vec2 = np.linalg.eig(A)
idx = val2.argsort()[::-1]
val2 = val2[idx]
vec2 = vec2[:,idx]
sigma = np.zeros(arr.shape)
sqrt_eval = np.sqrt(np.round_(val2.real,2))
for i in range(min(arr.shape)):
sigma[i][i] = sqrt_eval[i]
u = np.zeros((arr.shape[0], arr.shape[0]))
i =0
while sqrt_eval[i]>0:
temp = (np.dot(arr,vec2[:,i]))/sqrt_eval[i]
u[:,i] = temp.real
i +=1
re_construct = u[:,:k] @ sigma[0:k,:k] @ vec2.T[:k, :]
#print(u.shape,sigma.shape,vec.T.shape)
return re_construct
img = cv2.imread("The-Lena-face-Test-Image.ppm")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
scaler = StandardScaler()
scaler.fit(img)
img = scaler.transform(img)
def pca(k,img):
cov_mat = np.cov(img , rowvar = False)
eigen_values, eigen_vectors = np.linalg.eigh(cov_mat)
idx = np.argsort(eigen_values)[::-1]
eigenvalues = eigen_values[idx]
eigenvectors = eigen_vectors[:,idx]
eigenvector_subset = eigenvectors[:,0:k]
imgreduced = np.dot(eigenvector_subset.T , img.T ).T
imgreconstr = np.dot(imgreduced,eigenvector_subset.T)
img = scaler.inverse_transform(img)
imgreconstr = scaler.inverse_transform(imgreconstr)
return imgreconstr
def plot(k,img,arr):
pca_img = pca(k,img)
svd_img = svd(k,arr)
pca_img = pca_img/255
fig = plt.figure(figsize = (10, 5))
s = 'K : ' + str(k) + ' Frobenius norm :' + str(np.linalg.norm(svd_img - pca_img,'fro'))
fig.suptitle(s)
fig.add_subplot(121)
plt.text(15, -0.01,"PCA Image")
plt.imshow(pca_img, cmap='gray')
plt.axis('off')
fig.add_subplot(122)
plt.text(15, -0.01,"SVD Image")
plt.imshow(svd_img, cmap='gray')
plt.axis("off")
plt.show()
k = [20,40,60,80,100,150,200]
for i in k:
plot(i,img,arr)