-
Notifications
You must be signed in to change notification settings - Fork 8
/
image_pca.py
executable file
·58 lines (45 loc) · 1.67 KB
/
image_pca.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
'''
Created on 27. mar. 2015
@author: niko
'''
from sklearn import decomposition
from pylab import *
from skimage import data, io, color
import copy
from PIL import Image
from numpy import *
import random
def distortPrincipalComponents(components, randomValues, sigma=0.1):
componentsCount, dimensionsCount = components.shape
for i in range(len(randomValues)):
for j in range(componentsCount):
components[j][i] *= randomValues[i]
return components
def distortImage(img, componentsToDistortCount, sigma=0.1):
result = copy(img)
w,h,c = img.shape
#fig, axes = subplots(nrows = c, ncols = 1)
#gray()
n_comp = 200
randomValues = [random.gauss(0,sigma) for i in range(componentsToDistortCount)]
channels = [result[:,:,i] for i in range(c)]
for i in range(c):
pca = decomposition.PCA(n_components = n_comp)
pca.fit(channels[i])
channel_pca = pca.fit_transform(channels[i])
distorted = distortPrincipalComponents(channel_pca, randomValues)
result[:,:,i] = pca.inverse_transform(distorted)
#axes[i].imshow(result[:,:,i])
#axes[i].set_title('ch')
#plt.show()
return result
if __name__ == "__main__":
link = "/home/niko/datasets/DiabeticRetinopathyDetection/processed/run-normal/no_pca/train_test/31_right.jpeg"
retina = io.imread(link)
retinaDistorted = distortImage(retina, 3)
fig, axes = subplots(nrows = 2, ncols = 1)
axes[0].imshow(retina)
axes[0].set_title('original image')
axes[1].imshow(retinaDistorted)
axes[1].set_title('pca restored')
plt.show()