def blind_lucy_wrapper(image, max_its=8, its=5, N_filter=None, weiner=False, estimation_noise=0, filter_estimation=1, observation_noise=0): f = io.imread('../data/'+image+'.png', dtype=float) if len(f.shape) == 3: f = f.mean(axis=2) f /= f.max() if N_filter: g = helper.gaussian(sigma=N_filter/3, N=N_filter) g_k = helper.gaussian(sigma=N_filter/3 * filter_estimation, N=N_filter) g_0 = g_k.copy() else: N = f.shape[0] sigma = N/40 g = helper.gaussian(sigma=sigma, N=N) g_k = helper.gaussian(sigma=filter_estimation*sigma, N=N) c = fftconvolve(f, g, mode='same') #c += observation_noise*np.random.randn(*c.shape) #f_k = f + estimation_noise*np.random.randn(*f.shape) f_k = c.copy() for k in range(int(max_its)): f_k1 = f_k.copy() for i in range(its): g_k = richardson_lucy(c, f_k1, g_k, iterations=1, clip=False) f_k = richardson_lucy(c, g_k, f_k, iterations=1, clip=False) print("on {}, f.max() = {:0.3e}, g.max() = {:0.3e}".format(k, np.abs(f_k.max()), np.abs(g_k.max()))) f_k, g_k = np.abs(f_k), np.abs(g_k) helper.show_images({'estimation':f_k, 'original':f, 'observations':c, 'kernel estimate':g_k}) return f_k, g_k
def blind_lucy_wrapper(image, max_its=8, its=5, N_filter=3, weiner=False, estimation_noise=0, filter_estimation=1, observation_noise=0): f = io.imread('../data/'+image+'.png', dtype=float) if len(f.shape) == 3: f = f.mean(axis=2) f /= f.max() print(f.shape) g = helper.gaussian(sigma=N_filter/3, N=N_filter) g_k = helper.gaussian(sigma=N_filter/3 * filter_estimation, N=N_filter) g_0 = g_k.copy() c = fftconvolve(f, g, mode='same') #c += observation_noise*np.random.randn(*c.shape) f_k = f + estimation_noise*np.random.randn(*f.shape) #f_k = c.copy() for k in range(int(max_its)): g_k = richardson_lucy(g_k, f_k, iterations=int(its), clip=True) if weiner: f_k = wiener(f_k, g_k, 1e-5) else: f_k = richardson_lucy(f_k, g_k, iterations=int(its), clip=True) print("on {}, f.max() = {:0.3e}, g.max() = {:0.3e}".format(k, np.abs(f_k.max()), np.abs(g_k.max()))) f_k, g_k = np.abs(f_k), np.abs(g_k) helper.show_images({'estimation':f_k, 'original':f, 'observations':c})
def show_imgs(): helper.show_images({ 'x': x, 'h': h, 'x_k': x_k, 'h_k': np.abs(h_k) }, colorbar=True, drawnow=True, cmap='gray')
def test_pca(): dataloc = "../data/USPS.mat" A = load_data(dataloc) ps = [10, 50, 100, 200] for p in ps: U_p, A1 = pca(A, p) Re_A = reconstruction(U_p, A1) error = reconstruct_error(A, Re_A) print(error) show_images(Re_A, p, 1) show_images(Re_A, p, 2)
def test_pca(): dataloc = "../data/USPS.mat" A = load_data(dataloc) ps = [10, 50, 100, 200] for p in ps: pca = PCA(A, p) Ap = pca.get_reduced() A_re = pca.reconstruction(Ap) error = reconstruct_error(A, A_re) print(error) show_images(A_re, p, 1) show_images(A_re, p, 2)
import numpy as np from numpy.fft import ifft2, fft2, fftshift import helper output = helper.blur_and_noise(sigma=6, noise_sigma=1e-5) (x, X), (y, Y), (h, H) = output H_k = H * 5 # + np.random.randn(*H.shape) * 0.1 iterations = 20 for k in range(iterations): X_k = Y / (H_k + 1e-9) H_k = Y / (X_k + 1e-9) x_k = fftshift(ifft2(X_k)) helper.show_images({'original': x, 'estimate': np.abs(x_k), 'observations': y})
import numpy as np from skimage import color, data, restoration import numpy as np import numpy.random as npr from scipy.signal import convolve2d import helper camera = color.rgb2gray(data.camera()) camera = helper.get_image('cameraman') from scipy.signal import convolve2d psf = np.ones((5, 5)) / 25 camera = convolve2d(camera, psf, 'same') camera += 0.1 * camera.std() * np.random.standard_normal(camera.shape) deconvolved = restoration.richardson_lucy(camera, psf, 5) helper.show_images({'deconv':deconvolved})
import numpy as np from skimage import color, data, restoration import numpy as np import numpy.random as npr from scipy.signal import convolve2d import helper camera = color.rgb2gray(data.camera()) camera = helper.get_image('cameraman') from scipy.signal import convolve2d psf = np.ones((5, 5)) / 25 camera = convolve2d(camera, psf, 'same') camera += 0.1 * camera.std() * np.random.standard_normal(camera.shape) deconvolved = restoration.richardson_lucy(camera, psf, 5) helper.show_images({'deconv': deconvolved})
if clip: im_deconv[im_deconv > 1] = 1 im_deconv[im_deconv < -1] = -1 return fft2(im_deconv), im_deconv f = helper.get_image('cameraman64') N, _ = f.shape f /= f.sum() F = fft2(f) g = helper.gaussian(sigma=1, N=N) g /= g.sum() G = fft2(g) C = F*G c = ifft2(C) max_its = 80 for k in range(int(max_its)): F, f_k = update_f(F, G, C, eps=0, iterations=1) #G, g_k = update_g(F, G, C, eps=0, iterations=2) print("iteration {}, f.max = {:0.2e}, g.max = {:0.2e}".format(k, np.abs(f).max(), np.abs(g).max())) G, g_k = update_g(F, G, C, eps=0, iterations=2) helper.show_images({'kernel':np.abs(g_k), 'estimate':np.abs(f_k), 'observations':np.abs(c)})
# im_deconv *= fftconvolve(relative_blur, psf_mirror, 'same') if clip: im_deconv[im_deconv > 1] = 1 im_deconv[im_deconv < -1] = -1 return fft2(im_deconv), im_deconv f = helper.get_image("cameraman64") N, _ = f.shape f /= f.sum() F = fft2(f) g = helper.gaussian(sigma=1, N=N) g /= g.sum() G = fft2(g) C = F * G c = ifft2(C) max_its = 80 for k in range(int(max_its)): F, f_k = update_f(F, G, C, eps=0, iterations=1) # G, g_k = update_g(F, G, C, eps=0, iterations=2) print("iteration {}, f.max = {:0.2e}, g.max = {:0.2e}".format(k, np.abs(f).max(), np.abs(g).max())) G, g_k = update_g(F, G, C, eps=0, iterations=2) helper.show_images({"kernel": np.abs(g_k), "estimate": np.abs(f_k), "observations": np.abs(c)})
import numpy as np from numpy.fft import ifft2, fft2, fftshift import helper output = helper.blur_and_noise(sigma=6, noise_sigma=1e-5) (x, X), (y, Y), (h, H) = output H_k = H * 5# + np.random.randn(*H.shape) * 0.1 iterations = 20 for k in range(iterations): X_k = Y / (H_k + 1e-9) H_k = Y / (X_k + 1e-9) x_k = fftshift(ifft2(X_k)) helper.show_images({'original':x, 'estimate':np.abs(x_k), 'observations':y})