-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampling.py
More file actions
151 lines (97 loc) · 3.2 KB
/
sampling.py
File metadata and controls
151 lines (97 loc) · 3.2 KB
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
import numpy as np
import matplotlib.pyplot as plt
import cv2
from scipy import signal
def series(x, y):
plt.subplot(211)
plt.plot(x, y)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(212)
plt.magnitude_spectrum(y, Fs = 10)
plt.xlabel("Frequency [Hz]")
plt.ylabel("Amplitude")
plt.show()
def fourierSeries(x,y):
f = np.fft.fft(y)
plt.subplot(211)
plt.plot(x, y)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(212)
plt.plot(x,f)
plt.xlabel("Frequency [Hz]")
plt.ylabel("Amplitude")
plt.show()
def dft(img):
f = np.fft.fft2(img)
ft = np.fft.fftshift(f)
ms = 20 * np.log(np.abs(ft))
return ft, ms
def sample(x , y, n):
f = signal.resample(y, n)
xnew = np.linspace(0, 400, n, endpoint= False)
return f , xnew
def high_pass_filter(img,n):
fshift, ms = dft(img)
rows, cols = img.shape
crow,ccol = rows/2 , cols/2
fshift[int(crow)-n:int(crow)+n, int(ccol)-n:int(ccol)+n] = 0
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
return img_back
def low_pass_filter(img,n):
fshift, ms = dft(img)
rows, cols = img.shape
crow,ccol = rows/2 , cols/2
mask = np.zeros([rows, cols])
mask[int(crow)-n:int(crow)+n, int(ccol)-n:int(ccol)+n] = 1
fshift = fshift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
return img_back
def displaySampleFunction(x , y, n):
f, xnew = sample(x,y, n)
plt.plot(x,y)
#plt.plot(xnew, f)
plt.stem(xnew, f, linefmt='r-', markerfmt='rs', basefmt='r-')
plt.title('Sampling frequency at '+str(n))
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()
def displayFourierImage(img, fourier_transform, title):
plt.subplot(121), plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),
plt.imshow(fourier_transform, cmap='gray')
plt.title(title), plt.xticks([]), plt.yticks([])
plt.show()
x = np.linspace(0,400, 400)
y = 1 + np.cos((2*np.pi*x)/256) + np.cos((4*np.pi*x)/256) + np.cos((6*np.pi*x)/256)
img1 = cv2.imread('Image1.pgm',0)
img2 = cv2.imread('Image2.pgm',0)
#fourier transform of the images
ft, ms = dft(img1)
ft2, ms2 = dft(img2)
#Display the function in time and frequency domain
series(x,y)
#Display the function after sampling
#fourier transform of the images
ft, ms = dft(img1)
ft2, ms2 = dft(img2)
#display the log-magnitude of the frequency spectra
displayFourierImage(img1, ms, 'Fourier Transform of Image1.pgm')
displayFourierImage(img2, ms2, 'Fourier Transform of Image2.pgm')
n = 10
#apply the high pass filter to images
img_back = high_pass_filter(img1, n)
displayFourierImage(img1, img_back, 'High Pass Filter')
img_back = high_pass_filter(img2,n)
displayFourierImage(img2, img_back, 'High Pass Filter')
#apply the low pass filter to the images
img_back2 = low_pass_filter(img1,n)
displayFourierImage(img1, img_back2, 'Low Pass Filter')
img_back2 = low_pass_filter(img2,n)
displayFourierImage(img2, img_back2, 'Low Pass Filter')