import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from numpy.linalg import *
import numpy as np
from r_pca import R_pca
import skimage
from skimage import color
from numpy import *
import time
from sklearn.metrics import mean_squared_error
from math import sqrt



gray = mpimg.imread("CameraMan.png")
r = np.random.randint(256,size=(750,2))
r1,c1 = r.shape

for i in range(0,r1):
    gray[r[i,0],r[i,1]] = np.nan
Lambda = 0.0625 # close to the default one, but works better
tic = time.time()

# plt.imshow(gray, cmap = plt.cm.Greys_r)
# plt.show()

rpca = R_pca(gray)
toc = time.time()
L, S = rpca.fit(max_iter=500, iter_print=100)
plt.imshow(L, cmap = plt.cm.Greys_r)
#plt.show()
plt.imshow(S, cmap = plt.cm.Greys_r)
#plt.show()
plt.imshow(L+S, cmap = plt.cm.Greys_r)
#plt.show()

#rpca.plot_fit()
#plt.show()

rms = sqrt(mean_squared_error(gray, L))

print rms

