-
Notifications
You must be signed in to change notification settings - Fork 1
/
FourierInteractive.py
204 lines (170 loc) · 5.13 KB
/
FourierInteractive.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
# -*- coding: utf-8 -*-
"""
FourInteractive.py
Created on Wed Sep 28 16:36:45 2016
@author: slehar
"""
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.widgets import CheckButtons
from PIL import Image
import Tkinter, tkFileDialog
import numpy as np
import numpy.ma as ma
import sys
# Global Variables
rad1 = 0.
rad2 = .15
slidersLocked = False
angle = 0.
angleThresh = -1.
# Get image filename using finder dialog
root = Tkinter.Tk()
root.withdraw() # Hide the root window
imgFile = tkFileDialog.askopenfilename(
initialfile = 'Rover.png')
#### Open figure window ####
winXSize = 16
winYSize = 6
winAspect = winXSize/winYSize
plt.close('all')
fig = plt.figure(figsize=(winXSize, winYSize))
fig.canvas.set_window_title('Fourier Interactive')
# Keypress 'q' to quit callback function
def press(event):
global ptList, data
sys.stdout.flush()
if event.key == 'q':
plt.close()
# Connect keypress event to callback function
fig.canvas.mpl_connect('key_press_event', press)
# Lock Sliders Checkbox
rax = plt.axes([0.2, 0.05, 0.1/winAspect, 0.1])
check = CheckButtons(rax, ['Lock'], [False])
def func(label):
global slidersLocked
if label == 'Lock':
slidersLocked = check.lines[0][0].get_visible()
plt.draw()
check.on_clicked(func)
# Read image into numpy array
imgPil = Image.open(imgFile).convert('LA')
imgNp = np.array(imgPil.convert('L'))/255.
ySize, xSize = imgNp.shape
hafY, hafX = int(ySize/2), int(xSize/2)
# Axes for Original Image
axOrig = fig.add_axes([.05, .2, .7/winAspect, .7])
axOrig.axes.set_xticks([])
axOrig.axes.set_yticks([])
axOrig.set_title('Original')
imgplot = plt.imshow(imgPil, cmap='gray')
# Axes for Fourier Image
axFour = fig.add_axes([.3, .2, .7/winAspect, .7])
axFour.axes.set_xticks([])
axFour.axes.set_yticks([])
axFour.set_title('Fourier')
# Axes for Inverse Fourier Image
axFourInv = fig.add_axes([.56, .2, .7/winAspect, .7])
axFourInv.axes.set_xticks([])
axFourInv.axes.set_yticks([])
axFourInv.set_title('Inverse Fourier')
# Filter radius sliders
axSlider1 = fig.add_axes([0.3, 0.125, 0.234, 0.04])
axSlider1.set_xticks([])
axSlider1.set_yticks([])
#axSlider2 = plt.axes([0.3, 0.05, 0.237, 0.04])
axSlider2 = fig.add_axes([0.3, 0.05, 0.237, 0.04])
axSlider2.set_xticks([])
axSlider2.set_yticks([])
# Filter angular sliders
axSlider3 = fig.add_axes([0.7, 0.125, 0.234, 0.04])
axSlider3.set_xticks([])
axSlider3.set_yticks([])
#axSlider4 = plt.axes([0.7, 0.05, 0.237, 0.04])
axSlider4 = fig.add_axes([0.7, 0.05, 0.237, 0.04])
axSlider4.set_xticks([])
axSlider4.set_yticks([])
slider3 = Slider(axSlider3, 'angle', -np.pi, np.pi, valinit=0)
slider4 = Slider(axSlider4, 'thresh', -1., 1., valinit=-1.)
angle, angleThresh = slider3.val, slider4.val
slider1 = Slider(axSlider1, 'r1', 0.0, xSize, valinit=xSize*rad1)
slider2 = Slider(axSlider2, 'r2', 0.0, xSize, valinit=xSize*rad2)
rad1, rad2 = slider1.val, slider2.val
# Fourier Transform
fourImg = np.fft.fft2(imgNp)
fourShft = np.fft.fftshift(fourImg)
fourLog = np.log(np.abs(fourShft))
#plt.sca(axFour)
#fourPlot = plt.imshow(fourLog, cmap='gray')
plt.pause(.001)
#### Fourier Filtering ####
yy, xx = np.mgrid[-hafY:hafY, -hafX:hafX]
distImg = np.sqrt(xx**2 + yy**2)
angleImg = np.arctan2(yy,xx)
angleImgFlip = np.fliplr(np.flipud(angleImg))
maskImg = (distImg < (rad2 * xSize))
xmask = ma.make_mask(maskImg)
filtImg = fourShft * xmask
filtLog = np.log(np.maximum(np.abs(filtImg),1.))
plt.sca(axFour)
fourPlot = plt.imshow(filtLog, cmap='gray')
plt.pause(.001)
# Inverse Fourier Transform
fourIshft = np.fft.ifftshift(filtImg)
fourInv = np.fft.ifft2(fourIshft)
fourReal = np.real(fourInv)
plt.sca(axFourInv)
invPlot = plt.imshow(fourReal, cmap='gray')
def update():
plt.sca(axFour)
maskR1 = (distImg > rad1)
maskR2 = (distImg < rad2)
maskRadial = np.logical_and(maskR1, maskR2)
maskAngle = (np.sin(angleImg*2. + angle) >= angleThresh)
maskImg = np.logical_and(maskAngle, maskRadial)
maskImg[hafY,hafX] = True
xmask = ma.make_mask(maskImg)
filtImg = fourShft * xmask
filtLog = np.log(np.maximum(np.abs(filtImg),1.))
fourPlot.set_data(filtLog)
plt.sca(axFourInv)
fourIshft = np.fft.ifftshift(filtImg)
fourInv = np.fft.ifft2(fourIshft)
fourReal = np.real(fourInv)
invPlot.set_data(fourReal)
plt.pause(.001)
def update1(val):
global rad1
rad1 = slider1.val
update()
def update2(val):
global rad2
diff = max((rad2 - rad1), 0.)
rad2 = slider2.val
if slidersLocked:
val1 = rad2 - diff
slider1.set_val(val1)
update()
def update3(val):
global angle
angle = slider3.val
update()
def update4(val):
global angleThresh
angleThresh = slider4.val
update()
# Attach sliders to their callback functions
slider1.on_changed(update1)
slider2.on_changed(update2)
slider3.on_changed(update3)
slider4.on_changed(update4)
# Show image
# plt.ion()
plt.sca(axFour)
plt.show()
# Pop fig window to top]]
# figmgr=plt.get_current_fig_manager()
# figmgr.canvas.manager.window.raise_()
# geom=figmgr.window.geometry()
# (xLoc,yLoc,dxWidth,dyHeight)=geom.getRect()
# figmgr.window.setGeometry(10,10,dxWidth,dyHeight)