-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiltering.py
More file actions
181 lines (138 loc) · 6.42 KB
/
Copy pathFiltering.py
File metadata and controls
181 lines (138 loc) · 6.42 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
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
import numpy as np
class Filtering:
@staticmethod
def get_ideal_low_pass_filter(shape, cutoff, order):
"""Computes a Ideal low pass mask
takes as input:
shape: the shape of the mask to be generated
cutoff: the cutoff frequency of the ideal filter
returns a ideal low pass mask"""
filter = np.zeros(shape, np.float32)
width, height = shape[:2]
for x in range(-int(cutoff),int(cutoff)):
x_pos = int(x + width/2 - 1)
y_range = cutoff*np.sin(np.arccos(x/cutoff))
for y in range(-int(y_range), int(y_range)):
y_pos = int(y + height/2 - 1)
filter[x_pos, y_pos] = 1.0;
return filter
@staticmethod
def get_ideal_high_pass_filter(shape, cutoff, order):
"""Computes a Ideal high pass mask
takes as input:
shape: the shape of the mask to be generated
cutoff: the cutoff frequency of the ideal filter
returns a ideal high pass mask"""
filter = np.ones(shape, np.float32)
filter = filter - Filtering.get_ideal_low_pass_filter(shape, cutoff, order)
return filter
@staticmethod
def get_butterworth_low_pass_filter(shape, cutoff, order):
"""Computes a butterworth low pass mask
takes as input:
shape: the shape of the mask to be generated
cutoff: the cutoff frequency of the butterworth filter
order: the order of the butterworth filter
returns a butterworth low pass mask"""
filter = np.zeros(shape, np.float32)
width, height = shape[:2]
for x in range(width):
for y in range(height):
filter[x, y] = 1/(1+(np.sqrt((x - width/2)**2+(y - height/2)**2)/cutoff)**(2*order));
return filter
@staticmethod
def get_butterworth_high_pass_filter(shape, cutoff, order):
"""Computes a butterworth high pass mask
takes as input:
shape: the shape of the mask to be generated
cutoff: the cutoff frequency of the butterworth filter
order: the order of the butterworth filter
returns a butterworth high pass mask"""
#Hint: May be one can use the low pass filter function to get a high pass mask
filter = np.ones(shape, np.float32)
filter = filter - Filtering.get_butterworth_low_pass_filter(shape, cutoff, order)
return filter
@staticmethod
def get_gaussian_low_pass_filter(shape, cutoff, order):
"""Computes a gaussian low pass mask
takes as input:
shape: the shape of the mask to be generated
cutoff: the cutoff frequency of the gaussian filter (sigma)
returns a gaussian low pass mask"""
filter = np.zeros(shape, np.float32)
width, height = shape[:2]
for x in range(width):
for y in range(height):
filter[x, y] = np.exp(-((x - width/2)**2+(y - height/2)**2)/(2*cutoff**2));
return filter
@staticmethod
def get_gaussian_high_pass_filter(shape, cutoff, order):
"""Computes a gaussian high pass mask
takes as input:
shape: the shape of the mask to be generated
cutoff: the cutoff frequency of the gaussian filter (sigma)
returns a gaussian high pass mask"""
filter = np.ones(shape, np.float32)
filter = filter - Filtering.get_gaussian_low_pass_filter(shape, cutoff, order)
return filter
@staticmethod
def post_process_image(image):
"""Post process the image to create a full contrast stretch of the image
takes as input:
image: the image obtained from the inverse fourier transform
return an image with full contrast stretch
-----------------------------------------------------
1. Full contrast stretch (fsimage)
2. take negative (255 - fsimage)
"""
output = np.log(1 + np.abs(image))
min = output.min()
max = output.max()
P = 255/(max - min)
L = -(P*min)
output = output*P + L
return np.uint8(output)
@staticmethod
def GetMask(image, lowPassParams, highPassParams, isBandPass):
"""Get the combined mask for high pass/lowpass/bandpass/band reject"""
shape = np.shape(image)
lowPassFilter = None
highPassFilter = None
if lowPassParams != None:
filter_name = lowPassParams[0]
if filter_name == 'ideal_l':
lowPassFilter = Filtering.get_ideal_low_pass_filter(shape, lowPassParams[1],lowPassParams[2])
elif filter_name == 'butterworth_l':
lowPassFilter = Filtering.get_butterworth_low_pass_filter(shape, lowPassParams[1],lowPassParams[2])
elif filter_name == 'gaussian_l':
lowPassFilter = Filtering.get_gaussian_low_pass_filter(shape, lowPassParams[1],lowPassParams[2])
if highPassParams != None:
filter_name = highPassParams[0]
if filter_name == 'ideal_h':
highPassFilter = Filtering.get_ideal_high_pass_filter(shape, highPassParams[1],highPassParams[2])
elif filter_name == 'butterworth_h':
highPassFilter = Filtering.get_butterworth_high_pass_filter(shape, highPassParams[1],highPassParams[2])
elif filter_name == 'gaussian_h':
highPassFilter = Filtering.get_gaussian_high_pass_filter(shape, highPassParams[1],highPassParams[2])
if lowPassParams != None and highPassParams != None:
mask = lowPassFilter + highPassFilter
if isBandPass:
mask = 1 - mask
elif lowPassParams != None and highPassParams == None:
mask = lowPassFilter
elif lowPassParams == None and highPassParams != None:
mask = highPassFilter
else:
mask = np.ones(shape, np.uint8)
return mask
@staticmethod
def ApplyFiltering(image, mask):
"""Performs frequency filtering on an input image
returns a filtered image, magnitude of DFT, magnitude of filtered DFT
"""
dft = np.fft.fft2(image)
shifted_dft = np.fft.fftshift(dft)
filtered_dft = shifted_dft * mask
shifted_ift = np.fft.ifft2(filtered_dft)
filtered_image = np.abs(shifted_ift)
return Filtering.post_process_image(filtered_image), Filtering.post_process_image(np.abs(shifted_dft)), Filtering.post_process_image(filtered_dft)