-
Notifications
You must be signed in to change notification settings - Fork 17
/
gcp_check.py
359 lines (328 loc) · 15.1 KB
/
gcp_check.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
visual check tool for gcp_find.py
(c) Zoltan Siki siki (dot) zoltan (at) emk.bme.hu
"""
import argparse
from os import path
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import pandas as pd
import PIL
import PIL.ImageDraw
import PIL.ImageTk
import PIL.ImageFont
from matplotlib import font_manager
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):
""" set scollbar range """
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):
""" dummy function """
raise tk.TclError('Cannot use pack with this widget')
def place(self, **kw):
""" dummy function """
raise tk.TclError('Cannot use place with this widget')
class GcpCheck(tk.Tk):
""" class to check found GCPs visually """
def __init__(self, gcp_style, gcp_file=None, separator=" ", width=700, height=550,
img_path=''):
""" initialize
:param gcp_file: output file of gcp_find.py
:param gcp_style: GCP marker style parameters
:param separator: field separator in CGP file, default space
:param width: initial window width
:param height: initial window height
:param img_path: path to images if it is not in the same folder as CGP file
"""
tk.Tk.__init__(self)
self.title("Image Viewer")
self.geometry(f"{width}x{height}")
# add menu
self.menu = tk.Menu(master=self)
self.fileMenu = tk.Menu(self.menu, tearoff=0)
self.fileMenu.add_command(label="Open", command=self.SelectFile)
self.fileMenu.add_command(label="Exit", command=exit)
self.menu.add_cascade(label="File", menu=self.fileMenu)
self.config(menu=self.menu)
self.image = None
self.button_back = tk.Button(self, text="<-", command=self.back)
self.button_forward = tk.Button(self, text="->", command=self.forward)
self.button_back.grid(row=0, column=0, sticky='w')
self.button_forward.grid(row=0, column=2, sticky='e')
# scrollbars
vbar = AutoScrollbar(self, orient='vertical')
hbar = AutoScrollbar(self, orient='horizontal')
vbar.grid(row=1, column=4, sticky='ns')
hbar.grid(row=2, column=0, sticky='we')
self.canvas = tk.Canvas(self, highlightthickness=0,
xscrollcommand=hbar.set, yscrollcommand=vbar.set)
self.canvas.grid(row=1, column=0, columnspan=3, sticky='nswe')
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.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
# Bind events to the Canvas
self.canvas.bind('<Configure>', self.ShowImage) # 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('<Button-5>', self.wheel) # only with Linux, wheel scroll down
self.canvas.bind('<Button-4>', self.wheel) # only with Linux, wheel scroll up
self.gcp_file = gcp_file
self.style = gcp_style
self.separator = separator
self.width = None
self.height = None
self.img_path = img_path
self.imscale = 1.0 # scale for the canvaas image
self.delta = 1.3 # zoom magnitude
# load GCP file
self.gcps = []
self.imgs = []
self.img_no = 0
self.orig_img = None
self.img = None
font = font_manager.FontProperties(family='sans-serif', weight='normal')
font_file = font_manager.findfont(font)
self.font = PIL.ImageFont.truetype(font_file, size=self.style["fontsize"])
self.bind("<Configure>", self.ShowImage)
if self.gcp_file:
if self.LoadGcps():
self.ShowImage()
def SelectFile(self):
""" select and show gcp file """
self.gcp_file = filedialog.askopenfilename()
if self.gcp_file:
if self.LoadGcps():
self.img_no = 0
self.ShowImage()
def LoadGcps(self):
""" Load GCP image coordinates and image names
pandas data frame is created
a sorted list is also generated with image names
"""
retry = True
skiprows = 0
while retry:
try:
self.gcps = pd.read_csv(self.gcp_file, sep=self.separator,
skiprows=skiprows, header=None)
retry = False
except FileNotFoundError:
messagebox.showerror("Error", f"File not found: {self.gcp_file}")
return False
except UnicodeDecodeError:
messagebox.showerror("Error", f"Decode error: {self.gcp_file}")
return False
except pd.errors.ParserError:
if retry:
skiprows = 1
else:
messagebox.showerror("Error", f"File parse error: {self.gcp_file}")
return False
# necessary types of columns
typs = {"col": "int64", "row": "int64", "img": "object", "id": "int64",
"size": "int64", "east": "float64", "north": "float64",
"elev": "float64"}
if len(self.gcps.columns) == 4:
self.gcps.columns = ["col", "row", "img", "id"]
elif len(self.gcps.columns) == 5:
self.gcps.columns = ["col", "row", "img", "id", "size"]
elif len(self.gcps.columns) == 7:
self.gcps.columns = ["east", "north", "elev", "col", "row", "img", "id"]
else:
messagebox.showerror("Error", f"Invalid number of columns: {self.gcp_file}")
return False
for c, t in self.gcps.dtypes.iteritems():
if typs[c] != t:
messagebox.showerror("Error", f"Invalid column type {t} for '{c}'")
return False
self.imgs = sorted(list(set(self.gcps["img"])))
return True
def scroll_y(self, *args, **kwargs):
''' Scroll canvas vertically and redraw the image '''
self.canvas.yview(*args, **kwargs) # scroll vertically
self.ShowImage() # redraw the image
def scroll_x(self, *args, **kwargs):
''' Scroll canvas horizontally and redraw the image '''
self.canvas.xview(*args, **kwargs) # scroll horizontally
self.ShowImage() # 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.ShowImage() # redraw the image
def wheel(self, event):
''' Zoom with mouse wheel '''
x = self.canvas.canvasx(event.x)
y = self.canvas.canvasy(event.y)
bbox = self.canvas.bbox(self.container) # get image area
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 event.num == 5 or event.delta == -120: # 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 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
self.canvas.scale('all', x, y, scale, scale) # rescale all canvas objects
self.ShowImage()
def ShowImage(self, event=None):
""" show actual image with GCPs
"""
if self.imgs is None or len(self.imgs) == 0:
return
name = self.imgs[self.img_no]
if self.title() != name: # new image to display
self.title(name) # show image name in title
if not path.exists(name):
img_path = path.join(self.img_path, name)
if not path.exists(img_path):
img_path = path.join(path.split(self.gcp_file)[0], name)
# load image
self.image = PIL.Image.open(img_path)
self.width, self.height = self.image.size
# mark GCPs
img = PIL.ImageDraw.Draw(self.image)
for index, rec in self.gcps.loc[self.gcps['img'] == name].iterrows():
x = rec["col"]
y = rec["row"]
shape = [(x - self.style["markersize"]/2, y - self.style["markersize"]/2),
(x + self.style["markersize"]/2, y + self.style["markersize"]/2)]
img.ellipse(shape, outline=self.style["edgecolor"], width=self.style["edgewidth"])
img.text((x + self.style["markersize"]/2, y + self.style["markersize"]/2),
str(rec["id"]), font=self.font, fill=self.style["fontcolor"],
stroke_width=20)
self.container = self.canvas.create_rectangle(0, 0, self.width, self.height, width=0)
self.imscale = 1.0
canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
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(canvas_width),
self.canvas.canvasy(canvas_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]
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
image = self.image.crop((int(x1 / self.imscale), int(y1 / self.imscale), x, y))
imagetk = PIL.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]),
anchor='nw', image=imagetk)
self.canvas.lower(imageid) # set image into background
self.canvas.imagetk = imagetk # keep an extra reference to prevent garbage-collection
#canvas.scale(tk.ALL, x, y, xfactor, factor) TODO zoom extent
def ShowAll(self):
""" Show alll images """
for img in self.imgs:
self.ShowImage(img)
def ShowId(self, gcp_id):
""" Show images with GCP id
:param gcp_id: GCP id
"""
# collect image names with id
imgs = set()
for gcp in self.gcps:
if gcp[-1] == gcp_id:
imgs.add(gcp[-2])
for img in sorted(list(imgs)):
self.ShowImage(img, gcp_id)
def back(self):
self.img_no -= 1
if self.img_no < 0:
self.img_no = len(self.imgs) - 1
self.ShowImage()
def forward(self):
self.img_no += 1
if self.img_no >= len(self.imgs):
self.img_no = 0
self.ShowImage()
def cmd_params(parser):
""" handle command line parameters
:param parser: command line parser
"""
def_separator = " " # default separator is space
def_markersize = 200 # marker size
def_edgecolor = "red" # edge color for markers
def_edgewidth = 20 # edge width for markers
def_fontsize = 200 # text height pixel
def_fontcolor = 'red' # inner color for GP ID annotation
parser.add_argument('name', metavar='file_name', type=str, nargs='?',
help='GCP file to process')
parser.add_argument('--command', type=str, default='all',
help='command all/ID show all images/images with GCP ID')
parser.add_argument('--path', type=str, default='',
help='input path for images')
parser.add_argument('-s', '--separator', type=str, default=def_separator,
help=f'input file separator, default {def_separator}')
# parameters for marker display
parser.add_argument('--markersize', type=int, default=def_markersize,
help=f'marker size on image, default {def_markersize}')
parser.add_argument('--edgecolor', type=str, default=def_edgecolor,
help=f'marker edge color, default {def_edgecolor}')
parser.add_argument('--edgewidth', type=int, default=def_edgewidth,
help=f'marker edge width, default {def_edgewidth}')
parser.add_argument('--fontsize', type=int, default=def_fontsize,
help=f'font size on image, default {def_fontsize}')
parser.add_argument('--fontcolor', type=str, default=def_fontcolor,
help=f'font color on image, default {def_fontcolor}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
cmd_params(parser)
args = parser.parse_args()
if args.name:
name = args.name
else:
name = None
style = {'markersize': args.markersize,
'edgecolor': args.edgecolor,
'edgewidth': args.edgewidth,
'fontsize': args.fontsize,
'fontcolor': args.fontcolor,
}
gcp_c = GcpCheck(style, name, args.separator, img_path=args.path)
gcp_c.mainloop()
#if args.command == "all":
# gcp_c.ShowAll()
#elif re.match("^[0-9]+$", args.command):
# gcp_c.ShowId(int(args.command))
#else:
# gcp_c.ShowImage(args.command)