-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcortex_segmentor_v1_1.py
executable file
·1228 lines (886 loc) · 48.2 KB
/
cortex_segmentor_v1_1.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
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
#!/opt/local/bin/python
__author__ = "Kai Dierkes"
__date__ = "2017"
__maintainer__ = "Andrew G. Clark"
__email__ = "andrew.clark@curie.fr"
"""
This GUI-based program is designed to read images, perform segmentations
of an enriched region in the cell periphery and generate linescans normal
to the segmentation contour.
Images and segmentation files can be loaded, and segmentation parameters
can be saved. Linescans can also be generated and are automatically saved
in the same directory as the image.
Classes:
Experiment
App
"""
import os
import math
import numpy as np
from scipy.optimize import leastsq
from tkinter import *
from tkinter.filedialog import *
master = Tk()
from PIL import Image, ImageTk
import matplotlib.pyplot as plt
def fitfunc(p,phi):
"""Fourier series to fit the segmentation contour to the selected fitpoints."""
value = p[0]
for i in range(1,len(p),2):
k = (i+1)/2
value += p[i]*np.sin(k*phi)+p[i+1]*np.cos(k*phi)
return value
def errfunc(p,phi,xdat):
"""Error function used for fitting"""
return fitfunc(p,phi)-xdat
class Experiment:
"""A class to contain the image and segmentation data
for the current image.
"""
def __init__(self, file):
"""Initializes the experiment class
Args:
file (str): file path
"""
self.filename = file
self.segmentation = ""
self.directory = os.path.dirname(file)
self.cellimage = Image.open(self.filename)
self.cellimagematrix = self.cellimage.load()
self.size_x = self.cellimage.size[0]
self.size_y = self.cellimage.size[1]
self.no_pixels = self.size_x*self.size_y
self.no_stacks = self.countstacks(self.cellimage)
self.cellimage.seek(0)
self.current_layer = 0
self.fit_x = []
self.fit_y = []
self.fit_points = []
self.outline_pixel_list = []
self.linescan_pars = []
for i in range(0,self.no_stacks,1):
self.fit_x.append([False,[]])
self.fit_y.append([False,[]])
self.fit_points.append([])
self.outline_pixel_list.append([])
self.linescan_pars.append([0,360,50,50])
def countstacks(self,image):
"""Counts the number of slices in the image.
Args:
image (PIL Image): image to be counted
Returns:
stack_counter (int): number of slices in the image stack
"""
stack_counter = 0
eof_indicator = 0
while eof_indicator!=1:
try:
image.seek(stack_counter)
stack_counter += 1
except EOFError:
eof_indicator = 1
return stack_counter
def change_fit_points(self,xindex,yindex,fit_toggle,radius=2):
"""Adds or removes fit points from the image
Args:
xindex (int): x-coordinate of location of fit point addition/removal
yindex (int): y-coordinate of location of fit point addition/removal
fit_toggle (bool): toggle to add (==1) or remove (==0) fit point
"""
for i in range(-radius,radius+1,1):
for j in range(-radius,radius+1,1):
if 0<=xindex+i<self.size_x and 0<=yindex+j<self.size_y:
if fit_toggle==1:
if [xindex+i,yindex+j] not in self.fit_points[self.current_layer]:
self.fit_points[self.current_layer].append([xindex+i,yindex+j])
else:
while [xindex+i,yindex+j] in self.fit_points[self.current_layer]:
self.fit_points[self.current_layer].remove([xindex+i,yindex+j])
def seek(self,layer):
"""Changes current layer (slice) of the image
Args:
layer (int): desired layer to make current
"""
if layer>=0 and layer<self.no_stacks:
self.cellimage.seek(layer)
self.cellimagematrix = self.cellimage.load()
self.current_layer = layer
else:
return 0
class App:
"""A class for the GUI"""
def __init__(self,root):
"""Initializes the GUI App Class with windows, buttons, etc.
Args:
root (Tkinter.Tk instance): Tk instance for drawing GUI
"""
self.frame = Frame(root, width=60, height=512,borderwidth=10)
self.frame.grid(row=0,column=0,padx=10,pady=10,ipadx=10,ipady=10)
self.openbutton = Button(self.frame, text="Open file",command = self.openfile,width=15)
self.openbutton.grid(row=0,column=0)
self.loadbutton = Button(self.frame, text="Load", command=self.load,width=15)
self.loadbutton.grid(row=0,column=1)
self.seekplus = Button(self.frame, text="Next layer", command = lambda : self.seek(self.cell.current_layer+1),width=15)
self.seekplus.grid(row=10,column=1)
self.seekminus = Button(self.frame, text="Previous layer",command = lambda : self.seek(self.cell.current_layer-1),width=15)
self.seekminus.grid(row=10,column=0)
self.image_toggle = IntVar()
self.fit_points_toggle = IntVar()
self.segmentation_toggle = IntVar()
self.linescan_toggle = IntVar()
self.linescanimage_toggle = IntVar()
self.toggles = [[self.image_toggle,"Image"],
[self.fit_points_toggle,"Fit points"],
[self.segmentation_toggle,"Segmentation"],
[self.linescan_toggle,"Linescan - Points"],
[self.linescanimage_toggle,"Linescan - Image"]]
for i in range(0,len(self.toggles),1):
self.toggles[i].append(Checkbutton(self.frame, variable = self.toggles[i][0],command = self.draw))
if i!=4:
self.toggles[i][2].select()
else:
self.toggles[i][2].deselect()
self.toggles[i][2].grid(row=i+20,column=1)
self.toggles[i].append(Label(self.frame,text=self.toggles[i][1]))
self.toggles[i][3].grid(row=i+20,column=0)
self.phistart = Entry(self.frame,bg="white",width=10)
self.phistart.insert(0,"0")
self.phistart.grid(row=50,column=1)
self.phiend = Entry(self.frame,bg="white",width=10)
self.phiend.insert(0,"360")
self.phiend.grid(row=51,column=1)
self.outerlength = Entry(self.frame,bg="white",width=10)
self.outerlength.insert(0,"50")
self.outerlength.grid(row=52,column=1)
self.innerlength = Entry(self.frame,bg="white",width=10)
self.innerlength.insert(0,"50")
self.innerlength.grid(row=53,column=1)
self.resetradius = Entry(self.frame,bg="white",width=10)
self.resetradius .insert(0,"2")
self.resetradius.grid(row=65,column=0)
self.resetlabel = Label(self.frame,text="Fitpoint radius")
self.resetlabel.grid(row=65,column=1)
self.line_scan_labels = [["Startangle"],["Endangle"],["Outer length"],["Inner length"]]
for i in range(0,len(self.line_scan_labels),1):
self.line_scan_labels[i].append(Label(self.frame,text=self.line_scan_labels[i][0]))
self.line_scan_labels[i][1].grid(row=50+i,column=0)
self.copylinescanbutton = Button(self.frame, text="Copy to all",command = self.copy_linescan_parameters,width=6)
self.copylinescanbutton.grid(row = 50,column=2)
self.linescanbutton = Button(self.frame, text="Linescan",command = self.linescan,width=15)
self.linescanbutton.grid(row = 54)
self.linescanshowbutton = Button(self.frame, text="Show endpoints",command = self.linescan_show_endpoints,width=15)
self.linescanshowbutton.grid(row = 54,column=1)
self.scale_fit_points = Scale(self.frame, from_=0, to=0.1, orient=HORIZONTAL,resolution=0.0005,troughcolor="white",showvalue=1,borderwidth=2)
self.scale_fit_points.set(0.075)
self.scale_fit_points.grid(row=30,column=1)
self.fixpointsbutton = Button(self.frame, text="Choose fit points",command =self.choose_fit_points,width=15)
self.fixpointsbutton.grid(row=30,column=0)
self.segmentbutton = Button(self.frame, text="Segment",command = lambda: self.segment(-1),width=15)
self.segmentbutton.grid(row=40)
#self.segmentmodeslabel = Label(self.frame,text = "No. of modes")
#self.segmentmodeslabel.grid(row=40,column = 0)
self.segmentmodes = Entry(self.frame, bg="white",width=10)
self.segmentmodes.insert(0,"10")
self.segmentmodes.grid(row=40,column=1)
# self.hr1 = Canvas(self.frame,height=3,width=200,bd=-2)
# self.hr1.grid(row=43,columnspan=2,pady =2)
self.copysegmenationentry = Entry(self.frame,bg="white",width=10)
self.copysegmenationentry.insert(0,"10")
self.copysegmenationentry.grid(row=45,column=1)
self.segmentcopybutton = Button(self.frame, text="Copy segmentation",command = self.copysegmentation,width=15)
self.segmentcopybutton.grid(row=45,column=0)
self.segmentcopychoicewhat = StringVar(root)
self.segmentcopychoicewhat.set("Entry")
self.segmentcopychoice = OptionMenu(self.frame,self.segmentcopychoicewhat,"Entry", "Odd+1->Even","Even-1->Odd", "Ch1->Ch2/3", "Ch2->Ch1/3", "Ch3->Ch1/2")
self.segmentcopychoice.grid(row=45,column=2)
self.analyzewhatentry = Entry(self.frame,bg="white",width=10)
#self.analyzewhatentry.insert(0,"")
self.analyzewhatentry.grid(row=26,column=1)
self.analyzewhatlabel = Label(self.frame, text="Mode:",padx=5,pady = 5)
self.analyzewhatlabel.grid(row=26,column=0)
self.analyzewhat = StringVar(root)
self.analyzewhat.set("Current")
self.analyzewhatchoice = OptionMenu(self.frame, self.analyzewhat, "Current","Entry","All","Even","Odd",
"Every third (from 0)", "Every third (from 1)", "Every third (from 2)",
"All from entry","Every second from entry")
self.analyzewhatchoice.grid(row=26,column=2)
# self.hr1 = Canvas(self.frame,height=3,width=200,bd=-2)
# self.hr1.grid(row=25,columnspan=2,pady =2)
self.resetfitpointsbutton = Button(self.frame, text="Reset Fitpoints", relief="groove",command=self.resetfitpoints,width=15)
self.resetfitpointsbutton.grid(row=65,column=2)
self.button = Button(self.frame, text="Quit", relief="groove",command=self.frame.quit,width=15)
self.button.grid(row=70,column=0)
self.savebutton = Button(self.frame, text="Save", command=self.save,width=15)
self.savebutton.grid(row=70,column=1)
# for i in range(10,80,10):
# self.hr1 = Canvas(self.frame,height=3,width=200,bd=-2)
# self.hr1.grid(row=i-1,columnspan=2,pady =2)
self.cell = Experiment("./startupscreen.tif")
self.display = Canvas(root, width=self.cell.size_x, height=self.cell.size_y, background="black",bd=-1)
self.display.bind("<Button-1>", self.add_fit_points)
self.display.bind("<Button-2>", self.remove_fit_points)
self.display.grid(row=0,column=2)
self.image_canvas = ImageTk.PhotoImage(self.cell.cellimage)
# self.display.create_image((self.cell.size_x/2,self.cell.size_y/2),image=self.image_canvas)
self.display.create_image((0,0),image=self.image_canvas,anchor="nw")
self.frame.bind_all("<Down>", self.down)
self.frame.bind_all("<Left>", self.left)
self.frame.bind_all("<Right>",self.right)
def resetfitpoints(self):
"""Resets (deletes) all current fit points"""
self.cell.fit_points[self.cell.current_layer] = []
self.draw()
self.display.update_idletasks()
def down(self,event):
"""Segments the current cell layer.
Args:
event (Tk keystroke event)
"""
self.segment(self.cell.current_layer)
def left(self,event):
"""Seeks one slice backward
Args:
event (Tk keystroke event)
"""
self.seek(self.cell.current_layer-1)
def right(self,event):
"""Seeks one slice forward
Args:
event (Tk keystroke event)
"""
self.seek(self.cell.current_layer+1)
def copy_linescan_parameters(self):
"""Copies linescan parameters from one frame to another"""
outer = float(self.outerlength.get())
inner = float(self.innerlength.get())
phistart = float(self.phistart.get())
phiend = float(self.phiend.get())
for layer in range(0,self.cell.no_stacks):
self.cell.linescan_pars[layer] = [phistart, phiend, outer, inner]
def seek(self,layer):
"""Seeks image and segmentation/linescan data to a specific layer (slice)
Args:
layer (int): desired slice
"""
self.store_linescan_entries()
self.cell.seek(layer)
self.load_linescan_entries()
self.draw()
def generate_framelist(self):
"""Makes a list of frames to process based on users' selection in the GUI
Returns:
layers (list): list of layers to be processed
"""
analyze_choice = self.analyzewhat.get()
analyze_list = self.analyzewhatentry.get()
if "'" in analyze_list:
liste = analyze_list.split(",")
else:
liste = analyze_list.split()
if len(liste)==0:
liste = [0]
if analyze_choice=="Current":
layers = [self.cell.current_layer]
elif analyze_choice=="Entry":
layers = []
for entry in liste:
layers.append(int(entry))
elif analyze_choice=="All":
layers = range(0,self.cell.no_stacks,1)
elif analyze_choice=="Even":
layers = range(0,self.cell.no_stacks,2)
elif analyze_choice=="Odd":
layers = range(1,self.cell.no_stacks,2)
elif analyze_choice=="Every third (from 0)":
layers = range(0,self.cell.no_stacks,3)
elif analyze_choice=="Every third (from 1)":
layers = range(1,self.cell.no_stacks,3)
elif analyze_choice=="Every third (from 2)":
layers = range(2,self.cell.no_stacks,3)
elif analyze_choice=="All from entry":
if 0<=int(liste[0]) and int(liste[0])<self.cell.no_stacks:
layers = range(int(liste[0]),self.cell.no_stacks,1)
else:
layers = [0]
elif analyze_choice=="Every second from entry":
if 0<=int(liste[0]) and int(liste[0])<self.cell.no_stacks:
layers = range(int(liste[0]),self.cell.no_stacks,2)
else:
layers = [0]
print("Layers to work on:",layers)
return layers
def copysegmentation(self):
"""Copies segmentation and linescan parameters from one frame to another"""
#prepares lists for odds/evens and ch1-3
odds = range(1,self.cell.no_stacks,2)
evens = range(0,self.cell.no_stacks,2)
ch1 = range(0,self.cell.no_stacks,3)
ch2 = range(1,self.cell.no_stacks,3)
ch3 = range(2,self.cell.no_stacks,3)
#gets copy choice mode and determines list of copies
mode = self.segmentcopychoicewhat.get()
listofcopies = []
if mode=="Entry":
noframe = int(self.copysegmenationentry.get())
listofcopies = [[noframe,self.cell.current_layer]]
elif mode=="Odd+1->Even":
for i in range(0,len(odds),1):
listofcopies.append([odds[i],evens[i]])
elif mode=="Even-1->Odd":
for i in range(0,len(odds),1):
listofcopies.append([evens[i],odds[i]])
elif mode=="Ch1->Ch2/3":
for i in range(0,len(ch1),1):
listofcopies.append([ch1[i],ch2[i]])
listofcopies.append([ch1[i],ch3[i]])
elif mode=="Ch2->Ch1/3":
for i in range(0,len(ch1),1):
listofcopies.append([ch2[i],ch1[i]])
listofcopies.append([ch2[i],ch3[i]])
elif mode=="Ch3->Ch1/2":
for i in range(0,len(ch1),1):
listofcopies.append([ch3[i],ch1[i]])
listofcopies.append([ch3[i],ch2[i]])
else:
raise ValueError("Not a valid copy mode chioce!")
self.store_linescan_entries()
#copies fit points, segmentation and linescan parameters
for movement in listofcopies:
toframe = movement[1]
noframe = movement[0]
self.cell.fit_x[toframe][0]=self.cell.fit_x[noframe][0]
self.cell.fit_x[toframe][1] = []
for entry in self.cell.fit_x[noframe][1]:
self.cell.fit_x[toframe][1].append(entry)
self.cell.fit_y[toframe][0] = self.cell.fit_y[noframe][0]
self.cell.fit_y[toframe][1] = []
for entry in self.cell.fit_y[noframe][1]:
self.cell.fit_y[toframe][1].append(entry)
self.cell.fit_points[toframe] = []
for entry in self.cell.fit_points[noframe]:
self.cell.fit_points[toframe].append(entry)
self.cell.linescan_pars[toframe] = []
for entry in self.cell.linescan_pars[noframe]:
self.cell.linescan_pars[toframe].append(entry)
self.load_linescan_entries()
self.draw()
def save(self):
"""Saves current fit points, segmentation and linescan parameters for the whole image stack"""
self.store_linescan_entries()
current = os.getcwd()
os.chdir(self.cell.directory)
output = asksaveasfile(mode='w',filetypes=[("csd", "*.csd")],initialfile="%s"%self.cell.segmentation)
#goes through slices, collects and writes the data
for i in range(0,self.cell.no_stacks,1):
if self.cell.fit_x[i][0]:
output.write("1\n")
else:
output.write("0\n")
for k in range(0,len(self.cell.fit_x[i][1]),1):
output.write("%e "%self.cell.fit_x[i][1][k])
output.write("\n")
for k in range(0,len(self.cell.fit_x[i][1]),1):
output.write("%e "%self.cell.fit_y[i][1][k])
output.write("\n")
for k in range(0,4,1):
output.write("%i "%self.cell.linescan_pars[i][k])
output.write("\n")
for point in self.cell.fit_points[i]:
output.write("%i %i "%(point[0],point[1]))
output.write("\n")
output.close()
os.chdir(current)
def load(self):
"""Loads a new segmentation file"""
data_file = askopenfilename(filetypes=[("csd", "*.csd")],initialfile="%s"%self.cell.segmentation)
try:
ifile = open(data_file,"r")
self.cell.segmentation = data_file.split("/")[-1]
except:
return
dat_temp = ifile.readlines()
ifile.close()
#adds the segmentation data to the currently open image
for i in range(0,self.cell.no_stacks,1):
if int(dat_temp[i*5+0].split()[0])==1:
self.cell.fit_x[i][0] = True
self.cell.fit_y[i][0] = True
else:
self.cell.fit_x[i][0] = False
self.cell.fit_y[i][0] = False
self.cell.fit_x[i][1] = []
for fourier in dat_temp[i*5+1].split():
self.cell.fit_x[i][1].append(float(fourier))
self.cell.fit_y[i][1] = []
for fourier in dat_temp[i*5+2].split():
self.cell.fit_y[i][1].append(float(fourier))
linescan_pars = dat_temp[i*5+3].split()
for j in range(0,4,1):
self.cell.linescan_pars[i][j]=int(linescan_pars[j])
self.cell.fit_points[i] = []
fit_points_list = dat_temp[i*5+4].split()
for j in range(0,len(fit_points_list),2):
self.cell.fit_points[i].append([int(fit_points_list[j]),int(fit_points_list[j+1])])
self.cell.outline_pixel_list[i] = []
self.load_linescan_entries()
self.draw()
def load_linescan_entries(self):
"""Loads the linescan parameters to the GUI"""
outer = self.cell.linescan_pars[self.cell.current_layer][2]
inner = self.cell.linescan_pars[self.cell.current_layer][3]
phistart = self.cell.linescan_pars[self.cell.current_layer][0]
phiend = self.cell.linescan_pars[self.cell.current_layer][1]
self.outerlength.delete(0,END)
self.innerlength.delete(0,END)
self.phistart.delete(0,END)
self.phiend.delete(0,END)
self.outerlength.insert(0,"%i"%outer)
self.innerlength.insert(0,"%i"%inner)
self.phistart.insert(0,"%i"%phistart)
self.phiend.insert(0,"%i"%phiend)
self.draw()
def store_linescan_entries(self):
"""Gets the linescan parameters from the GUI"""
outer = float(self.outerlength.get())
inner = float(self.innerlength.get())
phistart = float(self.phistart.get())
phiend = float(self.phiend.get())
self.cell.linescan_pars[self.cell.current_layer] = [phistart,phiend,outer,inner]
def choose_fit_points(self):
"""Automatically selects fit points for fitting the segmentation contour
based on high thresholding and sequential deletion/refinement of the fit points.
The quality of the automatic segmentation can be adjusted to fit needs for specific
images here by changing the 'algorithm' list. This list determines what points should
be removed at each iteration. Each iteration is a sublist of three values indicating:
[direction (-1 = outside the current fit, 1 = inside the current fit),
number of modes used for the fit after this iteration,
the minimum distance away from the segmentation contour for fit point deletion]
"""
layers = self.generate_framelist()
#loops through the layers to be processed and performs the automated fit point selection
for layer in layers:
self.cell.seek(layer)
current_modes = self.segmentmodes.get()
self.segmentmodes.delete(0,END)
self.segmentmodes.insert(0,"4")
self.cell.fit_points[self.cell.current_layer] = []
#get a sorted list of pixel intensities
intensities = []
for i in range(0,self.cell.size_x,1):
for k in range(0,self.cell.size_y,1):
intensities.append([np.sum(self.cell.cellimagematrix[i,k]),i,k])
intensities.sort()
#initially chooses fit points based on simple threshold (factor selected in GUI)
for i in range(1,int(self.scale_fit_points.get()*self.cell.no_pixels),1):
self.cell.fit_points[self.cell.current_layer].append([intensities[-i][1],intensities[-i][2]])
self.draw()
self.display.update_idletasks()
#sequentially and progressively deletes fit points that are far away from the fit
algorithm = [[-1,4,25],
[1,4,25],
[-1,4,10],
[1,4,15],
[-1,8,10],
[1,8,15],
[-1,8,3],
[-1,10,2]]
#other examples of algorithms to use for automated fit point selection
# algorithm = [[-1,4,5]
# [1,4,25],
# [-1,4,25],
# [-1,4,10],
# [1,4,10],
# [1,8,10],
# [-1,8,10],
# [1,8,4],
# [1,8,2],
# [-1,8,4],
# [1,10,4]]
# algorithm = [[ 1,4,25],
# [1,4,25],
# [-1,4,45],
# [1,4,15],
# [1,4,10],
# [-1,6,15],
# [1,6,10],
# [1,6,8],
# [-1,8,15],
# [-1,8,15]]
# algorithm = [[1,8,10],
# [1,8,7],
# [-1,8,7],
# [-1,10,12],
# [1,10,8],
# [-1,10,10],
# [1,10,10],
# [-1,11,9]]
#iterates through the algorithm list and removes fit points
for iteration in algorithm:
side = iteration[0]
no_modes = iteration[1]
distance = iteration[2]
self.segmentmodes.delete(0,END)
self.segmentmodes.insert(0,"%s"%no_modes)
self.segment(self.cell.current_layer)
delete_list = []
for entry in self.cell.fit_points[self.cell.current_layer]:
PHI = math.atan2(float(256-entry[1]),float(256-entry[0]))
i = int(round(fitfunc(self.cell.fit_x[self.cell.current_layer][1],PHI)))
j = int(round(fitfunc(self.cell.fit_y[self.cell.current_layer][1],PHI)))
if math.sqrt((i-entry[0])**2+(j-entry[1])**2)>distance and 0<side*(math.sqrt((256-i)**2+(256-j)**2)-math.sqrt((256-entry[0])**2+(256-entry[1])**2)):
delete_list.append(entry)
for entry in delete_list:
self.cell.fit_points[self.cell.current_layer].remove(entry)
#re-segments and puts modes back to what it was before
self.segment(self.cell.current_layer)
self.segmentmodes.delete(0,END)
self.segmentmodes.insert(0,"%s"%current_modes)
self.segment(self.cell.current_layer)
self.draw()
self.display.update_idletasks()
def draw(self):
"""Draws the cell image, fit points and segmentation (if toggled in GUI)"""
allitems = self.display.find_all()
for item in allitems:
self.display.delete(item)
#displays image
if self.image_toggle.get() == 1:
self.image_canvas = ImageTk.PhotoImage(self.cell.cellimage)
# self.display.create_image((self.cell.size_x/2,self.cell.size_y/2),image=self.image_canvas)
self.display.create_image((0,0),image=self.image_canvas,anchor="nw")
#displays fit points
if self.fit_points_toggle.get() == 1:
for point in self.cell.fit_points[self.cell.current_layer]:
self.display.create_rectangle((point[0],point[1],point[0],point[1]),width=0,fill="green")
#displays segmentation
if self.segmentation_toggle.get() == 1:
if self.cell.fit_x[self.cell.current_layer][0]:
cell_outline = []
for PHI in np.arange(0,2*np.pi,0.01):
i = int(round(fitfunc(self.cell.fit_x[self.cell.current_layer][1],PHI)))
j = int(round(fitfunc(self.cell.fit_y[self.cell.current_layer][1],PHI)))
cell_outline.append([i,j])
self.display.create_line(cell_outline,fill="red",width="2.0")
if self.linescan_toggle.get() == 1:
draw_list = []
for entry in self.cell.outline_pixel_list[self.cell.current_layer]:
draw_list.append([entry[0],entry[1]])
try:
self.display.create_line(draw_list,fill="white",width="4.0")
except:
pass
self.display.create_text((20,20),text = "Layer %i/%i"%(self.cell.current_layer+1,self.cell.no_stacks),fill="white",anchor="w")
self.display.create_text((self.cell.size_x/2,self.cell.size_y-10),text = "%s"%self.cell.filename.split("/")[-1],fill="white")
# self.display.create_text((256,480),text = "%s"%self.cell.segmentation,fill="white")
#self.display()
self.display.update_idletasks()
def openfile(self):
"""Opens image file"""
image_file = askopenfilename(filetypes=[("tif", "*.tif")], initialdir=self.cell.directory)
self.cell = Experiment(image_file)
self.image_canvas = ImageTk.PhotoImage(self.cell.cellimage)
self.display.config(width=self.cell.size_x, height=self.cell.size_y)
outer = self.cell.linescan_pars[self.cell.current_layer][2]
inner = self.cell.linescan_pars[self.cell.current_layer][3]
phistart = self.cell.linescan_pars[self.cell.current_layer][0]
phiend = self.cell.linescan_pars[self.cell.current_layer][1]
self.outerlength.delete(0,END)
self.innerlength.delete(0,END)
self.phistart.delete(0,END)
self.phiend.delete(0,END)
self.outerlength.insert(0,"%i"%outer)
self.innerlength.insert(0,"%i"%inner)
self.phistart.insert(0,"%i"%phistart)
self.phiend.insert(0,"%i"%phiend)
self.draw()
def add_fit_points(self,event):
"""Adds fit point to current mouse location
Args:
event (Tk mouse click event)
"""
radius = int(self.resetradius.get())
self.cell.change_fit_points(event.x,event.y,1,radius)
self.draw()
#print('clicked to add at', event.x, event.y)
def remove_fit_points(self,event):
"""Adds fit point to current mouse location
Args:
event (Tk mouse click event)
"""
radius = int(self.resetradius.get())
self.cell.change_fit_points(event.x,event.y,0,radius)
self.draw()
#print('clicked to remove at', event.x, event.y)
def segment(self,mode):
"""Fits and draws a segmentation contour based on current fit points
Args:
mode (int): instruction to generate frame list for processing multiple frames
if mode==-1, otherwise, mode corresponds to the single frame to be processed
"""
if mode==-1:
scanlayers = self.generate_framelist()
else:
scanlayers = [mode]
for layer in scanlayers:
self.cell.seek(layer)
self.draw()
self.display.update_idletasks()
no_fit_points = len(self.cell.fit_points[self.cell.current_layer])
#gets angles of fit points
phi = [] #zeros(no_fit_points)
x = [] #zeros(no_fit_points)
y = [] #zeros(no_fit_points)
# gets the center position of the fit points and uses that for the fit (instead of the middle of the frame)
center_x = np.mean(np.array(self.cell.fit_points[self.cell.current_layer])[:,0])
center_y = np.mean(np.array(self.cell.fit_points[self.cell.current_layer])[:,1])
for i in range(0,no_fit_points,1):
xi = self.cell.fit_points[self.cell.current_layer][i][0]
yi = self.cell.fit_points[self.cell.current_layer][i][1]
i0 = self.cell.fit_points[self.cell.current_layer][i][0]
j0 = self.cell.fit_points[self.cell.current_layer][i][1]
#for j in range(0,self.cell.cellimage.getpixel((i0,j0)),1):
#fits assuming center in the middle of the frame
# phi.append(math.atan2(float(self.cell.size_y/2-yi),float(self.cell.size_x/2-xi)))
# fits assuming center as the center of the fit points
phi.append(math.atan2(float(center_y - yi), float(center_x - xi)))
x.append(xi)
y.append(yi)
phi = np.reshape(phi,len(phi))
x = np.reshape(x,len(x))
y = np.reshape(y,len(y))
#performs fit
no_modes = 2*(int(self.segmentmodes.get())-1)+1
p0 = np.ones(no_modes)
self.cell.fit_x[self.cell.current_layer][1],success = leastsq(errfunc, p0, args=(phi,x), maxfev = 500)
self.cell.fit_x[self.cell.current_layer][0] = True
self.cell.fit_y[self.cell.current_layer][1],success = leastsq(errfunc, p0, args=(phi,y), maxfev = 500)
self.cell.fit_y[self.cell.current_layer][0] = True
self.draw()
self.display.update_idletasks()
def linescan_show_endpoints(self):
"""Displays the endpoints of the linescan as determined by start and end
angles in the GUI
"""
phistart = float(self.phistart.get())
phiend = float(self.phiend.get())
istart = int(np.floor(fitfunc(self.cell.fit_x[self.cell.current_layer][1],phistart*np.pi/180.0)))
jstart = int(np.floor(fitfunc(self.cell.fit_y[self.cell.current_layer][1],phistart*np.pi/180.0)))
normalistart, normaljstart = self.normal_vector(phistart*np.pi/180.0)
iend = int(np.floor(fitfunc(self.cell.fit_x[self.cell.current_layer][1],phiend*np.pi/180.0)))
jend = int(np.floor(fitfunc(self.cell.fit_y[self.cell.current_layer][1],phiend*np.pi/180.0)))
normaliend, normaljend = self.normal_vector(phiend*np.pi/180.0)
self.draw()
#length = 30
il = float(self.innerlength.get())
ol = float(self.outerlength.get())
#draws line
# length = 30
# self.display.create_line([istart+length*normalistart,jstart+length*normaljstart,istart-length*normalistart,jstart-length*normaljstart],width=2,fill="grey")
# self.display.create_line([iend+length*normaliend,jend+length*normaljend,iend-length*normaliend,jend-length*normaljend],width=2,fill="grey")
self.display.create_line([istart + il * normalistart, jstart + il * normaljstart, istart - ol * normalistart, jstart - ol * normaljstart],
width=2, fill="grey")
self.display.create_line([iend + il * normaliend, jend + il * normaljend, iend - ol * normaliend, jend - ol * normaljend],
width=2, fill="grey")
def normal_vector(self,phi):
"""Calculates a normal vector for the current segmentation for a given angle
Args:
phi (int): angle at which the the normal vector should be calculated
Returns:
normal_i (float): x-coordinate endpoint of normal vector
normal_j (float): y-coordinate endpoint of normal vector
"""
ileft = fitfunc(self.cell.fit_x[self.cell.current_layer][1],phi-0.001)
jleft = fitfunc(self.cell.fit_y[self.cell.current_layer][1],phi-0.001)
iright = fitfunc(self.cell.fit_x[self.cell.current_layer][1],phi+0.001)
jright = fitfunc(self.cell.fit_y[self.cell.current_layer][1],phi+0.001)
normal_j = -1*(ileft-iright)
normal_i = jleft-jright
length_normal = math.sqrt(normal_i**2+normal_j**2)
normal_j /= length_normal
normal_i /= length_normal
return normal_i,normal_j
def linescan(self):
"""Performs linescan analysis on selected slices. Automatically draws
and displays linescan plots and saves the linescans to the image directory
as .dat text files.
Note: if pixels from the linescan fall out of the image, they are given an
intensity value of zero.
"""
self.store_linescan_entries()
#picks image slices to work on
scanlayers = self.generate_framelist()
for layer in scanlayers:
self.cell.seek(layer)
self.load_linescan_entries()