-
Notifications
You must be signed in to change notification settings - Fork 0
/
Butterworth_High_Pass_Filter.py
79 lines (56 loc) · 1.68 KB
/
Butterworth_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# import libraries
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
# Reading input image
input_image = plt.imread('cameraman.jpg')
# Checking if the image is already grayscale
if input_image.ndim == 3:
input_image = rgb2gray(input_image)
# 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.astype(float))
# Assigning the order value
n = 2
# Assigning Cut-off Frequency
D0 = 10
# Designing filter: Butterworth High Pass Filter
u = np.arange(M)
v = np.arange(N)
idx = u > M/2
u[idx] = u[idx] - M
idy = v > N/2
v[idy] = v[idy] - N
U, V = np.meshgrid(u, v, indexing='ij')
# Calculating Euclidean Distance
D = np.sqrt(U**2 + V**2)
# Determining the filtering mask
H = np.zeros_like(D)
mask = D != 0
H[mask] = 1 / (1 + (D0 / D[mask])**(2*n))
# Check the dimensions of H and FT_img using the size function
x, y = H.shape
x1, y1 = FT_img.shape
# We used pad function beceause the dimensions of the two arrays being multiplied do not match
pad_size = (x1-x, y1-y)
H_padded = np.pad(H, pad_size, 'constant')
# Convolution between the Fourier Transformed image and the mask
resized_img = FT_img * H_padded
# Getting the resultant image by Inverse Fourier Transform of the convoluted image
G = np.fft.ifft2(resized_img)
resultant_image = np.abs(G)
# Displaying the resultant 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(resultant_image, cmap='gray')
plt.title('BHPF')
plt.axis('off')
plt.show()