forked from 12HuYang/Rooster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoom_example.py
538 lines (484 loc) · 21.6 KB
/
zoom_example.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
# -*- coding: utf-8 -*-
# Advanced zoom example. Like in Google Maps.
# It zooms only a tile, but not the whole image. So the zoomed tile occupies
# constant memory and not crams it with a huge resized image for the large zooms.
import random
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk, ImageDraw
import tkinter.filedialog as filedialog
import cv2
import numpy as np
class AutoScrollbar(ttk.Scrollbar):
''' A scrollbar that hides itself if it's not needed.
Works only if you use the grid geometry manager '''
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
self.grid_remove()
else:
self.grid()
ttk.Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise tk.TclError('Cannot use pack with this widget')
def place(self, **kw):
raise tk.TclError('Cannot use place with this widget')
class Zoom_Advanced(ttk.Frame):
''' Advanced zoom of the image '''
def __init__(self, mainframe, canvas,path,rownum,colnum,canvasw,canvash):
''' Initialize the main Frame '''
ttk.Frame.__init__(self, master=mainframe)
# self.master.title('Zoom with mouse wheel')
# Vertical and horizontal scrollbars for canvas
vbar = AutoScrollbar(self.master, orient='vertical')
hbar = AutoScrollbar(self.master, orient='horizontal')
vbar.grid(row=0, column=1, sticky='ns')
hbar.grid(row=1, column=0, sticky='we')
# Create canvas and put image on it
self.canvasw=canvasw
self.canvash=canvash
self.canvas = canvas
self.canvas.configure(xscrollcommand=hbar.set,yscrollcommand=vbar.set)
# tk.Canvas(self.master, highlightthickness=0,
# xscrollcommand=hbar.set, yscrollcommand=vbar.set)
self.canvas.grid(row=0, column=0, sticky='nswe')
self.canvas.configure(width=canvasw,height=canvash)
self.canvas.update() # wait till canvas is created
vbar.configure(command=self.scroll_y) # bind scrollbars to the canvas
hbar.configure(command=self.scroll_x)
# Make the canvas expandable
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
# Bind events to the Canvas
self.canvas.bind('<Configure>', self.show_image) # canvas is resized
self.canvas.bind('<ButtonPress-1>', self.move_from)
self.canvas.bind('<B1-Motion>', self.move_to)
# self.canvas.bind('<MouseWheel>', self.wheel) # with Windows and MacOS, but not Linux
self.canvas.bind('<Double-Button-1>', self.labelimage) # only with Linux, wheel scroll down
# self.canvas.bind('<Button-4>', self.wheel) # only with Linux, wheel scroll up
# self.image = Image.open(path) # open image
# imagearray=cv2.imread(path,flags=cv2.IMREAD_ANYCOLOR)
# h,w,c=np.shape(imagearray)
# RGBfile=cv2.cvtColor(imagearray,cv2.COLOR_BGR2RGB)
self.path=path
self.image=Image.open(self.path)
self.transimage=self.image.convert("RGBA")
newdata=[(255,255,255,0) for i in range(self.image.height*self.image.width)]
self.transimage.putdata(newdata)
# self.transimage.show()
# self.image.paste(self.transimage,(0,0),self.transimage)
# self.canvasimg=self.canvas.create_image(0,0,image=self.image,anchor='nw')
self.npimage=np.zeros((self.image.height,self.image.width))
# self.height, self.width,_ = np.shape(self.image)
self.width, self.height = self.image.size
print(self.width,self.height)
self.imscale = 1.0 # scale for the canvaas image
self.delta = 1.2 # zoom magnitude
# Put image into container rectangle and use it to set proper coordinates to the image
self.container = self.canvas.create_rectangle(0, 0, self.width, self.height, width=0)
# Plot some optional random rectangles for the test purposes
# minsize, maxsize, number = 5, 20, 10
# for n in range(number):
# x0 = random.randint(0, self.width - maxsize)
# y0 = random.randint(0, self.height - maxsize)
# x1 = x0 + random.randint(minsize, maxsize)
# y1 = y0 + random.randint(minsize, maxsize)
# color = ('red', 'orange', 'yellow', 'green', 'blue')[random.randint(0, 4)]
# self.canvas.create_rectangle(x0, y0, x1, y1, fill=color, activefill='black')
self.rownum=rownum
self.colnum=colnum
self.infectlist=[]
self.predictlist=[]
self.confidence=[]
self.confidup=0.95
self.confiddown=0.75
self.confidthres=0.50
self.getgrid=0
# self.updatenpimage()
# self.show_image()
scale=max(self.canvasw/self.width,self.canvash/self.width)
print('scale',scale)
self.imscale=scale
self.canvas.scale('all', 0, 0, scale, scale) # rescale all canvas objects
self.show_image()
def updatenpimage(self):
gridnum=self.rownum*self.colnum
row_stepsize=int(self.height/self.rownum)
col_stepsize=int(self.width/self.colnum)
print(gridnum,row_stepsize,col_stepsize)
self.npimage=np.zeros((self.image.height,self.image.width))
for i in range(gridnum):
c=i%self.colnum
r=int(i/self.colnum)
print(r,c)
self.npimage[r*row_stepsize:(r+1)*row_stepsize,c*col_stepsize:(c+1)*col_stepsize]=i+1
print(self.npimage)
def scroll_y(self, *args, **kwargs):
''' Scroll canvas vertically and redraw the image '''
self.canvas.yview(*args, **kwargs) # scroll vertically
self.show_image() # redraw the image
def scroll_x(self, *args, **kwargs):
''' Scroll canvas horizontally and redraw the image '''
self.canvas.xview(*args, **kwargs) # scroll horizontally
self.show_image() # redraw the image
def move_from(self, event):
''' Remember previous coordinates for scrolling with the mouse '''
self.canvas.scan_mark(event.x, event.y)
def move_to(self, event):
''' Drag (move) canvas to the new position '''
self.canvas.scan_dragto(event.x, event.y, gain=1)
self.zoomx=event.x
self.zoomy=event.y
self.show_image() # redraw the image
def wheel(self, opt):
''' Zoom with mouse wheel '''
# x = self.canvas.canvasx(event.x)
# y = self.canvas.canvasy(event.y)
# x=0
# y=0
try:
x=self.zoomx
y=self.zoomy
except:
bbox = self.canvas.bbox(self.container) # get image area
x=int((bbox[2]-bbox[0])/2)
y=int((bbox[3]-bbox[1])/2)
# if bbox[0] < x < bbox[2] and bbox[1] < y < bbox[3]: pass # Ok! Inside the image
# else: return # zoom only inside image area
scale = 1.0
# # Respond to Linux (event.num) or Windows (event.delta) wheel event
if opt==0: # scroll down
i = min(self.width, self.height)
if int(i * self.imscale) < 30: return # image is less than 30 pixels
self.imscale /= self.delta
scale /= self.delta
if opt==1:
# if event.num == 4 or event.delta == 120: # scroll up
i = min(self.canvas.winfo_width(), self.canvas.winfo_height())
if i < self.imscale: return # 1 pixel is bigger than the visible area
self.imscale *= self.delta
scale *= self.delta
# x1 = max(bbox2[0] - bbox1[0], 0) # get coordinates (x1,y1,x2,y2) of the image tile
# y1 = max(bbox2[1] - bbox1[1], 0)
# x2 = min(bbox2[2], bbox1[2]) - bbox1[0]
# y2 = min(bbox2[3], bbox1[3]) - bbox1[1]
# if int(x2 - x1) > 0 and int(y2 - y1) > 0: # show image if it in the visible area
# x = min(int(x2 / self.imscale), self.width) # sometimes it is larger on 1 pixel...
# y = min(int(y2 / self.imscale), self.height) # ...and sometimes not
# # imagearray=self.image.astype('uint8')
# # self.image=Image.fromarray(self.image)
#
print(x,y,scale)
self.canvas.scale('all', x, y, scale, scale) # rescale all canvas objects
self.show_image()
def show_image(self,event=None):
''' Show image on the Canvas '''
bbox1 = self.canvas.bbox(self.container) # get image area
# Remove 1 pixel shift at the sides of the bbox1
bbox1 = (bbox1[0] + 1, bbox1[1] + 1, bbox1[2] - 1, bbox1[3] - 1)
bbox2 = (self.canvas.canvasx(0), # get visible area of the canvas
self.canvas.canvasy(0),
self.canvas.canvasx(self.canvas.winfo_width()),
self.canvas.canvasy(self.canvas.winfo_height()))
bbox = [min(bbox1[0], bbox2[0]), min(bbox1[1], bbox2[1]), # get scroll region box
max(bbox1[2], bbox2[2]), max(bbox1[3], bbox2[3])]
if bbox[0] == bbox2[0] and bbox[2] == bbox2[2]: # whole image in the visible area
bbox[0] = bbox1[0]
bbox[2] = bbox1[2]
if bbox[1] == bbox2[1] and bbox[3] == bbox2[3]: # whole image in the visible area
bbox[1] = bbox1[1]
bbox[3] = bbox1[3]
self.canvas.configure(scrollregion=bbox) # set scroll region
x1 = max(bbox2[0] - bbox1[0], 0) # get coordinates (x1,y1,x2,y2) of the image tile
y1 = max(bbox2[1] - bbox1[1], 0)
x2 = min(bbox2[2], bbox1[2]) - bbox1[0]
y2 = min(bbox2[3], bbox1[3]) - bbox1[1]
# self.image.paste(self.transimage,(0,0),self.transimage)
image=self.image.copy()
if self.getgrid==1:
image.paste(self.transimage,(0,0),self.transimage)
# image=image.paste(self.transimage,(0,0),self.transimage)
if int(x2 - x1) > 0 and int(y2 - y1) > 0: # show image if it in the visible area
x = min(int(x2 / self.imscale), self.width) # sometimes it is larger on 1 pixel...
y = min(int(y2 / self.imscale), self.height) # ...and sometimes not
# imagearray=self.image.astype('uint8')
# self.image=Image.fromarray(self.image)
image = image.crop((int(x1 / self.imscale), int(y1 / self.imscale), x, y))
# transimg=self.transimage.crop((int(x1 / self.imscale), int(y1 / self.imscale), x, y))
# image=image.paste(transimg,(0,0),transimg)
imagetk = ImageTk.PhotoImage(image.resize((int(x2 - x1), int(y2 - y1))))
imageid = self.canvas.create_image(max(bbox2[0], bbox1[0]), max(bbox2[1], bbox1[1]),image=imagetk,
anchor='nw')
self.canvas.lower(imageid) # set image into background
self.canvas.imagetk = imagetk # keep an extra reference to prevent garbage-collection
def changeimage(self,newimage,rownum,colnum,hasGrid):
if hasGrid==False:
self.image=newimage
self.rownum=rownum
self.colnum=colnum
self.width, self.height = self.image.size
print(self.width,self.height)
self.getgrid=1
if sum(self.infectlist)>0: #has labels, to revise from hidden grids
tempinfeclist=np.where(np.array(self.infectlist)==1)
tempinfeclist=list(tempinfeclist)[0]
# tempinfeclist=[ele for ele in tempinfeclist]
# tempinfeclist=self.infectlist.copy()
self.labelmulti(tempinfeclist)
if len(self.confidence)>0:
self.showcomparison([],False)
else:
self.infectlist=[0 for i in range(self.rownum*self.colnum)]
# newimage_width,newimage_height=newimage.size
self.updatenpimage() #segment np.array by grid numbers, rows and columns
self.show_image()
else:
self.image=Image.open(self.path)
print(self.width,self.height)
self.getgrid=0
self.show_image()
def addbars(self,locs):
x0=min(locs[1])
y0=min(locs[0])
x1=max(locs[1])
y1=max(locs[0])
draw=ImageDraw.Draw(self.image)
# endx=int(x0+(x1-x0)/2)
# endy=int(y0+(y1-y0)/2)
draw.line(((x0,y0),(x1,y0)),fill='red',width=5) #draw up red line
draw.line(((x0,y0),(x0,y1)),fill='red',width=5) #draw left red line
# self.show_image()
def rmbars(self,locs):
x0=min(locs[1])
y0=min(locs[0])
x1=max(locs[1])
y1=max(locs[0])
# endx=int(x0+(x1-x0)/2)
# endy=int(y0+(y1-y0)/2)
draw=ImageDraw.Draw(self.image)
draw.line(((x0,y0),(x1,y0)),fill='white',width=5) #draw up red line
draw.line(((x0,y0),(x0,y1)),fill='white',width=5) #draw left red line
# self.show_image()
def labelimage(self,event):
if self.getgrid==0:
return
x = int(self.canvas.canvasx(event.x))
y = int(self.canvas.canvasy(event.y))
bbox = self.canvas.bbox(self.container)
print(x,y)
print(bbox)
if bbox[0] < x < bbox[2] and bbox[1] < y < bbox[3]: pass # Ok! Inside the image
else:
print('outsie image area')
return # label only inside image area
# x=int(x*self.imscale)
# y=int(y*self.imscale)
# print(x,y,self.imscale)
x=x-bbox[0]
y=y-bbox[1]
mirrornpimage=self.npimage.copy()
mirrornpimage=cv2.resize(mirrornpimage,(int(self.width*self.imscale),int(self.height*self.imscale)),interpolation=cv2.INTER_LINEAR)
# mirrornpimage=cv2.resize(mirrornpimage,(bbox[2]-bbox[0],bbox[3]-bbox[1]),interpolation=cv2.INTER_LINEAR)
# infectnum=self.npimage[y,x]
infectnum=int(mirrornpimage[y,x])
print(infectnum)
if infectnum!=0:
locs=np.where(self.npimage==int(infectnum))
print(locs)
if self.infectlist[infectnum-1]==0:
# self.infectlist.append(infectnum)
self.infectlist[infectnum-1]=1
self.addbars(locs)
# color='red'
else:
# self.infectlist.remove(infectnum)
self.infectlist[infectnum-1]=0
self.rmbars(locs)
# color='white'
print(sum(self.infectlist))
if len(self.confidence)>0:
self.showindivicomparison(infectnum-1,locs)
self.show_image()
def labelmulti(self,infectlist):
self.infectlist=[0 for i in range(self.rownum*self.colnum)]
for i in range(0,self.rownum*self.colnum):
if i in infectlist:
self.infectlist[i]=1
infectnum=i+1
#infectnum=i
print(infectnum)
if infectnum!=0:
locs=np.where(self.npimage==int(infectnum))
# print(locs)
self.addbars(locs)
if len(self.confidence)>0:
self.showcomparison([],False)
self.show_image()
def labelall(self):
self.infectlist=list(np.array([1 for i in range(self.rownum*self.colnum)])-np.array(self.infectlist))
# for ele in self.infectlist:
# reverseinfect.remove(ele)
# self.uninfect.append(ele)
for i in range(len(self.infectlist)):
if self.infectlist[i]==0:
locs=np.where(self.npimage==(i+1))
self.rmbars(locs)
else:
locs=np.where(self.npimage==(i+1))
self.addbars(locs)
if len(self.confidence)>0:
self.showcomparison([],False)
self.show_image()
def adddiffsign(self,locs,type):
x0=min(locs[1])
y0=min(locs[0])
x1=max(locs[1])
y1=max(locs[0])
# startx=int(x0+(x1-x0)/2)
draw=ImageDraw.Draw(self.transimage)
if type=='1':
# draw.line(((startx,y0),(x1,y0)),fill='orange',width=5) #draw up red line
draw.ellipse([(x0-15,y0-15),(x0+15,y0+15)],fill='red')
# draw.line(((x0,y0),(x0,y1)),fill='orange',width=5) #draw left red line
if type=='-1':
draw.ellipse([(x0-15,y0-15),(x0+15,y0+15)], fill='white',outline='red', width=5)
def rmdiffsign(self,locs):
x0=min(locs[1])
y0=min(locs[0])
x1=max(locs[1])
y1=max(locs[0])
pixels=self.transimage.load()
for i in range(x0-15,x0+16):
for j in range(y0-15,y0+16):
pixels[i,j]=(255,255,255,0)
# startx = int(x0 + (x1 - x0) / 2)
# draw=ImageDraw.Draw(self.image)
# draw.line(((startx,y0),(x1,y0)),fill='white',width=5)
def addconfbar(self,locs):
x0=min(locs[1])
y0=min(locs[0])
x1=max(locs[1])
y1=max(locs[0])
starty=int(y0+(y1-y0)/2)
draw=ImageDraw.Draw(self.image)
draw.line(((x0,starty),(x0,y1)),fill='orange',width=5)
def rmconfbar(self,locs):
x0=min(locs[1])
y0=min(locs[0])
x1=max(locs[1])
y1=max(locs[0])
draw=ImageDraw.Draw(self.image)
draw.line(((x0,y0),(x0,y1)),fill='white',width=5)
def showindivicomparison(self,i,locs):
temppred = [0 for i in range(len(self.confidence))]
satisfiedpred = np.where(np.array(self.confidence) >= self.confidthres)
temppred=np.array(temppred)
temppred[satisfiedpred] = 1
difflist=list(np.array(temppred)-np.array(self.infectlist))
if difflist[i]!=0:
if difflist[i]==1:
print('pred=1,user=0')
self.adddiffsign(locs,str(1))
if difflist[i]==-1:
print('pred=0,user=1')
self.adddiffsign(locs,str(-1))
else:
# if self.infectlist[i]==0:
# color='white'
# if self.infectlist[i]==1:
# color='red'
self.rmdiffsign(locs)
# if self.confidence[i]>=self.confiddown and self.confidence[i]<=self.confidup:
# self.addconfbar(locs)
# else:
# # if self.infectlist[i]==0:
# # color='white'
# # if self.infectlist[i]==1:
# # color='red'
# self.rmconfbar(locs)
self.show_image()
def showcomparison(self,confidence,hasPred):
# self.predictlist=[0 for i in range(len(self.infectlist))]
# for num in predicts:
# self.predictlist[num]=1
print('hasPred',hasPred)
if self.confidence!=confidence and len(confidence)!=0:
# self.predictlist=predicts.copy()
self.confidence=confidence.copy()
temppred=[0 for i in range(len(self.confidence))]
satisfiedpred=np.where(np.array(self.confidence)>=self.confidthres)
temppred=np.array(temppred)
temppred[satisfiedpred]=1
difflist=list(np.array(temppred)-np.array(self.infectlist))
if hasPred==False:
for i in range(len(difflist)):
locs=np.where(self.npimage==(i+1))
if difflist[i]!=0: #do not agree
print(i+1)
if difflist[i]==1:
self.adddiffsign(locs,'1')
if difflist[i]==-1:
self.adddiffsign(locs, '-1')
else: #agree results
# if self.infectlist[i]==0:
# color='white'
# if self.infectlist[i]==1:
# color='red'
self.rmdiffsign(locs)
# if self.confidence[i]>=self.confiddown and self.confidence[i]<=self.confidup:
# self.addconfbar(locs)
# else:
# # if self.infectlist[i]==0:
# # color='white'
# # if self.infectlist[i]==1:
# # color='red'
# self.rmconfbar(locs)
else:
for i in range(len(difflist)):
locs=np.where(self.npimage==(i+1))
# if self.infectlist[i]==0:
# color='white'
# if self.infectlist[i]==1:
# color='red'
self.rmdiffsign(locs)
# self.rmconfbar(locs)
self.show_image()
def changeconfidance(self,confidthres):
# if self.confiddown==low and self.confidup==up:
# return
# if self.confidthres>=confidthres:
# print('no change on confidthres',self.confidthres)
# return
# self.confiddown=low
# self.confidup=up
if self.confidthres==confidthres:
return
self.confidthres=confidthres
print('change confidthres',self.confidthres)
# for i in range(len(self.confidence)):
# locs=np.where(self.npimage==(i+1))
# # if self.confidence[i]>=self.confiddown and self.confidence[i]<=self.confidup:
# # self.addconfbar(locs)
# if self.confidence[i]>=self.confidthres:
# self.addconfbar(locs)
# else:
# # if self.infectlist[i]==0:
# # color='white'
# # if self.infectlist[i]==1:
# # color='red'
# self.rmconfbar(locs)
self.showcomparison([],False)
# self.show_image()
def resetlabels(self):
self.infectlist = []
self.predictlist = []
self.confidence = []
# self.rownum=row
# self.colnum=column
def output(self):
res={}
res.update({'npimage':self.npimage})
image=self.image.copy()
image.paste(self.transimage,(0,0),self.transimage)
res.update({'labeledimage':image})
res.update({'infectedlist':self.infectlist})
return res