-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIP_015.py
50 lines (38 loc) · 1.22 KB
/
DIP_015.py
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
from matplotlib import pyplot as plt
import numpy as np
def highPassFiltering(img, size):
h, w = img.shape[0:2]
h1,w1 = int(h/2), int(w/2)
img[h1-int(size/2):h1+int(size/2), w1-int(size/2):w1+int(size/2)] = 0
return img
def lowPassFiltering(img, size):
h, w = img.shape[0:2]
h1,w1 = int(h/2), int(w/2)
img2 = np.zeros((h, w), np.uint8)
img2[h1-int(size/2):h1+int(size/2), w1-int(size/2):w1+int(size/2)] = 1
img3 = img2 * img
return img3
gray = cv2.imread('DataSet/House.jpeg', cv2.IMREAD_GRAYSCALE)
# Fourier transform
img_dft = np.fft.fft2(gray)
dft_shift = np.fft.fftshift(img_dft)
#High pass filter
dft_shift=lowPassFiltering(dft_shift, 350)
res = np.log(np.abs(dft_shift))
# Inverse Fourier Transform
idft_shift = np.fft.ifftshift(dft_shift)
ifimg = np.fft.ifft2(idft_shift)
ifimg = np.abs(ifimg)
# Draw pictures
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(1, 3, 1)
ax1.imshow(gray, 'gray')
ax1.title.set_text('Original image')
ax1 = fig.add_subplot(1, 3, 2)
ax1.imshow(res)
ax1.title.set_text('DFT filter')
ax1 = fig.add_subplot(1, 3, 3)
ax1.imshow(np.int8(ifimg), cmap=plt.cm.gray)
ax1.title.set_text('Result')
plt.show()