📚 The CoCalc Library - books, templates and other resources
License: OTHER
import numpy as np1import matplotlib.pyplot as plt23from skimage import io4from skimage.viewer import ImageViewer5from skimage.viewer.widgets import Slider6from skimage.viewer.plugins.base import Plugin789image = io.imread('../../images/clock_motion.png')10M, N = image.shape1112## Should pad, but doesn't make much difference in this case13MM, NN = 2 * M + 1, 2 * N + 11415def hann(image):16wy = np.hanning(image.shape[0])[:, None]17wx = np.hanning(image.shape[1])181920## Apply Hann window to prevent ringing21wy = np.hanning(M)[:, None]22wx = np.hanning(N)2324f = np.zeros((MM, NN))25f[:M, :N] = wy * wx * image2627F = np.fft.fft2(f)2829v, u = np.ogrid[:MM, :NN]30v -= (MM - 1) // 231u -= (NN - 1) // 2323334def apply_inverse_filter(image, T, a, b, K=5, clip=500):35uavb = u * a + v * b36H = T * np.sinc(uavb) * np.exp(-1j * np.pi * uavb)37H = np.fft.fftshift(H)3839HH = 1./H40HH[np.abs(HH) > K] = K4142gg = np.abs(np.fft.ifft2(F * HH))43gg = gg[:M, :N]44gg = np.clip(gg, 0, clip)45gg -= gg.min()46gg /= gg.max()4748return gg4950viewer = ImageViewer(image)5152plugin = Plugin(image_filter=apply_inverse_filter)53plugin += Slider('T', 0, 1, value=0.5, value_type='float', update_on='release')54plugin += Slider('a', -0.1, 0.1, value=0, value_type='float', update_on='release')55plugin += Slider('b', -0.1, 0.1, value=0, value_type='float', update_on='release')56plugin += Slider('K', 0, 100, value=15, value_type='float', update_on='release')57plugin += Slider('clip', 0, 1000, value=750, value_type='float', update_on='release')58viewer += plugin59viewer.show()606162