-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALLinFrequencyDomain.py
213 lines (194 loc) · 6.29 KB
/
ALLinFrequencyDomain.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 11 12:01:11 2022
@author: 076MSICE003 ANIL Pudasaini
Filters in Frequency Domain
"""
from cmath import pi
from math import floor, sqrt, exp
from scipy import fftpack
import numpy as np
import imageio
from PIL import Image
def readImage(path):
im=Image.open(path)
# im.show()
return im
def shiftXY(npArr):
rows,cols=npArr.shape
for i in range(rows):
for j in range(cols):
npArr[i][j]=npArr[i][j]*((-1)**(i+j)) #f(x,y) * (-1)^(x+y)
return npArr
def frequency_image(image):
npImage = np.array(image)
fft = fftpack.fftshift(fftpack.fft2(npImage))
magnitude_spectrum = 20*np.log(np.abs(fft))
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def laplacian(image):
npImage = np.array(image)
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
fft[i][j]=-((2*pi)**2) * (i*i+j*j) * fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def idealLPF(image):
npImage = np.array(image)
cutoff=float(input("Enter cutoff radius"))
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
# u=i-rows/2
# v=j-cols/2
duv=sqrt(i*i+j*j) #D(u,v)=sqrt(u^2+v^2)
if(duv<=cutoff):
lpf=1
else:
lpf=0
fft[i][j]=lpf*fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def butterworthLPF(image):
npImage = np.array(image)
cutoff=float(input("Enter cutoff radius:"))
n=int(input("Enter order of filter:"))
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
# u=i-rows/2
# v=j-cols/2
duv=sqrt(i*i+j*j) #D(u,v)=sqrt(u^2+v^2)
lpf=1/(1+(duv/cutoff)**(2*n))
fft[i][j]=lpf*fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def gaussianLPF(image):
npImage = np.array(image)
cutoff=float(input("Enter cutoff radius:"))
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
# u=i-rows/2
# v=j-cols/2
duv=sqrt(i*i+j*j) #D(u,v)=sqrt(u^2+v^2)
lpf=exp(-(1/2)*((duv/cutoff)**2))
fft[i][j]=lpf*fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def idealHPF(image):
npImage = np.array(image)
cutoff=float(input("Enter cutoff radius"))
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
# u=i-rows/2
# v=j-cols/2
duv=sqrt(i*i+j*j) #D(u,v)=sqrt(u^2+v^2)
if(duv<=cutoff):
hpf=0
else:
hpf=1
fft[i][j]=hpf*fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def butterworthHPF(image):
npImage = np.array(image)
cutoff=float(input("Enter cutoff radius:"))
n=int(input("Enter order of filter:"))
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
duv=sqrt(i*i+j*j) #D(u,v)=sqrt(u^2+v^2)
if(duv==0):
duv=0.0001
hpf=1/(1+(cutoff/duv)**(2*n))
fft[i][j]=hpf*fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
def gaussianHPF(image):
npImage = np.array(image)
cutoff=float(input("Enter cutoff radius:"))
npImage=shiftXY(npImage)
fft = fftpack.fftshift(fftpack.fft2(npImage))
rows, cols= fft.shape
for i in range(rows):
for j in range(cols):
# u=i-rows/2
# v=j-cols/2
duv=sqrt(i*i+j*j) #D(u,v)=sqrt(u^2+v^2)
hpf=1-exp(-(1/2)*((duv/cutoff)**2))
fft[i][j]=hpf*fft[i][j]
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(fft)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
magnitude_spectrum = 20*np.log(np.abs(ifft2))
magnitude_spectrum=shiftXY(magnitude_spectrum)
newImage = Image.fromarray(magnitude_spectrum.astype(np.uint8))
newImage.show()
inp=int(input("Enter your choice:\n \
1. Fourier Image \n \
2. Laplacian \n \
\n Low Pass Filter \n \
3. Ideal LPF \n \
4. Butterworth LPF \n \
5. Gaussian LPF \n \
\n 3High Pass Filter \n \
6. Ideal HPF \n \
7. Butterworth HPF \n \
8. Gaussian HPF \n \
"))
#READING IMAGE
im=imageio.imread("girl.jpg",as_gray = True)
if(inp==1):
frequency_image(im)
elif(inp==2):
laplacian(im)
elif(inp==3):
idealLPF(im)
elif(inp==4):
butterworthLPF(im)
elif(inp==5):
gaussianLPF(im)
elif(inp==6):
idealHPF(im)
elif(inp==7):
butterworthHPF(im)
elif(inp==8):
gaussianHPF(im)