-
Notifications
You must be signed in to change notification settings - Fork 0
/
LowpassFilters.R
89 lines (62 loc) · 1.76 KB
/
LowpassFilters.R
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
# LOG FUNCTION IS USED JUST TO CREATE FOURIER SPECTRUM IMAGE
# I HAVE NOT TAKEN FFT OF FILTER BECAUSE THE METHOD WE ARE USING TO CALCULATE THE DISTANCE-
# MATRIX ALREADY WORKS IN FREQUENCY DOMAIN
library(imager)
img.pic = load.image("/home/prinzz/Desktop/Work/Digital Image Processing/gitHub/test4.jpeg")
dim(img.pic)
size = rep(128,2)
m = size[1]
n = size[2]
P = m*2
Q = n*2
img.resized = as.matrix(resize((img.pic),size[1],size[2]),size[1],size[2])
plot(as.cimg(img.resized))
img.pad = matrix(0,m*2,n*2)
# ----PHASE SHIFTING IN PADDED IMAGE----
for (i in 1:m)
for (j in 1:n)
{
img.pad[i,j] = img.resized[i,j]
img.pad[i,j] = img.pad[i,j]*((-1)^(i+j))
}
# ----PHASE SHIFTING IN PADDED IMAGE----
img.fft=fft(img.pad) #DFT OF PADDED IMAGE
# ----LOG IMAGE----
img.log = log(1+abs(img.fft))
plot(as.cimg(img.log))
# ----LOG IMAGE----
D0=50
#----FILTER CREATION----
kernel.mat = matrix(0,P,Q)
for (i in 1:P) {
for (j in 1:Q) {
distance=((i-m)^2+(j-n)^2)^(0.5)
# if( distance <= D0){ #IDEAL PASS FILTER
# kernel.mat[i,j] = 1
# }else{
# kernel.mat[i,j] = 0
# }
N=2
# kernel.mat[i,j]=(1/(1 + (distance/D0)^2*N)) #BUTTERWORTH FILTER
kernel.mat[i,j]=exp((-(distance)^2)/(2*((D0)^2))) #GAUSSIAN FILTER
}
}
#----FILTER CREATION----
img.filtered = kernel.mat*img.fft
img.ifft = fft(img.filtered,inverse = TRUE)/length(img.filtered)
img.ifft = Re(img.ifft)
for (i in 1:P) {
for (j in 1:Q) {
img.ifft[i,j] = (img.ifft[i,j])*((-1)^(i+j))
}
}
img.nopad=matrix(0,m,n)
for (i in 1:m) {
for (j in 1:n) {
img.nopad[i,j]=img.ifft[i,j]
}
}
img.ifftlog=log(1+abs(img.filtered))
plot(as.cimg(Re(img.ifftlog))) #DO RUN THIS
plot(as.cimg(Re(img.nopad)))
# ----CREATED BY PRIYANSHU AGARWAL----