-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanktonImageAnalysis.py
More file actions
1530 lines (1378 loc) · 72.9 KB
/
Copy pathPlanktonImageAnalysis.py
File metadata and controls
1530 lines (1378 loc) · 72.9 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
# A module constructing a collage of plankton images for classification using a graphical user interface
# Requires Python 3.6 or later.
# Danny Grunbaum, PublicSensors.org and University of Washington 20200420-20200706
import os
import pickle
import gzip
import copy
from PIL import Image, ImageFilter, ImageOps
import numpy as np
import cv2
import csv
import time
import random
from matplotlib.patches import Rectangle, Circle, Polygon, Ellipse
import matplotlib.colors as mpl_colors
import matplotlib.patches as mpl_patches
import matplotlib.mathtext as mathtext
import matplotlib.artist as mpl_artist
import matplotlib.image as mpl_image
from matplotlib.widgets import LassoSelector
from matplotlib.path import Path
from matplotlib.transforms import IdentityTransform
# Specify the backend for matplotlib -- default does not allow interactive mode
from matplotlib import use
use('TkAgg') # this needs to happen before pyplot is loaded
import matplotlib.pyplot as plt
plt.ion() # set interactive mode
from matplotlib.backend_bases import MouseButton
from matplotlib import get_backend
from configGUI import *
#from config23 import *
from tkinter import Tk, simpledialog
from tkinter.filedialog import askopenfilename, asksaveasfilename
#root = Tk()
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
import imageio
global load_dir
load_dir=os.getcwd()
global selector
#============================================================================
version = 'PIA_20220120'
print('PlanktonImageAnalysis version',version)
#============================================================================
# Default thresholds for segmenting blobs from a image or binary image
#minThreshold = 5
minThreshold = 20 # Can/should be reset during creation of Analysis or Frame objects
#minThreshold = 50 # Can/should be reset during creation of Analysis or Frame objects
#minThreshold = 10
maxThreshold = 255 # Should genereally be 255
minArea=10 # Obsolete, currently used only in depreciated "simple" blob segmentation
#============================================================================
# Lasso selection infrastructure from the matplotlib demo, lasso_selector_demo_sgskip.py
class SelectFromCollection(object):
"""Select indices from a matplotlib collection using `LassoSelector`.
Selected indices are saved in the `ind` attribute. This tool fades out the
points that are not part of the selection (i.e., reduces their alpha
values). If your collection has alpha < 1, this tool will permanently
alter the alpha values.
Note that this tool selects collection objects based on their *origins*
(i.e., `offsets`).
Parameters
----------
ax : :class:`~matplotlib.axes.Axes`
Axes to interact with.
collection : :class:`matplotlib.collections.Collection` subclass
Collection you want to select from.
alpha_other : 0 <= float <= 1
To highlight a selection, this tool sets all selected points to an
alpha value of 1 and non-selected points to `alpha_other`.
"""
def __init__(self, ax, collection, alpha_other=0.3):
self.canvas = ax.figure.canvas
self.collection = collection
self.alpha_other = alpha_other
self.xys = collection.get_offsets()
self.Npts = len(self.xys)
# Ensure that we have separate colors for each object
self.fc = collection.get_facecolors()
if len(self.fc) == 0:
raise ValueError('Collection must have a facecolor')
elif len(self.fc) == 1:
self.fc = np.tile(self.fc, (self.Npts, 1))
self.lasso = LassoSelector(ax, onselect=self.onselect)
self.ind = []
def onselect(self, verts):
path = Path(verts)
self.ind = np.nonzero(path.contains_points(self.xys))[0]
self.fc[:, -1] = self.alpha_other
self.fc[self.ind, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()
def disconnect(self):
self.lasso.disconnect_events()
self.fc[:, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()
# Menu bar infrastructure for choosing classes, modified from an example on the matplotlib site;
# Uses new version of menu construction in revised menu.py from matplotlib gallery, downloaded 20210407
class ItemProperties:
def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
alpha=1.0):
self.fontsize = fontsize
self.labelcolor = labelcolor
self.bgcolor = bgcolor
self.alpha = alpha
class MenuItem(mpl_artist.Artist):
padx = 5
pady = 5
def __init__(self, fig, labelstr, props=None, hoverprops=None,
on_select=None,number=0):
super().__init__()
self.set_figure(fig)
self.labelstr = labelstr
self.props = props if props is not None else ItemProperties()
self.hoverprops = (
hoverprops if hoverprops is not None else ItemProperties())
if self.props.fontsize != self.hoverprops.fontsize:
raise NotImplementedError(
'support for different font sizes not implemented')
self.on_select = on_select
self.number=number
# Setting the transform to IdentityTransform() lets us specify
# coordinates directly in pixels.
self.label = fig.text(0, 0, labelstr, transform=IdentityTransform(),
size=props.fontsize)
self.text_bbox = self.label.get_window_extent(
fig.canvas.get_renderer())
self.rect = mpl_patches.Rectangle((0, 0), 1, 1) # Will be updated later.
self.set_hover_props(False)
fig.canvas.mpl_connect('button_release_event', self.check_select)
def check_select(self, event):
over, _ = self.rect.contains(event)
if not over:
return
if self.on_select is not None:
self.on_select(self)
def set_extent(self, x, y, w, h, depth):
self.rect.set(x=x, y=y, width=w, height=h)
self.label.set(position=(x + self.padx, y + depth + self.pady/2))
self.hover = False
def draw(self, renderer):
self.rect.draw(renderer)
self.label.draw(renderer)
def set_hover_props(self, b):
props = self.hoverprops if b else self.props
self.label.set(color=props.labelcolor)
self.rect.set(facecolor=props.bgcolor, alpha=props.alpha)
def set_hover(self, event):
"""
Update the hover status of event and return whether it was changed.
"""
b, _ = self.rect.contains(event)
changed = (b != self.hover)
if changed:
self.set_hover_props(b)
self.hover = b
return changed
class Menu:
def __init__(self, fig, x0, y0, menuitems):
self.figure = fig
self.menuitems = menuitems
maxw = max(item.text_bbox.width for item in menuitems)
maxh = max(item.text_bbox.height for item in menuitems)
depth = max(-item.text_bbox.y0 for item in menuitems)
#totalh = self.numitems*maxh + (self.numitems + 1)*2*MenuItem.pady
#x0 = 100
#y0 = 400
width = maxw + 2*MenuItem.padx
height = maxh + MenuItem.pady
for item in menuitems:
left = x0
bottom = y0 - maxh - MenuItem.pady
item.set_extent(left, bottom, width, height, depth)
fig.artists.append(item)
y0 -= maxh + MenuItem.pady
fig.canvas.mpl_connect('motion_notify_event', self.on_move)
def on_move(self, event):
if any(item.set_hover(event) for item in self.menuitems):
self.figure.canvas.draw()
#============================================================================
# A function to move windows on the desktop, following
# https://stackoverflow.com/questions/7449585/how-do-you-set-the-absolute-position-of-figure-windows-with-matplotlib
def move_figure(f, x, y):
"""Move figure's upper left corner to pixel (x, y)"""
backend = get_backend()
if backend == 'TkAgg':
f.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
elif backend == 'WXAgg':
f.canvas.manager.window.SetPosition((x, y))
else:
# This works for QT and GTK
# You can also use window.setGeometry
f.canvas.manager.window.move(x, y)
#============================================================================
# Create the classification GUI
lbl_fig = plt.figure(figsize=lbl_figsize,facecolor='k')
lbl_fig.subplots_adjust(left=0.01)
lbl_fig.canvas.manager.set_window_title('Category Selection')
#lbl_fig.canvas.set_window_title('Category Selection')
move_figure(lbl_fig,lbl_figpos[0],lbl_figpos[1])
menuitems = []
for ilabel in range(len(labels)):
label=labels[ilabel]
props = ItemProperties(labelcolor='black', bgcolor=colors[ilabel],fontsize=lbl_fontsize, alpha=lbl_alpha)
hoverprops = ItemProperties(labelcolor='white', bgcolor=colors[ilabel],
fontsize=lbl_fontsize, alpha=lbl_alpha)
def on_select(item):
global cat_number, cat_color, cat_label
print('you selected %d, %s' % (item.number, item.labelstr))
cat_number=item.number
cat_color=colors[item.number]
cat_label=item.labelstr
item = MenuItem(lbl_fig, label, props=props, hoverprops=hoverprops,
on_select=on_select, number=ilabel)
menuitems.append(item)
menu = Menu(lbl_fig, lbl_x0, lbl_y0, menuitems)
#============================================================================
# Define a class to facilitate assignment and handling of ROI image classification
class Frame():
"""A class to contain and analyze full images ("frames") from ZooCAM profiles
"""
global selector
def __init__(self,frame_dir=None,frame_file=None,frame_image=None,ROIlist=[],ROIgroup=[],counter=None,display=False):
self.frame_dir=frame_dir
self.frame_file=frame_file
if frame_image is not None:
self.frame_image=frame_image
elif self.frame_file is not None:
try:
self.read_frame()
except:
self.frame_image=None
#print('ERROR: Failed to load frame_ file %s' % self.frame_file)
if display:
try:
self.show_frame()
except:
print('ERROR: Failed to show frame_ file %s' % self.frame_file)
self.counter=counter
self.ROIlist=ROIlist
self.ROIgroup=ROIgroup
print('creating Frame, len(self.ROIlist)=',len(self.ROIlist))
def read_frame(self,frame_dir=None,frame_file=None):
print('reading frame...')
if frame_dir is not None:
self.frame_dir=frame_dir
if frame_file is not None:
self.frame_file=frame_file
frame_path=os.path.join(self.frame_dir,self.frame_file)
print('path is %s' % frame_path)
try:
self.frame_image=cv2.imread(frame_path, cv2.IMREAD_GRAYSCALE)
self.color_frame_image=cv2.imread(frame_path)#,cv2.COLOR_BAYER_RG2RGB)
self.color_frame_image=imageio.imread(frame_path)
#self.color_frame_image=cv2.imread(frame_path, cv2.IMREAD_ANYCOLOR)
print('Loaded frame from path %s' % frame_path)
except:
self.frame_image=None
print('ERROR: Failed to load frame from path %s' % frame_path)
def show_frame(self,fig_num=100):
try:
plt.figure(fig_num,facecolor=tuple([i/255 for i in bg_color]))
plt.imshow(self.frame_image, cmap='gray', interpolation='bicubic')
plt.tight_layout(pad=plt_pad)
except:
print('ERROR: Failed to show frame image...')
def binary_frame(self, min_val=minThreshold, max_val=maxThreshold,display=False,fill_holes=True,fig_num=101):
#def binary_frame(self, min_val=thr_min_val, max_val=thr_max_val,display=False,fill_holes=True,fig_num=101):
self.binary_image=cv2.threshold(self.frame_image, min_val, max_val, cv2.THRESH_BINARY)[1]
#print('before:',self.binary_image)
if fill_holes: # Fill holes within thresholded blobs
# after example at https://www.programcreek.com/python/example/89425/cv2.floodFill
frame_floodfill=self.binary_image.copy()
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = self.binary_image.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(frame_floodfill, mask, (0,0), 255);
# Invert floodfilled image
frame_floodfill_inv = cv2.bitwise_not(frame_floodfill)
# Combine the two images to get the foreground.
self.binary_image = self.binary_image.astype(np.uint8) | frame_floodfill_inv.astype(np.uint8)
#print('after:',self.binary_image)
self.binary_fig_num=fig_num
if display:
self.show_binary_frame()
def show_binary_frame(self):
plt.figure(self.binary_fig_num,facecolor=tuple([i/255 for i in bg_color]))
plt.imshow(self.binary_image, cmap='gray')
plt.tight_layout(pad=plt_pad)
title_str='Figure '+str(self.binary_fig_num)+ \
', Filename: '+self.frame_file
plt.gcf().canvas.manager.set_window_title(title_str)
#plt.gcf().canvas.set_window_title(title_str)
def show_blobs_frame(self):
print('plotting contours...')
cnt_fig=plt.figure(self.blobs_fig_num,facecolor=tuple([i/255 for i in bg_color]))
cnt_fig.clf()
title_str='Figure '+str(self.blobs_fig_num)+ \
', Filename: '+self.frame_file
plt.gcf().canvas.manager.set_window_title(title_str)
#plt.gcf().canvas.set_window_title(title_str)
if self.use_binary:
plt.imshow(self.binary_image, cmap='gray')
else:
plt.imshow(self.frame_image, cmap='gray')
for ctr in self.contours:
polygon = Polygon(np.squeeze(ctr, axis=1),True,linewidth=1,edgecolor='m',facecolor='none')
# Add the patch to the Axes
plt.gca().add_patch(polygon)
bbox=cv2.boundingRect(ctr)
rect = Rectangle((bbox[0],bbox[1]),bbox[2],bbox[3],
linewidth=1,edgecolor='c',facecolor='none')
# Add the patch to the Axes
plt.gca().add_patch(rect)
plt.tight_layout(pad=plt_pad)
cnt_fig.canvas.draw()
def show_ROIs_frame(self,color_classes=True):
roi_fig=plt.figure(self.ROI_fig_num,facecolor=tuple([i/255 for i in bg_color]))
roi_fig.clf()
title_str='Figure '+str(self.ROI_fig_num)+ \
', Filename: '+self.frame_file
plt.gcf().canvas.manager.set_window_title(title_str)
#plt.gcf().canvas.set_window_title(title_str)
if self.use_binary:
plt.imshow(self.binary_image, cmap='gray')
else:
plt.imshow(self.color_frame_image, cmap='gray')
#plt.imshow(self.frame_image, cmap='gray')
edge_color='r'
for roi in self.ROIlist:
if color_classes:
edge_color=roi.fill_color
rect = Rectangle((roi.j_beg,roi.i_beg),roi.j_end-roi.j_beg,roi.i_end-roi.i_beg,
linewidth=1,edgecolor=edge_color,facecolor='none')
# Add the patch to the Axes
plt.gca().add_patch(rect)
plt.tight_layout(pad=plt_pad)
cfm = plt.get_current_fig_manager()
cfm.window.attributes('-topmost', True)
cfm.window.attributes('-topmost', False)
roi_fig.canvas.draw()
def show_all_frames(self):
self.show_binary_frame()
self.show_blobs_frame()
self.show_ROIs_frame()
def select_group(self):
# bring ROI figure to the front
plt.figure(self.ROI_fig_num)
cfm = plt.get_current_fig_manager()
cfm.window.attributes('-topmost', True)
cfm.window.attributes('-topmost', False)
# Add reference points to all ROIs
ref_pts=np.zeros([len(self.ROIlist),2])
for i,roi in enumerate(self.ROIlist):
ref_pts[i,0]=roi.j_beg
ref_pts[i,1]=roi.i_beg
pts=plt.scatter(ref_pts[:, 0], ref_pts[:, 1], color='r',s=20)
print("Entering group selection: select ROIs with lasso, then <cr> to accept, c or q to cancel...")
selector = SelectFromCollection(plt.gca(),pts)
def accept(event):
if event.key == "enter":
print("New group formed with ROIs:")
print('selector.ind=',selector.ind)
# Check for empty group selections:
if len(selector.ind)==0:
print('Empty grouping selected; skipping group definition...')
else:
print('selector.xys[selector.ind]=',selector.xys[selector.ind])
new_group_num=self.create_group(selector.ind)
self.plot_group(new_group_num)
selector.ind=[]
selector.xys=[]
selector.disconnect()
plt.disconnect(self.binding_id)
self.show_ROIs_frame()
elif event.key == "c" or event.key == "q":
print('cancelled...')
selector.ind=[]
selector.xys=[]
selector.disconnect()
plt.disconnect(self.binding_id)
self.show_ROIs_frame()
self.binding_id=plt.gcf().canvas.mpl_connect("key_press_event", accept)
def create_group(self,grp_ind,reset_current=True,reset_previous=True):
# Create a group out of the indices in the list grp_ind,
# and any ROIs these are already grouped with.
# "reset_" flags determine whether respective categories are reset.
# Default behavior is to reset both, to avoid divergence in categories
# within a group (e.g. with "undo").
grps = [*{*[self.ROIgroup[ir] for ir in grp_ind]}] # unique list of represented groups
print('grps=',grps)
extended_grp_ind=[]
for ir,roi in enumerate(self.ROIlist): # set all ROIs with specified group numbers
if self.ROIgroup[ir] in grps: # to lowest group number
self.ROIgroup[ir]=grps[0]
roi.group=grps[0]
if reset_current:
roi.category=default_category
if reset_previous:
roi.prev_category=default_category
roi.fill_color=colors[roi.category]
roi.label=labels[roi.category]
roi.code=codes[roi.category]
print('Forming new group #',grps[0], ' with members ',self.ROIgroup)
return grps[0]
def plot_group(self,igrp,grp_fig_num=106):
# Plot ROIs in specified group to a new image window
self.grp_fig_num=grp_fig_num
grp_fig=plt.figure(self.grp_fig_num,facecolor=tuple([i/255 for i in bg_color]))
grp_fig.clf()
title_str='Group #'+str(igrp)+' Figure '+str(self.grp_fig_num)+ \
', Filename: '+self.frame_file
plt.gcf().canvas.manager.set_window_title(title_str)
#plt.gcf().canvas.set_window_title(title_str)
if self.use_binary:
plt.imshow(self.binary_image, cmap='gray')
else:
plt.imshow(self.frame_image, cmap='gray')
for ir,roi in enumerate(self.ROIlist):
if roi.group == igrp: # indicate selected group with a cyan ROI box
#if self.ROIgroup[ir] == igrp: # indicate selected group with a cyan ROI box
colr='c'
else:
colr='r'
rect = Rectangle((roi.j_beg,roi.i_beg),roi.j_end-roi.j_beg,roi.i_end-roi.i_beg,
linewidth=1,edgecolor=colr,facecolor='none')
# Add the patch to the Axes
plt.gca().add_patch(rect)
plt.tight_layout(pad=plt_pad)
cfm = plt.get_current_fig_manager()
cfm.window.attributes('-topmost', True)
cfm.window.attributes('-topmost', False)
grp_fig.canvas.draw()
def export_all_groups(self,target_dir,grp_export_fig_num=206,plotting=False,verbose=False,delay=1):
''' Method to export classified ROIs to a specified directory (target_dir), into subdirectories
named for the classification labels. Subdirectories are created if not already present
within target_dir; target_dir itself is not presently created if not already present.
'''
for igrp in self.ROIgroup:
self.export_group(igrp,target_dir,grp_export_fig_num=grp_export_fig_num,plotting=plotting,verbose=verbose)
time.sleep(delay)
def export_group(self,igrp,target_dir,grp_export_fig_num=206,plotting=False,verbose=False,exportROI=True,exportEll=True):
''' Method to export classified ROIs to a specified directory (target_dir), into subdirectories
named for the classification labels. Subdirectories are created if not already present
within target_dir; target_dir itself is not presently created if not already present.
'''
# get list of group members
grp_inds = self.get_group_members(igrp)
#grp_ind = self.ROIgroup.index(igrp)
mem_inds=[self.ROIindices[grp_ind] for grp_ind in grp_inds]
#mem_inds=self.ROIindices[grp_ind]
# get extent of group ROIs in frame
i_beg=min([self.ROIlist[mem_ind].i_beg for mem_ind in mem_inds])
i_end=max([self.ROIlist[mem_ind].i_end for mem_ind in mem_inds])
j_beg=min([self.ROIlist[mem_ind].j_beg for mem_ind in mem_inds])
j_end=max([self.ROIlist[mem_ind].j_end for mem_ind in mem_inds])
if verbose:
print('i_beg,j_beg,i_end,j_end = ',i_beg,j_beg,i_end,j_end)
# create an image with the composite of group ROIs
composite=Image.new('RGB',[i_end-i_beg,j_end-j_beg]) # new image with default black background
for mem_ind in mem_inds:
if verbose:
print('mem_ind = ',mem_ind)
print('category,label,code = ',self.ROIlist[mem_ind].category,self.ROIlist[mem_ind].label,self.ROIlist[mem_ind].code)
print('image size = ',self.ROIlist[mem_ind].ROIimage.size)
print('box = ',[self.ROIlist[mem_ind].i_beg-i_beg,self.ROIlist[mem_ind].j_beg-j_beg,
self.ROIlist[mem_ind].i_end-i_beg-1,self.ROIlist[mem_ind].j_end-j_beg-1])
composite.paste(self.ROIlist[mem_ind].ROIimage,
box=[self.ROIlist[mem_ind].i_beg-i_beg,self.ROIlist[mem_ind].j_beg-j_beg])
#composite.paste(self.ROIlist[mem_ind].ROIimage,
#box=[self.ROIlist[mem_ind].i_beg-i_beg,self.ROIlist[mem_ind].j_beg-j_beg,
# self.ROIlist[mem_ind].i_end-i_beg,self.ROIlist[mem_ind].j_end-j_beg])
# construct path for the saved image
grp_dir_path=os.path.join(target_dir,self.ROIlist[mem_inds[0]].label.replace('/','_'))
if os.path.exists(grp_dir_path)==False:
print('creating new image directory: ',grp_dir_path)
os.mkdir(grp_dir_path)
# Add ROI geometry to ROI filename
grp_image_name=self.frame_file.replace('.tif','_grp')+str(igrp)
if self.exportRoi:
grp_image_name+='_r{}_{}_{}_{}'.format(i_beg,j_beg,i_end-i_beg,j_end-j_beg)
if self.exportEll:
ellbox=self.ROIlist[mem_ind].ellbox
grp_image_name+='_e{}_{}_{}_{}_{}'.format(ellbox[0][0],ellbox[0][1],ellbox[1][0],ellbox[1][1],ellbox[2])
grp_image_name+='.tif'
#grp_image_name=self.frame_file.replace('.tif','_grp')+str(igrp)+'.tif'
grp_image_path=os.path.join(grp_dir_path,grp_image_name)
if verbose:
print('Creating classified image: ',grp_image_path)
#print('category,label,code = ',self.ROIlist[mem_ind].category,self.ROIlist[mem_ind].label,self.ROIlist[mem_ind].code)
try:
composite.save(grp_image_path)
except:
print('ERROR: Failed to create classified image: ',grp_image_path)
if plotting:
# Create a new image window to plot the specified group
self.grp_export_fig_num=grp_export_fig_num
grp_export_fig=plt.figure(self.grp_export_fig_num,facecolor=tuple([i/255 for i in bg_color]))
grp_export_fig.clf()
title_str='Group #'+str(igrp)+' Figure '+str(self.grp_export_fig_num)+ \
', Filename: '+self.frame_file
plt.gcf().canvas.manager.set_window_title(title_str)
#plt.gcf().canvas.set_window_title(title_str)
plt.imshow(composite, cmap='gray')
grp_export_fig.canvas.draw()
def get_group_members(self,igrp):
# Return a list of all members of the indicated group
print('Getting members of group ',igrp)
group_members=[]
for ir,roi in enumerate(self.ROIlist): # set all ROIs with specified group numbers
#print('ir,roi.group = ',ir,roi.group)
if roi.group == igrp:
group_members.append(ir)
return group_members
def classify_group(self,igrp,next_category,reset_previous=False):
# Set new classification of all ROIs in group igrp.
# If reset_previous is True, the previous category is also reset.
# This should be done whenever a new group is formed, so that members
# of groups cannot diverge with "undo" clicks..
print('Classifying group ',igrp)
for ir,roi in enumerate(self.ROIlist): # set all ROIs with specified group numbers
if roi.group == igrp: # to lowest group number
print('ir,roi.group = ',ir,roi.group)
print('prev.,current, new category = ',roi.prev_category,roi.category,next_category)
roi.prev_category=roi.category # shift current category into previous
roi.category=next_category # replace current category with submitted next category
roi.fill_color=colors[roi.category]
roi.label=labels[roi.category]
roi.code=codes[roi.category]
def dissolve_group(self,igrp):
print('dissolving group ',igrp)
for ir,roi in enumerate(self.ROIlist): # dissolve group, by setting all ROI group
if self.ROIgroup[ir] == igrp: # numbers back to ROOI index
self.ROIgroup[ir]=ir
roi.group=ir
roi.category=default_category
roi.prev_category=default_category
roi.fill_color=colors[roi.category]
roi.label=labels[roi.category]
roi.code=codes[roi.category]
self.plot_group(igrp)
self.show_ROIs_frame()
def segment_frame(self, method='contour',min_val=minThreshold, max_val=maxThreshold,min_area=minArea,
display_ROIs=False,display_blobs=False,display_blobsCV=False,use_binary=True,
fig_numROI=102,fig_numBLOB=103,fig_numCTR=104,category=None,ROIpad=15):
if method == 'simple':
self.segment_frameSIMPLE(min_val=min_val, max_val=max_val,min_area=min_area,
display_ROIs=display_ROIs,display_blobs=display_blobs,display_blobsCV=display_blobsCV,
use_binary=use_binary,fig_numROI=fig_numROI,fig_numBLOB=fig_numBLOB,category=category)
elif method == 'contour':
self.segment_frameCONTOUR(min_val=min_val, max_val=max_val,min_area=min_area,
display_ROIs=display_ROIs,display_blobs=display_blobs,display_blobsCV=display_blobsCV,
use_binary=use_binary,fig_numROI=fig_numROI,fig_numBLOB=fig_numBLOB,category=category,
fig_numCTR=fig_numCTR,ROIpad=ROIpad)
else:
print("Unknown method, '%s', for Frame segmentation; valid choices are 'simple' and 'contour'")
return
self.ROIgroup=[]
for i in range(len(self.ROIlist)): # Parse ROIs into groups; initially each group contains only
self.ROIgroup.append(i) # a single ROI, to be aggregated subsequently e.g. using lasso
self.ROIlist[i].group=i # Revised group infrastructure with group residing in the ROI object
def segment_frameCONTOUR(self, min_val=minThreshold, max_val=maxThreshold,min_area=minArea,
display_ROIs=False,display_blobs=False,display_blobsCV=False,use_binary=True,
fig_numROI=102,fig_numBLOB=103,fig_numCTR=104,category=None,ROIpad=5):
print('starting segment_frameCONTOUR: len(self.ROIlist)=',len(self.ROIlist))
self.ROIlist=[]
self.blob_keypoints = []
self.blobs_fig_num = fig_numBLOB
self.ROI_fig_num = fig_numROI
self.use_binary=use_binary
self.contours, hierarchy = cv2.findContours(self.binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
#self.contours, hierarchy = cv2.findContours(self.binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if display_blobs:
self.show_blobs_frame()
# dimensions of image in pixels
nx=len(self.frame_image)
ny=len(self.frame_image[0])
# Parse ROIs from contours
for ctr in self.contours:
#print(ctr[:,0,0])
area=cv2.contourArea(ctr)
bbox=cv2.boundingRect(ctr)
try:
ellbox = cv2.fitEllipse(ctr)
ell = Ellipse((ellbox[0][0],ellbox[0][1]),ellbox[1][0],ellbox[1][1],angle=ellbox[2],
linewidth=1,edgecolor='y',facecolor='none')
if display_ROIs:
plt.gca().add_patch(ell)
except:
ellbox=None
i_beg=np.max([np.min(ctr[:,0,1])-ROIpad,0])
i_end=np.min([np.max(ctr[:,0,1])+ROIpad,nx-1])
j_beg=np.max([np.min(ctr[:,0,0])-ROIpad,0])
j_end=np.min([np.max(ctr[:,0,0])+ROIpad,ny-1])
# get blob subimage
blob_img = Image.fromarray( self.color_frame_image[i_beg:i_end, j_beg:j_end])
#blob_img = Image.fromarray( cv2.cvtColor(self.color_frame_image[i_beg:i_end, j_beg:j_end], cv2.COLOR_BGR2RGB))
#blob_img = Image.fromarray( cv2.cvtColor(self.frame_image[i_beg:i_end, j_beg:j_end], cv2.COLOR_BGR2RGB))
#blob_img = Image.fromarray( cv2.cvtColor(self.frame_image[i_beg:i_end, j_beg:j_end], cv2.COLOR_BGR2GRAY))
self.ROIlist.append(ROI(ROIimage=blob_img,edge=np.squeeze(ctr,axis=1),
area=area,bbox=bbox,ellbox=ellbox,
i_beg=i_beg,i_end=i_end,j_beg=j_beg,j_end=j_end,
category=category))
if display_ROIs:
self.show_ROIs_frame()
def segment_frameSIMPLE(self, min_val=minThreshold, max_val=maxThreshold,min_area=minArea,
display_ROIs=False,display_blobs=False,display_blobsCV=False,use_binary=True,
fig_numROI=102,fig_numBLOB=103,category=None):
print('starting segment_frameSIMPLE: len(self.ROIlist)=',len(self.ROIlist))
self.ROIlist=[]
# set up blob detection parameters
params = cv2.SimpleBlobDetector_Params()
params.filterByInertia = False
params.filterByConvexity = False
params.filterByCircularity = False
params.filterByColor = False
params.minThreshold = min_val
params.maxThreshold = max_val
params.minArea = min_area # only detect blobs that have at least 10 pixels
detector = cv2.SimpleBlobDetector_create(params)
# grab blobs from binary image
# note: a blob is represented as a center point and a radius;
# an ROI is the minimal rectangular that encloses a blob
# list of keypoints in image corresponding to blobs
if use_binary:
print('segmenting using binary image...')
self.blob_keypoints = detector.detect(self.binary_image)
else:
print('segmenting using original image...')
self.blob_keypoints = detector.detect(self.frame_image)
print('found %d blobs: ' % len(self.blob_keypoints))
#print(dir(self.blob_keypoints[0]))
# dimensions of image in pixels
nx=len(self.frame_image)
ny=len(self.frame_image[0])
# Parse ROIs from blobs
for kp in self.blob_keypoints:
#print(kp.pt[0],kp.pt[1],kp.size)
kp_i = int(kp.pt[1])
kp_j = int(kp.pt[0])
kp_sz = int(kp.size) # kp.size is the diameter of the widest part of the blob
# get boundaries on blob subimage
if (kp_i - kp_sz) > 0:
i_beg = kp_i - kp_sz
else:
i_beg = 0
if kp_j - kp_sz > 0:
j_beg = kp_j - kp_sz
else:
j_beg = 0
if kp_i + kp_sz < len(self.frame_image):
i_end = kp_i + kp_sz
else:
i_end = len(img)
if kp_j + kp_sz < len(self.frame_image[0]):
j_end = kp_j + kp_sz
else:
j_end = len(self.frame_image[0])
# get blob subimage
blob_img = Image.fromarray( cv2.cvtColor(self.frame_image[i_beg:i_end, j_beg:j_end], cv2.COLOR_BGR2RGB))
#blob_img = Image.fromarray( cv2.cvtColor(self.frame_image[i_beg:i_end, j_beg:j_end], cv2.COLOR_BGR2GRAY))
self.ROIlist.append(ROI(ROIimage=blob_img,keypoints=kp,i_beg=i_beg,i_end=i_end,j_beg=j_beg,j_end=j_end,
category=category))
# write subimage to new file
#cv2.imwrite(output_name, blob_img)
if display_ROIs:
roi_fig=plt.figure(fig_numROI,facecolor=tuple([i/255 for i in bg_color]))
roi_fig.clf()
if use_binary:
plt.imshow(self.binary_image, cmap='gray')
else:
plt.imshow(self.frame_image, cmap='gray')
for roi in self.ROIlist:
rect = Rectangle((roi.j_beg,roi.i_beg),roi.j_end-roi.j_beg,roi.i_end-roi.i_beg,
linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
plt.gca().add_patch(rect)
plt.tight_layout(pad=plt_pad)
if display_blobs:
blob_fig=plt.figure(fig_numBLOB,facecolor=tuple([i/255 for i in bg_color]))
blob_fig.clf()
if use_binary:
plt.imshow(self.binary_image, cmap='gray')
else:
plt.imshow(self.frame_image, cmap='gray')
for roi in self.ROIlist:
circ = Circle((roi.keypoints.pt[0],roi.keypoints.pt[1]),roi.keypoints.size,
linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
plt.gca().add_patch(circ)
plt.tight_layout(pad=plt_pad)
# Plotting segmented circles is proving problematic;
# This flag turns it on and off for comparison with the plotting in pyplot above
# Note that kp.size--> radius (as above) corresponds more closely with the ROIs
if display_blobsCV:
if True: #use_binary:
frame_blobs=cv2.drawKeypoints(self.binary_image, self.blob_keypoints, np.array([]),
(0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
else: # This does not display, for reasons unclear to me...
frame_blobs=cv2.drawKeypoints(self.frame_image, self.blob_keypoints, np.array([]),
(0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('blobs',frame_blobs)
#print('frame_blobs.shape = ',frame_blobs.shape)
cv2.waitKey(1)
print('exiting segment_frameSIMPLE: len(self.ROIlist)=',len(self.ROIlist))
class ROI():
"""A class to contain and analyze ROIs extracted from ZooCAM frames
"""
def __init__(self,ROIfile=None,ROIimage=None,counter=None,category=None,label=None,code=None,bg_color=None,fill_color=None,
keypoints=None,i_beg=None,i_end=None,j_beg=None,j_end=None,edge=None,area=None,bbox=None,ellbox=None):
self.ROIfile=ROIfile
if ROIimage is not None:
self.ROIimage=ROIimage
elif self.ROIfile is not None:
try:
self.read_ROI()
except:
self.ROIimage=None
print('ERROR: Failed to load ROI file %s' % self.ROIfile)
self.counter=counter
if category is not None: # Initialize category, label, color, code
self.category=category
else:
self.category=default_category
self.prev_category=category
if label is not None:
self.label=label
else:
self.label=labels[self.category]
if code is not None:
self.code=code
else:
self.code=codes[self.category]
if fill_color is not None:
self.fill_color=fill_color
else:
self.fill_color=colors[self.category]
self.bg_color=bg_color
self.image_with_border=None
self.image_with_edge=None
self.keypoints=keypoints
self.edge=edge
self.area=area
self.bbox=bbox
self.ellbox=ellbox
self.i_beg=i_beg
self.i_end=i_end
self.j_beg=j_beg
self.j_end=j_end
self.group=None
def add_border(self,padsize=[90,90],bg_color=(16,16,16)):
# calculate the border sizes to make the padded image
ow,oh=self.ROIimage.size
#print('self.ROIimage.size=',self.ROIimage.size)
nw=padsize[0]
nh=padsize[1]
delta_w = nw-ow
delta_h = nh-oh
ltrb_border=(delta_w//2,delta_h//2,delta_w-(delta_w//2),delta_h-(delta_h//2))
# create the padded image
self.image_with_border=ImageOps.expand(self.ROIimage,border=ltrb_border,fill=bg_color)
def add_edge(self,edge=5):#,category=None,reset_previous=None):
self.edge=edge
self.image_with_edge=ImageOps.expand(self.image_with_border,border=self.edge,fill=self.fill_color)
def show_image(self,axi):
axi.imshow(self.image_with_edge)
axi.axis('off') # turn off axes rendering
def read_ROI(self,ROIfile=None):
if ROIfile is not None:
self.ROIfile=ROIfile
if self.ROIfile is not None:
try:
self.ROIimage=Image.open(self.ROIfile)
except:
self.ROIimage=None
print('ERROR: Failed to load ROI file %s' % self.ROIfile)
def save_snapshot(analysis,compression='gzip',directory=None,prefix="snapshot",timestamp=True,frame_num=None):
# Save a snapshot of the submitted analysis object
if timestamp:
prefix+='_'+str(time.time())
if directory is not None:
prefix=os.path.join(directory,prefix)
if compression is None:
savefile=prefix+'.pkl'
outfile = open(savefile,'wb')
elif compression == 'gzip':
savefile=prefix+'.pklz'
outfile = gzip.open(savefile,'wb')
else:
print('Warning: unsupported file format in save_snapshot(). Using uncompressed format...')
savefile=prefix+'.pkl'
outfile = open(savefile,'wb')
if frame_num is None: # save entire analysis object
pickle.dump(analysis,outfile)
else: # save only specified frame
anal=analysis # create a copy
anal.Frames=[analysis.Frames[frame_num]]
anal.Frame_num=0
pickle.dump(anal,outfile)
outfile.close()
print('Done saving ',savefile,'...')
def load_snapshot(loadfile,compression='gzip'):
# Load a snapshot of an analysis object from the specified file
print('Unpickling analysis snapshot...')
if compression is None:
infile = open(loadfile,'rb')
elif compression == 'gzip':
infile = gzip.open(loadfile,'rb')
else:
print('Warning: unsupported file format in load_snapshot(). Using uncompressed format...')
infile = open(loadfile,'rb')
#infile = open(loadfile,'r')
analysis = pickle.load(infile)
infile.close()
print('Done loading ',loadfile,'...')
return analysis
def resume_analysis(loadfile=None,compression='gzip',old_analysis=None):
global load_dir
# If an old analysis is passed, delete it to avoid duplication
if old_analysis != None:
print('Purging and deleting old analysis...')
#try:
# old_analysis.close_analysis_window()
#except:
# pass
old_analysis.purge()
del old_analysis
# Resume an analysis in progress
if loadfile==None:
loadfile = askopenfilename(initialdir = load_dir,title = "Open snapshot or archive:",
filetypes = (("pklz files","*.pklz"),("all files","*.*")))
if loadfile==None:
print('User canceled load...')
return
# Record the load directory to be reused next reload
load_dir=os.path.split(loadfile)[0]
print('Loading snapshot/archive ',loadfile)
analysis=load_snapshot(loadfile)
analysis.create_controlsGUI()
try:
analysis.close_analysis_window()
except:
pass
analysis.open_analysis_window()
analysis.Frames[analysis.Frame_num].show_all_frames()
analysis.load_ROIset(Frame_num=analysis.Frame_num)
#analysis.load_ROIset(Frame_num=0)
# For backwards compatibility, check for analyst, comment and log fields.
# Create them if they're not already present
if not hasattr(analysis,'analyst'):
analysis.analyst='nobody'
analysis.analyst=simpledialog.askstring(title = "Analyst", prompt = "Specify analyst:",
initialvalue='')
print('\nComments:')
if not hasattr(analysis,'comments'):
print('comment field not found; creating one...')
cmt_str='{}; {}; Initiated comment field in existing Analysis object'.format(time.time(),analysis.analyst)
analysis.comments=[cmt_str]
try:
for c in analysis.comments:
print(c)
except:
print('Error in printing comments...')
print('\nLog:')
if not hasattr(analysis,'log'):
print('log field not found; creating one...')
log_str='{}; {}; Initiated log field in existing Analysis object with {} Frames'.format(time.time(),analysis.analyst,len(analysis.Frames))
analysis.log=[log_str]
try:
for l in analysis.log:
print(l)
except:
print('Error in printing log...')
return analysis
def parse_input_file(input_file,minThreshold=minThreshold,maxThreshold=maxThreshold):
"""Parse an input file to generate the specified FrameSetArray
"""
analyst = input('\nEnter analyst name/initials: ')
print('Logs will cite analyst: ',analyst)