-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfourriers_transf.py
More file actions
50 lines (38 loc) · 1.48 KB
/
fourriers_transf.py
File metadata and controls
50 lines (38 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Step 1: Read image
img = cv2.imread('image.jpg', 0)
# Step 2: Apply DFT
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
# Step 3: Create masks
rows, cols = img.shape
crow, ccol = rows//2, cols//2
mask_lpf = np.zeros((rows, cols, 2), np.uint8)
mask_hpf = np.ones((rows, cols, 2), np.uint8)
# Low-pass radius
r = 40
mask_lpf[crow-r:crow+r, ccol-r:ccol+r] = 1 # center circle pass
# High-pass opposite of low-pass
mask_hpf[crow-r:crow+r, ccol-r:ccol+r] = 0
# Step 4: Apply masks
lpf_dft = dft_shift * mask_lpf
hpf_dft = dft_shift * mask_hpf
# Step 5: Inverse DFT
def inverse_dft(filtered):
f_ishift = np.fft.ifftshift(filtered)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
return img_back
lpf_img = inverse_dft(lpf_dft)
hpf_img = inverse_dft(hpf_dft)
# Step 6: Display
plt.figure(figsize=(15, 10))
plt.subplot(2,3,1), plt.imshow(img, cmap='gray'), plt.title("Original"), plt.axis('off')
plt.subplot(2,3,2), plt.imshow(lpf_img, cmap='gray'), plt.title("Low Pass Filtered"), plt.axis('off')
plt.subplot(2,3,3), plt.imshow(hpf_img, cmap='gray'), plt.title("High Pass Filtered"), plt.axis('off')
plt.subplot(2,3,4), plt.imshow(mask_lpf[:,:,0], cmap='gray'), plt.title("Low Pass Mask"), plt.axis('off')
plt.subplot(2,3,5), plt.imshow(mask_hpf[:,:,0], cmap='gray'), plt.title("High Pass Mask"), plt.axis('off')
plt.tight_layout()
plt.show()