def wf(y, mask, iter, verbose=False): m = y.size n = mask.sum() mu_max = 0.2 t0 = 330. y = y**2 lambd2 = y.sum() x = np.random.rand(*y.shape) x[~mask] = np.zeros(m - n) x = x / np.linalg.norm(x, 2) for i in range(1000): x = tools.ifft2d(y * tools.fft2d(x)) x[~mask] = np.zeros(m - n) x = x / np.linalg.norm(x, 2) x = x * np.sqrt(lambd2) for t in range(iter): mu = np.min([1 - np.exp(-t / t0), mu_max]) f = tools.fft2d(x) x = x - mu * tools.ifft2d((f**2 - y) * f) / lambd2 x[~mask] = np.zeros(m - n) x = np.real(x) res = np.real(x) return res
def oss(y, mask, beta=0.9, verbose=False): phase = np.random.rand(*y.shape) * 2 * np.pi x_prev = np.real(tools.ifft2d(y * np.exp(1j * phase))) sigma1 = np.linspace(y.shape[0], 1. / y.shape[0], 10) sigma2 = np.linspace(y.shape[1], 1. / y.shape[1], 10) filtercount = 10 iter = 2000 Rsize = y.shape[0] X = np.arange(1, iter + 1) FX = (filtercount + 1 - np.ceil(X * filtercount / iter)) * np.ceil( iter / (1 * filtercount)) FX = ((FX - np.ceil(iter / filtercount)) * (2 * Rsize) / np.max(FX)) + (2 * Rsize / 10) for i in range(iter): if i == 0 or FX[i - 1] != FX[i]: sigma = (FX[i], FX[i]) filter = tools.get_gauss2d(y.shape, sigma, normalize=False) filter = ifftshift(filter) Fx_curr = y * np.exp(1j * phase) x_curr = np.real(tools.ifft2d(Fx_curr)) idx = (x_curr < 0) + (mask == False) x_curr[idx] = x_prev[idx] - beta * x_curr[idx] Fx_curr = tools.fft2d(x_curr) idx = (mask == False) x_curr[idx] = np.real(tools.ifft2d(Fx_curr * filter))[idx] Fx_curr = tools.fft2d(x_curr) phase = np.angle(Fx_curr) x_prev = x_curr.copy() return x_curr
def prox(self, x, tau=1): # z = np.zeros(y.shape) # z[self.mask] = x.flatten() z = x zf = tools.fft2d(z) mag = 1 / (self.alpha + tau) * (self.alpha * self.y + tau * np.abs(zf)) res = mag * np.exp(1j * np.angle(zf)) res = np.real(tools.ifft2d(res)) # return res[self.mask].reshape(x.shape) return res
def hio(y, mask, iter, beta=0.9, verbose=False): phase = np.random.rand(*y.shape) * 2 * np.pi x_prev = np.real(tools.ifft2d(y * np.exp(1j * phase))) for i in range(iter): if verbose: print('Iteration #{:d} '.format(i + 1), end='') Fx_curr = y * np.exp(1j * phase) x_curr = np.real(tools.ifft2d(Fx_curr)) idx = ((1 + beta) * x_curr < x_prev) + (mask == False) # idx = (x_curr < 0) + (mask == False) x_curr[idx] = x_prev[idx] - beta * x_curr[idx] Fx_curr = tools.fft2d(x_curr) phase = np.angle(Fx_curr) x_prev = x_curr.copy() # residual = np.power(y - np.abs(new_f), 2).mean() return x_curr
def prox(self, x, tau=1): xf = tools.fft2d(x) mag = 1 / (self.alpha + tau) * (self.alpha * self.y + tau * np.abs(xf)) res = mag * np.exp(1j*np.angle(xf)) res = np.real(tools.ifft2d(res)) return res
def grad(self, x): f = tools.fft2d(x) return self.alpha * np.real(tools.ifft2d(f - y * f / np.abs(f)))
def __call__(self, x): f = tools.fft2d(x) return np.real(self.alpha * np.sum((y - np.abs(f))**2))
img = np.array(img) / 255. n, m = img.shape pad_len_1 = int(img.shape[0] * (np.sqrt(args.samprate) - 1)) // 2 pad_len_2 = int(img.shape[1] * (np.sqrt(args.samprate) - 1)) // 2 n, m = img.shape img = np.pad(img, ((pad_len_1, pad_len_1), (pad_len_2, pad_len_2)), 'constant', constant_values=((0, 0), (0, 0))) mask = np.ones(img.shape, dtype=bool) * False mask[pad_len_1:-pad_len_1, pad_len_2:-pad_len_2] = True # sd = 0.1 * np.sqrt(n*m) # noise = np.random.normal(size=img.shape) * sd + 1j * np.random.normal(size=img.shape) * sd # noise = 0 if args.noise == 'gaussian': y = np.real(np.abs(tools.fft2d(img))) + np.random.normal(size=img.shape) * args.noiselvl / 255. elif args.noise == 'poisson': yy = np.real(np.abs(tools.fft2d(img))) alpha = args.noiselvl / 255. intensity_noise = alpha * yy * np.random.normal(size=img.shape) y = (yy**2 + intensity_noise) y = y * (y > 0) y = np.sqrt(y) elif args.noise == 'rician': yy = tools.fft2d(img) y = yy + (np.random.normal(size=img.shape) + 1j * np.random.normal(size=img.shape)) * args.noiselvl / 255. / np.sqrt(2) y = np.abs(y) x = algo.hio(y, mask, args.iter, beta=args.beta)
img = Image.open(args.image).convert('L') img = np.array(img) / 255 pad_len_1 = int(img.shape[0] * (np.sqrt(args.samprate) - 1)) // 2 pad_len_2 = int(img.shape[1] * (np.sqrt(args.samprate) - 1)) // 2 n, m = img.shape img = np.pad(img, ((pad_len_1, pad_len_1), (pad_len_2, pad_len_2)), 'constant', constant_values=((0, 0), (0, 0))) mask = np.ones(img.shape, dtype=bool) * False mask[pad_len_1:-pad_len_1, pad_len_2:-pad_len_2] = True if args.noise == 'gaussian': y = np.real(np.abs(tools.fft2d(img))) + np.random.normal( size=img.shape) * args.noiselvl / 255. sigma = args.noiselvl if args.noiselvl > 0 else 1 elif args.noise == 'poisson': yy = np.real(np.abs(tools.fft2d(img))) alpha = args.noiselvl / 255. intensity_noise = alpha * yy * np.random.normal(size=img.shape) y = (yy**2 + intensity_noise) y = y * (y > 0) y = np.sqrt(y) sigma = np.std(y - yy) * 255. elif args.noise == 'rician': yy = tools.fft2d(img) y = yy + (np.random.normal(size=img.shape) + 1j * np.random.normal(