-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPCA_Compression.py
67 lines (37 loc) · 1.05 KB
/
PCA_Compression.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# coding: utf-8
# In[28]:
#importing the necessary libraries.
import numpy as np
from matplotlib.image import imread
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA, IncrementalPCA
from skimage import color
from skimage import io
image_raw = imread("flower.jpg")
print(image_raw.shape)
plt.figure(figsize=[12,8])
plt.imshow(image_raw)
# In[29]:
img = color.rgb2gray(io.imread('flower.jpg'))
plt.figure(figsize=[12,8])
plt.imshow(image_bw)
# In[31]:
pca = PCA()
pca.fit(img)
variance = np.cumsum(pca.explained_variance_ratio_)*100
# Calculating the number of components needed to preserve 98% of the data
k = np.argmax(variance>98)
print("k for 98% variance: "+ str(k))
#print("\n")
plt.figure(figsize=[10,5])
plt.axvline(x=k, color="k")
plt.axhline(y=95, color="r")
ax = plt.plot(var_cumu)
# In[35]:
ipca = IncrementalPCA(n_components=k)
image_compressed = ipca.inverse_transform(ipca.fit_transform(img))
# Plotting the compressed image
plt.figure(figsize=[12,8])
plt.imshow(image_compressed)
# In[ ]: