-
Notifications
You must be signed in to change notification settings - Fork 1
/
processDataWithCustomROI.py
434 lines (291 loc) · 13.4 KB
/
processDataWithCustomROI.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 12 17:19:27 2020
@author: fubar
"""
import numpy as np
import cv2
######## special configuration of matplotlib ######
import matplotlib as mpl
#reload interactive backend Qt5Agg
mpl.use('Qt5Agg')
#read the absolute filepath to stylesheet
from KerrPy.File.loadFilePaths import mpl_stylesheet_file
from globalVariables import use_tex
mpl.style.use(mpl_stylesheet_file)
mpl.rcParams['text.usetex'] = use_tex
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
######## ends here ###############################
from globalVariables import deep_debug, dict_ROI, image_shape
from KerrPy.File.loadFilePaths import space_filepath
from KerrPy.Image.processOpenCV import findEdge, saveImage, saveRestoredImage
from KerrPy.Image.processROI import restoreROI, restoreColorROI
from KerrPy.File.processPulse import savePulse
from KerrPy.Fits.processData import processData
from KerrPy.File.processSpace import saveSpace
# initialize list for indices of the image
list_counters = []
list_space_shape = [0,0,0,0]
list_pulse_index = []
list_iter_index = []
list_exp_index = []
list_img_file = []
list_counters = [list_space_shape, list_pulse_index, list_iter_index, list_exp_index, list_img_file]
# find the indices
processData(list_counters=list_counters)
if deep_debug: print(f'list_counters: {list_counters}')
list_space_shape = list_counters[0]
list_pulse_index = list_counters[1]
list_iter_index = list_counters[2]
list_exp_index = list_counters[3]
list_img_file = list_counters[4]
# number of images
n_images = len(list_img_file)
def iterateImages():
"""
this method has the following attributes
0. counter
1. space
2. img
3. img_color
4. img_restored
Upon being called, this method reads the image file
corrsponding to the function attribute `counter`.
Then this image is "blitted" to the first image of the widget.
Blitting the image instead of updating the complete figure results
in very high performance gains in terms of latency, as it involves
updating only the portion of pixels which change when an event is
triggered.
Reference: Blitting tutorial
https://matplotlib.org/3.3.1/tutorials/advanced/blitting.html#sphx-glr-tutorials-advanced-blitting-py
The other attributes space, img_color and img_restored are set
in the `on_select()` callback method for the rectangle selection event
generated by the myWidget object of class type RectangleSelector
"""
# get the counter of the function
i = iterateImages.counter
if i < n_images:
img_file = list_img_file[i]
img = cv2.imread(img_file, 0)
# set the img attribute of iterateImages
iterateImages.img = img
#update the image
blit_img = myWidget.ax.imshow(img, cmap='gray', animated=True)
# draw the animated artist, this uses a cached renderer
myWidget.ax.figure.axes[1].draw_artist(blit_img)
# show the result to the screen, this pushes the updated RGBA buffer from the
# renderer to the GUI framework so you can see it
myWidget.ax.figure.canvas.blit(myWidget.ax.figure.bbox)
##### **IMPORTANT** update the background ################
myWidget.background = myWidget.canvas.copy_from_bbox(myWidget.ax.bbox)
else:
#deactivate the window
myWidget.set_active(False)
###### IMPORTANT save the space in the end ######################
space = iterateImages.space
saveSpace(space)
# event handling functions
def onselect(eclick, erelease):
"""
This function is called when keypress and release event is created.
eclick and erelease are matplotlib events at press and release.
# print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata))
# print('endposition : (%f, %f)' % (erelease.xdata, erelease.ydata))
# print('used button : ', eclick.button)
However we will use `myWidget.corners` attribute
to retrieve the four corners of the rectangle
"""
# reset the background back in the canvas state, screen unchanged
myWidget.ax.figure.canvas.restore_region(bg)
coordinates = myWidget.corners
# update the attribute of iterateImage coordinate
iterateImages.coordinates = coordinates
# get the image attribute
img = iterateImages.img
# save the image_crop
x0 = int(eclick.xdata)
y0 = int(eclick.ydata)
x1 = int(erelease.xdata)
y1 = int(erelease.ydata)
img_crop = img[y0:y1, x0:x1]
# read the coordinates
# coordinates = np.load(coordinates_file)
if deep_debug: print(f"coordinates: {coordinates}")
# current counter
i = iterateImages.counter
pulse_index = list_pulse_index[i]
iter_index = list_iter_index[i]
exp_index = list_exp_index[i]
###### ** important ** pass coordinates as optional keyword argument to findEdge()
pulse, img_color, windowROI = findEdge(pulse_index, iter_index, exp_index, img, coordinates=coordinates)
############## now restore the crop ####################
img_background = np.ones(img.shape, dtype = np.uint8)*255
img_crop_restored = restoreROI(img_crop, img_background, windowROI)
# write the img_crop_restored to cropped axis
blit_crop_restored = myWidget.ax.figure.axes[1].imshow(img_crop_restored, cmap='gray', vmin=0, vmax=255, animated=True)
# myWidget.ax.figure.axes[1].imshow(img_crop, cmap='gray')
# draw the animated artist, this uses a cached renderer
myWidget.ax.figure.axes[1].draw_artist(blit_crop_restored)
# show the result to the screen, this pushes the updated RGBA buffer from the
# renderer to the GUI framework so you can see it
myWidget.ax.figure.canvas.blit(myWidget.ax.figure.bbox)
###########now restore the fit ###############
img_restored = restoreColorROI(img_color, img, windowROI)
# update the subplot restored image
blit_restored = myWidget.ax.figure.axes[2].imshow(img_restored, animated=True)
# draw the animated artist, this uses a cached renderer
myWidget.ax.figure.axes[2].draw_artist(blit_restored)
# show the result to the screen, this pushes the updated RGBA buffer from the
# renderer to the GUI framework so you can see it
myWidget.ax.figure.canvas.blit(myWidget.ax.figure.bbox)
# myWidget.update()
# update the space attribute
space = iterateImages.space
space[exp_index, iter_index, pulse_index] = pulse
iterateImages.space = space
# update the img_color attribute
iterateImages.img_color = img_color
# update the img_restored attribute
iterateImages.img_restored = img_restored
return
def saveIteration():
"""
0. This function is callback to keypress event 'n' or 'N' handled byy
to toggle_selector() method
1. Upon calling, save the params, img_fit, and img_restored.
2. Increment the counter attribute of iterateImages().
We are using function attributes to record state across callbacks.
First we "get" the `counter`, `img_color`, `img_restored` and `space` attributes
which were first "set" by the `onselect()` callback method.
After saving respectively the fit, restored image and parameters
we "set" the `counter` attribute by incremented to point to the next image.
"""
# get the counter
i = iterateImages.counter
print(f"i is {i}")
# read the img_color attribute
img_color = iterateImages.img_color
# get the restored_image attribute
img_restored = iterateImages.img_restored
#read the space attribute
space = iterateImages.space
# indices of the image
pulse_index = list_pulse_index[i]
iter_index = list_iter_index[i]
exp_index = list_exp_index[i]
# get the pulse params from space
pulse = space[exp_index, iter_index, pulse_index]
#save the image to file
saveImage(pulse_index, iter_index, exp_index, img_color)
# save the restored image to file
saveRestoredImage(pulse_index, iter_index, exp_index, img_restored)
# save the params
savePulse(pulse_index, iter_index, exp_index, pulse)
#save the space to file to allow restart options
np.save(space_filepath, space)
# iterate the counter after the save
iterateImages.counter += 1
return
def discardIteration():
"""
0. This function is callback to keypress event 'd' or 'D' handled byy
to toggle_selector() method.
1. It does the job of discarding the image.
Upon calling, set the params to dummy i.e. `np.nan`,
img_fit to img, img_color to img
2. Increment the counter attribute of iterateImages().
"""
# get the counter
i = iterateImages.counter
print(f"i is {i}")
# read the image attribute
img = iterateImages.img
#read the space attribute
space = iterateImages.space
# indices of the image
pulse_index = list_pulse_index[i]
iter_index = list_iter_index[i]
exp_index = list_exp_index[i]
# set the pulse params to np.nan except confidence which is set to 0
pulse = np.array([0, np.nan, np.nan, np.nan, np.nan, np.nan])
# update the pulse params to space
space[exp_index, iter_index, pulse_index] = pulse
iterateImages.space = space
#save the image to file
saveImage(pulse_index, iter_index, exp_index, img)
# save the restored image to file
saveRestoredImage(pulse_index, iter_index, exp_index, img)
# save the params
savePulse(pulse_index, iter_index, exp_index, pulse)
#save the space to file to allow restart options
np.save(space_filepath, space)
# iterate the counter after the save
iterateImages.counter += 1
return
def toggle_selector(event):
"""
0. Key press event handler for the `myWidget` RectangleSelector widget.
1. 'n' or 'N' event calls `saveIteration()` followed by `iterateImages()`
2. 'd' or 'D' event calls `discardIteration()` followed by `iterateImages()`
"""
print('Key pressed.')
if event.key in ['N', 'n'] and myWidget.get_active():
print('Image iterated.')
myWidget.set_active(True)
saveIteration()
iterateImages()
if event.key in ['D', 'd'] and myWidget.get_active():
print('Image discarded.')
myWidget.set_active(True)
discardIteration()
iterateImages()
if event.key in ['A', 'a'] and not myWidget.get_active():
print('RectangleSelector activated.')
myWidget.set_active(True)
if __name__ == '__main__':
if dict_ROI['isWidget']:
##### set the matplotlib figure params ##################
mpl.rcParams['figure.figsize'] = 9.0,4.0
mpl.rcParams['figure.subplot.bottom'] = 0.1
mpl.rcParams['figure.subplot.left'] = 0.1
mpl.rcParams['figure.subplot.right'] = 0.9
mpl.rcParams['figure.subplot.top'] = 0.7
fig, axes = plt.subplots(1,3)
fig.suptitle('widget ROI selection', fontsize= 24)
fig.text(0.5,0.85, '''Press `N' to iterate image, `D' to discard image, `Q' to kill the window''',
ha='center', va='center')
axes[0].set_title('raw image')
axes[1].set_title('cropped image')
axes[2].set_title('restored image')
############# initialize the widget ##################
# use a white background image
img_white = np.ones(image_shape, dtype=np.uint8)*255
axes[0].imshow(img_white, cmap='gray', vmin=0, vmax=255)
axes[1].imshow(img_white, cmap='gray', vmin=0, vmax=255)
# and also set the autoscale OFF
axes[1].set_autoscale_on(False)
# also draw the restored image to set the bbox dimensions
axes[2].imshow(img_white, cmap='gray', vmin=0, vmax=255)
#pause for a 'short' to ensure the figure is rendered
# atleast one before storing the bbox info (in sec)
plt.pause(0.1)
# use this as background for future restoration
# get copy of entire figure (everything inside fig.bbox) sans animated artist
bg = fig.canvas.copy_from_bbox(fig.bbox)
# a reference to the RectangleSelector widget is needed
# to prevent from Garbage Collection
myWidget = RectangleSelector(axes[0], onselect, useblit=True)
######## initialize attributes of iterateImages #############
# set the shape of space array
space = np.zeros((list_space_shape[0], list_space_shape[1], list_space_shape[2], list_space_shape[3]), dtype=np.float)
np.save(space_filepath, np.array(space))
iterateImages.counter = 0
iterateImages.space = space
iterateImages.img = img_white
iterateImages.img_color = img_white
iterateImages.img_restored = img_white
iterateImages()
print(f'fig.canvas.supports_blit property: {fig.canvas.supports_blit}' )
#### IMPORTANT: create event loop for matplotlib fig window ####
myWidget.connect_event('key_press_event', toggle_selector)