-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ideal_High_Pass_Filter.py
62 lines (44 loc) · 1.35 KB
/
Ideal_High_Pass_Filter.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
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
# Reading input image
input_image = cv2.imread('cameraman.jpg')
# Checking if the image is already grayscale
if input_image.shape[2] == 3:
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
# Saving the size of the input image in pixels
M, N = input_image.shape
# Getting Fourier Transform of the input image
FT_img = np.fft.fft2(input_image)
# Assign Cut-off Frequency
D0 = 10
# Designing filter: Ideal High Pass Filter
u = np.arange(M)
u[u > M/2] = u[u > M/2] - M
v = np.arange(N)
v[v > N/2] = v[v > N/2] - N
# Calculating Euclidean Distance
D = np.sqrt(u[:, np.newaxis]**2 + v**2)
# Determining the filtering mask
H = np.double(D > D0)
# Check the dimensions of H and FT_img using the shape attribute
x, y = H.shape
x1, y1 = FT_img.shape
# Convolution between the Fourier Transformed image and the mask
G = H * FT_img
# Getting the resultant image by Inverse Fourier Transform of the convoluted image
output_image = np.real(np.fft.ifft2(G))
# Displaying Input Image and Output Image
plt.figure(figsize=(15, 8))
plt.subplot(1, 2, 1)
plt.imshow(input_image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(output_image, cmap='gray')
plt.title('Ideal High Pass Filter')
plt.axis('off')
plt.show()