-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (40 loc) · 1.15 KB
/
main.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
import numpy as np
import numpy.linalg as linalg
import sys
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
r = 500 #512 (256 )
Maxiter = 500
Ro = float('inf')
t = 0
image = Image.open('Lenna.png')
gray_img = image.convert("L")
M = np.asarray(gray_img)
U,S,V = linalg.svd(M)
Vbarre = V[:,:r]
Ro = float('inf')
t = 0
while 1:
t += 1
VbarrePlus = np.where(Vbarre < 0, 0, Vbarre)
Y = np.dot( Vbarre.T, VbarrePlus)
Ro0 = Ro
Ro = linalg.norm(Y-np.identity(Y.shape[0]),'fro')**2
if(Ro0-Ro < sys.float_info.epsilon) or (t == Maxiter):
break
U,S,V= linalg.svd(Y)
Vbarre = np.dot(Vbarre,np.dot(U,V.T))
#S = np.where(Vbarre < 0, 0, Vbarre) #Vbarre Plus
S = np.dot(Vbarre,Y)
aux = np.dot(M,Vbarre)
W = np.dot(aux,linalg.inv(Y.T)) # transposé de la matrice diagonale inferieure de Y
# Il s'agit de l'inverse de la transposée de la matrice Y
out = np.dot(W,S.T)
data = Image.fromarray(out)
plt.imshow(data, cmap='gray')
plt.show()
new_p = data.convert("L")
new_p.save("output"+str(r)+"v2.png")
loss = mean_absolute_error(out, M) #mean square error
print(loss)