-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca reconstruction.py
34 lines (30 loc) · 1.35 KB
/
pca reconstruction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import scipy
import scipy.ndimage
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
# IMPORTING IMAGE USING SCIPY AND TAKING R,G,B COMPONENTS
a = scipy.ndimage.imread("C:/Users/DELL-pc/Desktop/ayush/c98b4234-48a8-11e7-93ee-bf101afa7e33.jpg")
a_np = np.array(a)
a_r = a_np[:,:,0]
a_g = a_np[:,:,1]
a_b = a_np[:,:,2]
def comp_2d(image_2d): # FUNCTION FOR RECONSTRUCTING 2D MATRIX USING PCA
cov_mat = image_2d - np.mean(image_2d , axis = 1)
eig_val, eig_vec = np.linalg.eigh(np.cov(cov_mat)) # USING "eigh", SO THAT PROPRTIES OF HERMITIAN MATRIX CAN BE USED
p = np.size(eig_vec, axis =1)
idx = np.argsort(eig_val)
idx = idx[::-1]
eig_vec = eig_vec[:,idx]
eig_val = eig_val[idx]
numpc = 30 # THIS IS NUMBER OF PRINCIPAL COMPONENTS, YOU CAN CHANGE IT AND SEE RESULTS
if numpc <p or numpc >0:
eig_vec = eig_vec[:, range(numpc)]
score = np.dot(eig_vec.T, cov_mat)
recon = np.dot(eig_vec, score) + np.mean(image_2d, axis = 1).T # SOME NORMALIZATION CAN BE USED TO MAKE IMAGE QUALITY BETTER
recon_img_mat = np.uint8(np.absolute(recon)) # TO CONTROL COMPLEX EIGENVALUES
return recon_img_mat
a_r_recon, a_g_recon, a_b_recon = comp_2d(a_r), comp_2d(a_g), comp_2d(a_b) # RECONSTRUCTING R,G,B COMPONENTS SEPARATELY
img = np.dstack((a_r_recon, a_g_recon, a_b_recon)) # COMBINING R.G,B COMPONENTS TO PRODUCE COLOR IMAGE
img = Image.fromarray(img)
img.show()