import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from numpy import fft
from IPython.html import widgets
%matplotlib inline
np.fft.fft2()
to get the spectrum, which is a complex matrix (that is what we need for manipulation)np.abs
to get the magnitude spectrum, and take the np.log()
to make them visiblenp.fft.fftshift()
to make the DC component (low freq) move from top-left to centernp.fft.ifftshift()
(no fft.fftishift) to shift back to top-left centered DCnp.ifft2()
to get the image domain matrixrose = cv2.imread("public-images/blue_rose.jpg", cv2.IMREAD_GRAYSCALE)
rose_fft = fft.fftshift(fft.fft2(rose))
plt.imshow(np.log(np.abs(rose_fft)), cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f1ce5402a10>
hp_rose_fft = rose_fft.copy()
cr, cc = rose.shape[0] / 2, rose.shape[1] / 2
hp_rose_fft[cr-30:cr+30, cc-30:cc+30] = 0
hp_rose = fft.ifft2(fft.ifftshift(hp_rose_fft))
hp_rose = np.abs(hp_rose)
plt.imshow(hp_rose, cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f1ce528e050>
this is determined by the effect of the kernel used in that filer. convolution in time domain is equivalent to mulitplication in frequence domain.
mean_filter = np.ones((3, 3))
gaussian_filter = cv2.getGaussianKernel(5, 10) * cv2.getGaussianKernel(5, 10).T
laplacian = np.array([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])
fig, axes = plt.subplots(1, 3, figsize = (3 * 4, 4))
filters = zip(["mean_filter", "gaussian_filter", "laplacian"],
[mean_filter, gaussian_filter, laplacian])
for i, (filter_name, filter_kernel) in enumerate(filters):
specterum = fft.fftshift(fft.fft2(filter_kernel))
specterum = np.log(np.abs(specterum) + 1)
axes[i].imshow(specterum, cmap = plt.cm.gray)
axes[i].set_title(filter_name)