-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFT.py
103 lines (81 loc) · 2.66 KB
/
DFT.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 5 12:04:31 2022
@author: Anil Pudasaini 076MSICE003
"""
from scipy import fftpack
import numpy as np
import imageio
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
def frequency_image(image):
image_array = np.array(image)
fft1 = fftpack.fftshift(fftpack.fft2(image_array))
magnitude_spectrum = 20*np.log(np.abs(fft1))
freq_image = Image.fromarray(magnitude_spectrum)
freq_image = freq_image.convert("L")
return freq_image
def low_pass_filter(image1):
#convert image to numpy array
image1_np=np.array(image1)
#fft of image
fft1 = fftpack.fftshift(fftpack.fft2(image1_np)
#Without shifting the data would be centered around origin at the top left
#Shifting it moves the origin to the center of the image
#Create a low pass filter image
x,y = image1_np.shape[0],image1_np.shape[1]
#defining filter
#size of circle
e_x,e_y=70,70
#create a box
bbox=((x/2)-(e_x/2),(y/2)-(e_y/2),(x/2)+(e_x/2),(y/2)+(e_y/2))
low_pass=Image.new("L",(image1_np.shape[0],image1_np.shape[1]),color=0)
draw1=ImageDraw.Draw(low_pass)
draw1.ellipse(bbox, fill=1)
low_pass_np=np.array(low_pass)
low_pass_np = low_pass_np.T
#end of defining filter
#multiply both the images
filtered=np.multiply(fft1,low_pass_np)
#inverse fft
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(filtered)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
data = Image.fromarray(ifft2)
data = data.convert("L")
return data
def high_pass_filter(image):
#converting image to array
image_array = np.array(image)
#sending image to low pass filter
lowpass_image = low_pass_filter(image)
#converting image to array
lowpass_image_array = np.array(lowpass_image)
#subtracting lowpass image from original to obtain highpass image
high_pass_array = image_array - lowpass_image_array
#array to image
high_pass_image = Image.fromarray(high_pass_array)
high_pass_image = high_pass_image.convert("L")
return high_pass_image
image = imageio.imread('girl.jpg',as_gray=True)
freq_image = frequency_image(image)
lowpass_image = low_pass_filter(image)
highpass_image = high_pass_filter(image)
fig = plt.figure()
fig.set_figheight(10)
fig.set_figwidth(10)
#plotting original image
fig.add_subplot(2,2,1)
plt.imshow(image, cmap='gray')
plt.title('original')
#plotting the image in frequency domain
fig.add_subplot(2,2,2)
plt.imshow(freq_image, cmap='gray')
plt.title('Frequency Domain')
#plotting lowpass image
fig.add_subplot(2,2,3)
plt.imshow(lowpass_image, cmap='gray')
plt.title('lowpass_image')
#plotting highpass image
fig.add_subplot(2,2,4)
plt.imshow(highpass_image, cmap='gray')
plt.title('highpass_image')