-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intcatch_segmentation_GUI.py
737 lines (610 loc) · 32.6 KB
/
Intcatch_segmentation_GUI.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
#!/usr/bin/env python
# coding: utf-8
# # Tool for segment images pyxelwise
# This tool can be used to create training set of image segment and is used on the Intcatch project to create a training set, for pixelwise water detection
#
# In[1]:
#!/usr/bin/env python
from tkinter import *
from PIL import Image, ImageEnhance
from PIL import ImageTk
from tkinter import filedialog
import cv2
from skimage.segmentation import felzenszwalb, slic, quickshift
from skimage.segmentation import mark_boundaries
from skimage.segmentation import find_boundaries
from scipy.misc import toimage
import numpy as np
import glob
import matplotlib.pyplot as plt
import os
# In[10]:
class SegmentationGUI(Frame):
def __init__(self,master):
self.root=master
#the name of the window
self.root.title("Segmentation Tool")
self.panelA = None
#vector of all images in the same folder
self.images = None
#index of the image opened
self.index_image=None
#image opened
self.image=None
#path to the image
self.path=None
#image original before segmentation
self.image_original=None
self.image_original_2x=None
self.image_original_3x=None
self.image_original_4x=None
self.height_original=None
self.width_original=None
self.mask=None
#trace the actual zoom of the image
self.zoomCycle = 0
self.segment_type_choice=None
self.mask_type_choice=None
self.segments=None
self.slider1=None
self.slider2=None
self.slider3=None
self.panel_height = None
self.panel_width = None
self.intro_window()
#self.main_window()
def intro_window(self):
self.panel0 = Canvas(self.root, width=500, height=500, background="bisque")
self.T = Text(self.root, height=15, width=70)
self.T.insert(END, "SEGMENTATION TOOL \n \n" +
"This is a tool used to label to a pixelwise level an image\n\n" +
"main comands: \n \n" +
"1- Drag and drop right mouse to select areas \n" +
"2- mouse wheel to zoom the image \n" +
"3- left click to drag the image in the window \n" +
"\n \n First step select a resolution in this window in the commands below \n and press continue")
self.panel0.grid(row=1,column=1,columnspan=4,rowspan=4)
self.T.grid(row=1,column=1,columnspan=4,sticky='WE')
self.panel_width=IntVar()
self.panel_width.set(800)
RadioButtonChoiceTheWidthResolution1=Radiobutton(self.root, text="width -> 800", padx = 20, variable=self.panel_width, value=800)
RadioButtonChoiceTheWidthResolution2=Radiobutton(self.root, text="width -> 1200", padx = 20, variable=self.panel_width, value=1200)
RadioButtonChoiceTheWidthResolution3=Radiobutton(self.root, text="width -> 1900", padx = 20, variable=self.panel_width, value=1900)
RadioButtonChoiceTheWidthResolution1.grid(row=2,column=1,columnspan=1,sticky='WE')
RadioButtonChoiceTheWidthResolution2.grid(row=3,column=1,columnspan=1,sticky='WE')
RadioButtonChoiceTheWidthResolution3.grid(row=4,column=1,columnspan=1,sticky='WE')
self.panel_height=IntVar()
self.panel_height.set(600)
RadioButtonChoiceTheHeightResolution1=Radiobutton(self.root, text="height -> 600", padx = 20, variable=self.panel_height, value=600)
RadioButtonChoiceTheHeightResolution2=Radiobutton(self.root, text="height -> 700", padx = 20, variable=self.panel_height, value=700)
RadioButtonChoiceTheHeightResolution3=Radiobutton(self.root, text="height -> 900", padx = 20, variable=self.panel_height, value=900)
RadioButtonChoiceTheHeightResolution1.grid(row=2,column=2,columnspan=1,sticky='WE')
RadioButtonChoiceTheHeightResolution2.grid(row=3,column=2,columnspan=1,sticky='WE')
RadioButtonChoiceTheHeightResolution3.grid(row=4,column=2,columnspan=1,sticky='WE')
btnContinue = Button(self.root, text="Continue", command=self.main_window)
btnContinue.grid(row=3,column=3,columnspan=1)
def main_window(self):
# create a button, then when pressed, will trigger a file chooser
# dialog and allow the user to select an input image; then add the
# button the GUI
btnSelectImage = Button(self.root, text="Select an image", command=self.open_image)
btnReset = Button(self.root, text="Reset", command=self.reset_image)
btnOpenMask = Button(self.root, text="Open a Mask", command=self.open_mask)
#Next image button
btnNext = Button(self.root, text='Next', command=self.Next)
#Previous image button
btnPrevious = Button(self.root, text='Previous', command=self.Previous)
#Zoom + button
btnZoomP = Button(self.root, text='Zoom +', command=self.zoomerPiuBtn)
#Zoom - button
btnZoomM = Button(self.root, text='Zoom -', command=self.zoomerMenoBtn)
self.panelA = Canvas(self.root, width=self.panel_width.get(), height=self.panel_height.get(), background="bisque")
self.segment_type_choice=IntVar()
self.segment_type_choice.set(1)
RadioButton1=Radiobutton(self.root, text="SLIC", padx = 20, variable=self.segment_type_choice, value=1,command=self.radioBtn_refresh)
RadioButton2=Radiobutton(self.root, text="felzenszwalb", padx = 20, variable=self.segment_type_choice, value=2,command=self.radioBtn_refresh)
RadioButton3=Radiobutton(self.root, text="quickshift(slow)", padx = 20, variable=self.segment_type_choice, value=3,command=self.radioBtn_refresh)
self.mask_type_choice=IntVar()
self.mask_type_choice.set(1)
RadioButtonChoiceTheMask1=Radiobutton(self.root, text="Water", padx = 20, variable=self.mask_type_choice, value=1)
RadioButtonChoiceTheMask2=Radiobutton(self.root, text="Other", padx = 20, variable=self.mask_type_choice, value=2)
RadioButtonChoiceTheMask3=Radiobutton(self.root, text="Undo", padx = 20, variable=self.mask_type_choice, value=3)
btnShow = Button(self.root, text='Show', command=self.slider_refresh)
self.slider1 = Scale(self.root, from_=0, to=5000, orient=HORIZONTAL)
self.slider2 = Scale(self.root, from_=0, to=100, orient=HORIZONTAL)
self.slider3 = Scale(self.root, from_=0, to=30, orient=HORIZONTAL, resolution = 0.5)
self.slider1.set(150)
self.slider2.set(20)
self.slider3.set(1)
# Save Mask Button
btnSave = Button(self.root, text='Save Mask', command=self.save_mask)
# Adjust the Mask
btnAdjust = Button(self.root, text='Adjust the Mask', command=self.adjust_mask)
self.text = Text(self.root,width=100, height=1)
#set the position of the widget in the window
btnSelectImage.grid(row=0,column=0,columnspan=2)
btnReset.grid(row=0,column=2,columnspan=2)
btnOpenMask.grid(row=0,column=4,columnspan=2)
self.panelA.grid(row=1,column=1,columnspan=4,rowspan=4)
self.text.grid(row=5,column=1,columnspan=3,sticky='WE')
btnZoomP.grid(row=2,column=5)
btnZoomM.grid(row=3,column=5)
RadioButtonChoiceTheMask1.grid(row=1,column=6)
RadioButtonChoiceTheMask2.grid(row=2,column=6)
RadioButtonChoiceTheMask3.grid(row=3,column=6)
btnNext.grid(row=2,column=0)
btnPrevious.grid(row=3,column=0)
RadioButton1.grid(row=6,column=1)
RadioButton2.grid(row=6,column=2)
RadioButton3.grid(row=6,column=3)
self.slider1.grid(row=7,column=1,columnspan=3,sticky='WE')
self.slider2.grid(row=8,column=1,columnspan=3,sticky='WE')
self.slider3.grid(row=9,column=1,columnspan=3,sticky='WE')
btnShow.grid(row=8,column=4)
btnAdjust.grid(row=10,column=2,columnspan=2)
btnSave.grid(row=11,column=2,columnspan=2)
#set the keybord and mouse events
self.panelA.bind("<Button-1>", self.select_segment)
self.panelA.bind("<B1-Motion>", self.select_segment)
# This is what enables using the mouse for move the image in the window
self.panelA.bind("<ButtonPress-3>",self.move_start)
self.panelA.bind("<B3-Motion>",self.move_move)
#linux scroll zoom
self.panelA.bind("<Button-4>", self.zoomerPiu)
self.panelA.bind("<Button-5>", self.zoomerMeno)
#windows scroll zoom
self.panelA.bind("<MouseWheel>", self.zoomer)
def adjust_mask(self):
if(self.path!=None):
self.t = Toplevel()
self.t.wm_title("adjust the mask")
self.mask=cv2.resize(self.mask,(self.width_original, self.height_original), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original
height,width=self.image.shape[:2]
self.panelB = Canvas(self.t, width=self.panel_width.get(), height=self.panel_height.get(), background="bisque")
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.6,self.mask,0.3,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT=toimage(imageOUT)
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelB.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelB.image = imageOUT
RadioButtonChoiceTheMask1=Radiobutton(self.t, text="Water", padx = 20, variable=self.mask_type_choice, value=1)
RadioButtonChoiceTheMask2=Radiobutton(self.t, text="Other", padx = 20, variable=self.mask_type_choice, value=2)
RadioButtonChoiceTheMask3=Radiobutton(self.t, text="Undo", padx = 20, variable=self.mask_type_choice, value=3)
self.pen_size=IntVar()
self.pen_size.set(3)
RadioButtonPenSize1=Radiobutton(self.t, text="small", padx = 20, variable=self.pen_size, value=1)
RadioButtonPenSize2=Radiobutton(self.t, text="normal", padx = 20, variable=self.pen_size, value=2)
RadioButtonPenSize3=Radiobutton(self.t, text="big", padx = 20, variable=self.pen_size, value=3)
btnCloseAdjustMask = Button(self.t, text='Confirm Modification', command=self.close_adjust_mask)
self.panelB.grid(row=0,column=0,columnspan=3)
RadioButtonChoiceTheMask1.grid(row=1,column=0)
RadioButtonChoiceTheMask2.grid(row=1,column=1)
RadioButtonChoiceTheMask3.grid(row=1,column=2)
RadioButtonPenSize1.grid(row=2,column=0)
RadioButtonPenSize2.grid(row=2,column=1)
RadioButtonPenSize3.grid(row=2,column=2)
btnCloseAdjustMask.grid(row=3,column=1)
#set the keybord and mouse events
self.panelB.bind("<B1-Motion>", self.draw)
self.panelB.bind("<ButtonPress-3>",self.move_start_adjust_mask)
self.panelB.bind("<B3-Motion>",self.move_move_adjust_mask)
#linux scroll zoom
self.panelB.bind("<Button-4>", self.zoomerPiuB)
self.panelB.bind("<Button-5>", self.zoomerMenoB)
#windows scroll zoom
self.panelB.bind("<MouseWheel>", self.zoomerB)
def move_start_adjust_mask(self,event):
self.panelB.scan_mark(event.x, event.y)
def move_move_adjust_mask(self,event):
self.panelB.scan_dragto(event.x, event.y, gain=1)
def draw(self,event):
x=int(self.panelB.canvasx(event.x))#event.x
y=int(self.panelB.canvasy(event.y))#event.y
if(self.mask_type_choice.get() == 1): color = np.float64([100,100,255])
if(self.mask_type_choice.get() == 2): color = np.float64([255,255,255])
if(self.mask_type_choice.get() == 3): color = np.float64([0,0,0])
height,width=self.image.shape[:2]
if(self.pen_size.get()==1):
if(x>=5): x1=x-5
else: x1=0
if(y>=5): y1=y-5
else: y1=0
if(x<=width-5): x2=x+5
else: x2=width
if(y<=height-5): y2=y+5
else: y2=height
if(self.pen_size.get()==2):
if(x>=15): x1=x-15
else: x1=0
if(y>=15): y1=y-15
else: y1=0
if(x<=width-15): x2=x+15
else: x2=width
if(y<=height-15): y2=y+15
else: y2=height
if(self.pen_size.get()==3):
if(x>=25): x1=x-25
else: x1=0
if(y>=25): y1=y-25
else: y1=0
if(x<=width-25): x2=x+25
else: x2=width
if(y<=height-25): y2=y+25
else: y2=height
self.mask[y1:y2,x1:x2]=color
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT = toimage(imageOUT)
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelB.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelB.image = imageOUT
def close_adjust_mask(self):
self.t.destroy()
self.mask=cv2.resize(self.mask,(self.width_original, self.height_original), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT = toimage(imageOUT)
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
def open_mask(self):
if(self.path!=None):
path=self.path_to_image()
mask = cv2.imread(path)
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
self.mask=mask
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT = toimage(imageOUT)
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
def open_image(self):
path=self.path_to_image()
self.path=path
# ensure a file path was selected
if len(path) > 0:
#get the self.root of the path
pathroot=path[:path.rfind('/')] + '/*'+path[path.rfind('.'):]
#open all the images in the path
self.images = glob.glob(pathroot)
#find the index of the image selected for trace the previous and next images
self.index_image=self.find_index_selected(self.images,path)
image = cv2.imread(path)
image = self.increase_contrast_image(image)
#convert to RGB from BGR
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.height_original,self.width_original=image.shape[:2]
self.image_original=image
self.image_original_2x = cv2.resize(image,(self.width_original*2, self.height_original*2), interpolation = cv2.INTER_CUBIC)
self.image_original_3x = cv2.resize(image,(self.width_original*3, self.height_original*3), interpolation = cv2.INTER_CUBIC)
self.image_original_4x = cv2.resize(image,(self.width_original*4, self.height_original*4), interpolation = cv2.INTER_CUBIC)
#init the mask for the image
self.mask = np.zeros(self.image_original.shape[:3], dtype = "uint8")
self.image=image
# convert the images to PIL format...
image=toimage(image)
# ...and then to ImageTk format
imageOUT = ImageTk.PhotoImage(image)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
self.text.delete(1.0,END)
self.text.insert(INSERT,self.path)
def reset_image(self):
if(self.path!=None):
path=self.path
if len(path) > 0:
#get the self.root of the path
pathroot=path[:path.rfind('/')] + '/*'+path[path.rfind('.'):]
#open all the images in the path
self.images = glob.glob(pathroot)
#find the index of the image selected for trace the previous and next images
self.index_image=self.find_index_selected(self.images,path)
image = cv2.imread(path)
image = self.increase_contrast_image(image)
#convert to RGB from BGR
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.height_original,self.width_original=image.shape[:2]
self.image_original=image
self.image_original_2x = cv2.resize(image,(self.width_original*2, self.height_original*2), interpolation = cv2.INTER_CUBIC)
self.image_original_3x = cv2.resize(image,(self.width_original*3, self.height_original*3), interpolation = cv2.INTER_CUBIC)
self.image_original_4x = cv2.resize(image,(self.width_original*4, self.height_original*4), interpolation = cv2.INTER_CUBIC)
#init the mask for the image
self.mask = np.zeros(self.image_original.shape[:3], dtype = "uint8")
self.image=image
# convert the images to PIL format...
image=toimage(image)
# ...and then to ImageTk format
imageOUT = ImageTk.PhotoImage(image)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
self.text.delete(1.0,END)
self.text.insert(INSERT,self.path)
#increase the contrast of the image
def increase_contrast_image(self,image):
lab= cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
cl = clahe.apply(l)
limg = cv2.merge((cl,a,b))
image_high_contrast = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return image_high_contrast
#dialog window to choose the path to images
def path_to_image(self):
path = filedialog.askopenfilename()
return path
#find the index of the actual image selected
def find_index_selected(self,images,path):
i=0
for pathImage in self.images:
if(path==pathImage):
indexSelected=i
i=i+1
return indexSelected
def select_segment(self,event):
if(self.path!=None):
#true_x = panelA.canvasx(event.x)
#true_y = panelA.canvasy(event.y)
x=int(self.panelA.canvasx(event.x))#event.x
y=int(self.panelA.canvasy(event.y))#event.y
height,width=self.image.shape[:2]
if(x>0 and x<width and y>0 and y<height):
#print(x,"-",y,"-",true_x,"-",true_y)
clicked_segment = self.segments[y,x]
if(self.mask_type_choice.get() == 1): color = np.float64([100,100,255])
if(self.mask_type_choice.get() == 2): color = np.float64([255,255,255])
if(self.mask_type_choice.get() == 3): color = np.float64([0,0,0])
self.mask[self.segments == clicked_segment] = color
#image2=np.copy(self.image)
#for x in range (self.width_original):
# for y in range (self.height_original):
# tmp=self.mask.item(x,y)
# if(np.array_equal(tmp, (0,0,0))==False):
# image2[y,x]=self.mask[y,x]
#imageOUT = cv2.bitwise_or(self.image,self.mask)
#idx = (self.mask==color)
#image2[idx] = self.mask[idx]
#image2=np.copy(self.image)
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
imageOUT = toimage(mark_boundaries(imageOUT, self.segments))
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
def slider_pack(self):
if(self.segment_type_choice.get()==1):
self.slider1.configure(from_=0, to=5000)
self.slider2.configure(from_=0, to=100)
self.slider3.configure(from_=0, to=30)
self.slider1.set(150)
self.slider2.set(20)
self.slider3.set(1)
if(self.segment_type_choice.get()==2):
self.slider1.configure(from_=0, to=1000)
self.slider2.configure(from_=1, to=400)
self.slider3.configure(from_=0, to=1, resolution=0.05)
self.slider1.set(100)
self.slider2.set(50)
self.slider3.set(0.95)
if(self.segment_type_choice.get()==3):
self.slider1.configure(from_=3, to=31)
self.slider2.configure(from_=1, to=400)
self.slider3.configure(from_=0, to=1, resolution=0.05)
self.slider1.set(3)
self.slider2.set(6)
self.slider3.set(0.5)
def radioBtn_refresh(self):
self.slider_pack()
if(self.segment_type_choice.get()==1):
numSeg=self.slider1.get()
comp= self.slider2.get()
sigma=float(self.slider3.get())
self.segments = slic(self.image, n_segments=numSeg, compactness=comp, sigma=sigma,slic_zero=True)
if(self.segment_type_choice.get()==2):
scale=self.slider1.get()
min_size=self.slider2.get()
sigma=self.slider3.get()
self.segments = felzenszwalb(self.image, scale=scale, sigma=sigma, min_size=min_size)
if(self.segment_type_choice.get()==3):
kernel_size=self.slider1.get()
max_dist= self.slider2.get()
ratio=self.slider3.get()
self.segments = quickshift(self.image, kernel_size=3, max_dist=6, ratio=0.5)
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT = toimage(mark_boundaries(imageOUT, self.segments))
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
def slider_refresh(self):
if(self.segment_type_choice.get()==1):
numSeg=self.slider1.get()
comp= self.slider2.get()
sigma=self.slider3.get()
self.segments = slic(self.image, n_segments=numSeg, compactness=comp, sigma=sigma,slic_zero=True)
if(self.segment_type_choice.get()==2):
scale=self.slider1.get()
min_size= self.slider2.get()
sigma=self.slider3.get()
self.segments = felzenszwalb(self.image, scale=scale, sigma=sigma, min_size=min_size)
if(self.segment_type_choice.get()==3):
kernel_size=self.slider1.get()
max_dist= self.slider2.get()
ratio=self.slider3.get()
self.segments = quickshift(self.image, kernel_size=3, max_dist=6, ratio=0.5)
imageOUT = np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT = toimage(mark_boundaries(imageOUT, self.segments))
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
def Next(self):
if(self.path!=None):
if(self.index_image<len(self.images)-1):
self.index_image=self.index_image+1
self.path=self.images[self.index_image]
image = cv2.imread(self.path)
image = self.increase_contrast_image(image)
#convert to RGB from BGR
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.height_original,self.width_original=image.shape[:2]
self.image_original=image
self.image_original_2x = cv2.resize(image,(self.width_original*2, self.height_original*2), interpolation = cv2.INTER_CUBIC)
self.image_original_3x = cv2.resize(image,(self.width_original*3, self.height_original*3), interpolation = cv2.INTER_CUBIC)
self.image_original_4x = cv2.resize(image,(self.width_original*4, self.height_original*4), interpolation = cv2.INTER_CUBIC)
#init the mask for the image
self.mask = np.zeros(self.image_original.shape[:3], dtype = "uint8")
self.image=image
# convert the images to PIL format...
image=toimage(image)
# ...and then to ImageTk format
imageOUT = ImageTk.PhotoImage(image)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
self.text.delete(1.0,END)
self.text.insert(INSERT,self.path)
def Previous(self):
if(self.path!=None):
if(self.index_image>=0):
self.index_image=self.index_image-1
self.path=self.images[self.index_image]
image = cv2.imread(self.path)
image = self.increase_contrast_image(image)
#convert to RGB from BGR
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.height_original,self.width_original=image.shape[:2]
self.image_original=image
self.image_original_2x = cv2.resize(image,(self.width_original*2, self.height_original*2), interpolation = cv2.INTER_CUBIC)
self.image_original_3x = cv2.resize(image,(self.width_original*3, self.height_original*3), interpolation = cv2.INTER_CUBIC)
self.image_original_4x = cv2.resize(image,(self.width_original*4, self.height_original*4), interpolation = cv2.INTER_CUBIC)
#init the mask for the image
self.mask = np.zeros(self.image_original.shape[:3], dtype = "uint8")
self.image=image
# convert the images to PIL format...
image=toimage(image)
# ...and then to ImageTk format
imageOUT = ImageTk.PhotoImage(image)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
self.text.delete(1.0,END)
self.text.insert(INSERT,self.path)
#move the image in the window
def move_start(self,event):
self.panelA.scan_mark(event.x, event.y)
def move_move(self,event):
self.panelA.scan_dragto(event.x, event.y, gain=1)
#windows zoom
def zoomer(self,event):
if (event.delta > 0):
if self.zoomCycle != 4: self.zoomCycle += 1
elif (event.delta < 0):
if self.zoomCycle != 0: self.zoomCycle -= 1
self.zoom()
#linux zoom
def zoomerPiu(self,event):
if self.zoomCycle != 4: self.zoomCycle += 1
self.zoom()
def zoomerMeno(self,event):
if self.zoomCycle != 0: self.zoomCycle -= 1
self.zoom()
#windows zoom
def zoomerB(self,event):
if (event.delta > 0):
if self.zoomCycle != 4: self.zoomCycle += 1
elif (event.delta < 0):
if self.zoomCycle != 0: self.zoomCycle -= 1
self.zoomB()
#linux zoom
def zoomerPiuB(self,event):
if self.zoomCycle != 4: self.zoomCycle += 1
self.zoomB()
def zoomerMenoB(self,event):
if self.zoomCycle != 0: self.zoomCycle -= 1
self.zoomB()
#zoom with button
def zoomerPiuBtn(self):
if self.zoomCycle != 4: self.zoomCycle += 1
self.zoom()
def zoomerMenoBtn(self):
if self.zoomCycle != 0: self.zoomCycle -= 1
self.zoom()
def zoom(self):
if(self.path!=None):
if (self.zoomCycle) != 0:
if self.zoomCycle == 1:
self.mask=cv2.resize(self.mask,(self.width_original, self.height_original), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original
elif self.zoomCycle == 2:
self.mask=cv2.resize(self.mask,(self.width_original*2, self.height_original*2), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original_2x
elif self.zoomCycle == 3:
self.mask=cv2.resize(self.mask,(self.width_original*3, self.height_original*3), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original_3x
elif self.zoomCycle == 4:
self.mask=cv2.resize(self.mask,(self.width_original*4, self.height_original*4), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original_4x
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT=toimage(imageOUT)
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelA.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelA.image = imageOUT
def zoomB(self):
if(self.path!=None):
if (self.zoomCycle) != 0:
if self.zoomCycle == 1:
self.mask=cv2.resize(self.mask,(self.width_original, self.height_original), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original
elif self.zoomCycle == 2:
self.mask=cv2.resize(self.mask,(self.width_original*2, self.height_original*2), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original_2x
elif self.zoomCycle == 3:
self.mask=cv2.resize(self.mask,(self.width_original*3, self.height_original*3), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original_3x
elif self.zoomCycle == 4:
self.mask=cv2.resize(self.mask,(self.width_original*4, self.height_original*4), interpolation = cv2.INTER_CUBIC)
self.image=self.image_original_4x
imageOUT = cv2.bitwise_or(self.image,self.mask)
#imageOUT = cv2.addWeighted(self.image,0.3,self.mask,0.7,0)
#imageOUT=np.where(self.mask==(0,0,0),self.image,self.mask)
imageOUT=toimage(imageOUT)
imageOUT = ImageTk.PhotoImage(imageOUT)
self.panelB.create_image(0, 0, image = imageOUT, anchor = NW)
self.panelB.image = imageOUT
def save_mask(self):
if(self.path!=None):
mask=cv2.resize(self.mask,(self.width_original, self.height_original), interpolation = cv2.INTER_CUBIC)
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
mask2saveWater=mask
#mask2saveWater = np.zeros(self.image_original.shape[:2], dtype = "uint8")
#mask2saveOther = np.zeros(self.image_original.shape[:3], dtype = "uint8")
#for x in range (self.width_original):
# for y in range (self.height_original):
# tmp=mask[y,x]
# if(np.array_equal(tmp, (100,100,255))):
# mask2saveWater[y,x] = 1
#elif(mask [x,y] == (255,255,255)):
#mask2saveOther[x,y] = (255,255,255)
Fname=(self.path[self.path.rfind('/')+1 : self.path.rfind('.')] + '-annotation.png')
f = filedialog.asksaveasfile(mode='wb',initialfile=Fname, defaultextension=".png", filetypes=(("PNG file", "*.png"),("All Files", "*.*")))
if f:
abs_path = os.path.abspath(f.name)
cv2.imwrite(abs_path,mask2saveWater)
#maskOUT=toimage(mask2saveWater)
#maskOUT.save(abs_path)
#The master of the GUI
root=Tk()
#call the class for the SegmentationGUI
app=SegmentationGUI(root)
#start the GUI loop
root.mainloop()
# In[ ]:
# In[ ]: