-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
424 lines (388 loc) · 21.8 KB
/
app.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
from PyQt5 import QtCore, QtGui, QtWidgets
from pyexiv2 import Image
import numpy as np
import pandas as pd
import os
import sys
centers = [] # list to store vignette centers as "x;y" strings
coefficients = [] # list to store vignette coefficients as "1.1;2.2;3.3;4.4;5.5;6.6" strings
def meta_filter(img_list):
"""
Check whether images in img_list contain metadata necessary for image averaging. If they lack tags or the value is 0
remove those images from the list and display their filenames in the text browser
:param img_list: filenames list containing all images from opened directory
:return: filenames list without filtered images, string with names of removed images
"""
filtered = ""
for name in img_list:
img = Image(name)
exif = img.read_exif()
try:
iso_speed = exif['Exif.Photo.ISOSpeedRatings']
exposure_time = exif['Exif.Photo.ExposureTime']
if iso_speed is None or exposure_time is None or iso_speed == '0' or exposure_time == '0':
img_list.remove(name)
filtered += str(name) + ", "
except KeyError:
img_list.remove(name)
filtered += str(name) + ", "
return img_list, filtered[:len(filtered)-2]
def check_average(img_list):
"""
Check whether the metadata from the image corresponds to the metadata from the first image of each band. BandName is
used to make sure images are from the same band. ISOSpeedRating and ExposureTime are used to make sure averaging
will deliver correct result. If there is a difference in tags, finish function presumably. Else, find an average
image and store it in the list. Repeat for each band
:param img_list: filenames list containing all images from opened directory
:return: Checkout flag, average images list, image size parameters and blacklevel
"""
averages = []
for i in range(5):
# Filter out all filenames but from band that is being processed
names = [name for name in img_list if "img{}_".format(i) in name]
img = Image(names[0]) # read first image and use its metadata as a reference
exif = img.read_exif()
xmp = img.read_xmp()
iso_speed = exif['Exif.Photo.ISOSpeedRatings']
exposure_time = exif['Exif.Photo.ExposureTime']
band_name = xmp['Xmp.Camera.BandName']
# Also define image size parameters and blacklevel from metadata for further use in the main script
img_width = int(exif['Exif.Image.ImageWidth'])
img_height = int(exif['Exif.Image.ImageLength'])
blacklevel = float(exif['Exif.Image.BlackLevel'][:len(exif['Exif.Image.BlackLevel'])-2])
for j in range(1, len(names)): # compare metadata from each image (besides first) to the reference
img = Image(names[j])
exif = img.read_exif()
xmp = img.read_xmp()
if exif['Exif.Photo.ISOSpeedRatings'] != iso_speed or exif['Exif.Photo.ExposureTime'] != exposure_time or \
xmp['Xmp.Camera.BandName'] != band_name:
return False, averages
# Open images as NumPy arrays of arrays for successful averaging of uint16 pixels
import matplotlib.image as mpimg
images = np.array([np.array(mpimg.imread(name)) for name in names])
average = np.array(np.mean(images, axis=0), dtype='uint16')
averages.append(average)
return True, averages, img_width, img_height, blacklevel
def poly6(x, b, c, e, g):
return 1 + b * x + c * x**2 + e * x**4 + g * x**6
class Ui_MainWindow(object):
"""
Qt-generated GUI Class with implemented open_file, finder and save_file functions. Open directory with
images of Geoscan Pollux bands. Launch the main script to calculate vignette coefficients and store them into lists.
Save tags.json and tags.ini configuration files in chosen directory
The GUI contains text browser to inform the user about the calibration progress
"""
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setFixedSize(543, 408)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 20, 540, 50))
font = QtGui.QFont()
font.setFamily("Ubuntu Mono")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setStyleSheet("")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.text = QtWidgets.QTextBrowser(self.centralwidget)
self.text.setGeometry(QtCore.QRect(15, 65, 520, 300))
self.text.setStyleSheet("")
self.text.setObjectName("text")
self.btn_start = QtWidgets.QPushButton(self.centralwidget)
self.btn_start.setEnabled(False)
self.btn_start.setGeometry(QtCore.QRect(15, 370, 150, 30))
self.btn_start.setObjectName("btn_start")
self.btn_clear = QtWidgets.QPushButton(self.centralwidget)
self.btn_clear.setGeometry(QtCore.QRect(425, 370, 110, 30))
self.btn_clear.setObjectName("btn_clear")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(0, 10, 543, 30))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.btn_open = QtWidgets.QPushButton(self.centralwidget)
self.btn_open.setGeometry(QtCore.QRect(0, 0, 80, 25))
self.btn_open.setAutoFillBackground(False)
self.btn_open.setStyleSheet("")
self.btn_open.setObjectName("btn_open")
self.btn_save = QtWidgets.QPushButton(self.centralwidget)
self.btn_save.setEnabled(False)
self.btn_save.setGeometry(QtCore.QRect(80, 0, 90, 25))
self.btn_save.setObjectName("btn_save")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Vignette Finder"))
self.label.setText(_translate("MainWindow", "Определение параметров виньетирования"))
self.text.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" color:#000000;\">"
"Чтобы начать, откройте директорию с фотографиями</span></p></body></html>"))
self.btn_open.setText(_translate("MainWindow", "Открыть"))
self.btn_open.clicked.connect(self.open_file)
self.btn_start.setText(_translate("MainWindow", "Запустить скрипт"))
self.btn_start.clicked.connect(self.finder)
self.btn_clear.setText(_translate("MainWindow", "Очистить"))
self.btn_clear.clicked.connect(self.text.clear)
self.btn_save.setText(_translate("MainWindow", "Сохранить"))
self.btn_save.clicked.connect(self.save_file)
def open_file(self):
"""
Change current directory to the folder of user's choice for further use by the main script. Display selected
directory in the text browser
"""
folder = QtWidgets.QFileDialog.getExistingDirectory(None, "Выберите директорию")
if folder:
self.text.append("Открыта директория {}".format(folder))
self.btn_start.setEnabled(True) # enable "Запустить скрипт" button to work with inner images
os.chdir(folder)
QtWidgets.qApp.processEvents()
def finder(self):
"""
Open all filtered images in chosen directory and store them in the list. If there are images in correct format,
launch check_average and use returned images list to calculate the vignette centers (theorized to correlate
with mass center of the image) and vignette coefficients as polynomial of pixels brightness approximation.
Format and store collected data in lists as strings. Check if coefficients exceed proposed limits and display
warnings in the text browser
"""
allfiles = os.listdir(os.getcwd()) # get all filenames in the current directory
# Filter out everything but "img...tif" files and sort them by names
img_list = [filename for filename in allfiles if filename.startswith("img") and filename.endswith(".tif")]
img_list.sort()
if len(img_list) == 0:
self.text.append("Фотографии в директории отсутствуют")
pass
else:
note = "Найдены следующие изображения: "
for img in img_list: # display found images in the text browser
note += "{}, ".format(img)
note = note[:len(note)-2]
self.text.append(note)
QtWidgets.qApp.processEvents()
img_list, filtered = meta_filter(img_list)
note = "Следующие изображения не содержат необходимые теги и будут проигнорированы: {}".format(filtered)
self.text.append(note)
QtWidgets.qApp.processEvents()
meta_check, avg_arr, img_width, img_height, blacklevel = check_average(img_list)
if meta_check:
self.text.append("Метаданные изображений совпадают, поиск параметров...")
QtWidgets.qApp.processEvents()
for i in range(5):
# Calculating vignette center position which is supposed to be the brightest point in the image
note = ("Центр виньетирования для канала {}: ".format(i))
image = avg_arr[i].T
image = image - blacklevel
from scipy import ndimage
com = ndimage.center_of_mass(image) # center of mass calculation method
xc = int(com[0])
yc = int(com[1])
center = str(xc)+","+str(yc)
centers.append(center)
note += str(xc)+", "+str(yc)
self.text.append(note)
QtWidgets.qApp.processEvents()
note = "Коэффициенты полинома: "
# Calculating brightest pixel value. All images will be normalized to that value. Averaging 11x11
Vref = image[xc-5:xc+6, yc-5:yc+6].mean()
Vx = np.empty(img_width)
df_r = []
df_V = []
j = 0
k = 0
""" Following cycle computes each pixel to brighest pixel ratio, distance to vignetting center
and stores data in the lists to be used in the save_file function later """
while j < img_width:
while k < img_height:
Vx[j] = image[j, k] / Vref
if Vx[j] < 1.2:
r = ((j - xc) ** 2 + (k - yc) ** 2) ** (1 / 2)
df_r.append(r) # append radius to the processed pixel into dataframe
# append processed pixel value normalized to the brightest pixel value into dataframe
df_V.append(str(Vx[j]))
k = k + 1
k = 0
j = j + 1
df0 = np.vstack((df_r, df_V)).T
df0 = df0.astype(np.float64)
df0 = pd.DataFrame(df0, columns=['r', 'V']) # form a dataframe for approximation
from scipy.optimize import curve_fit
popt, pcov = curve_fit(poly6, df0['r'], df0['V']) # coefficients calculation using polynomial
err_arr = [] # list to store limit-exceeding coefficients
checks = str(popt)[1:-1].split() # format coefficients for check and further usage
checks.insert(2, "0")
checks.insert(4, "0")
checks = list(map(float, checks))
note += str(checks)
coefficient = ""
for j in range(len(checks)):
coefficient += str(checks[j])+","
coefficient = coefficient[:len(coefficient)-1]
coefficient = coefficient.replace("0.0,", "0,")
coefficients.append(coefficient) # append formatted coefficients into err_arr
if checks[0] < -10e-05 or checks[0] > 10e-05:
err_arr.append(0)
if checks[1] < -10e-07 or checks[1] > 10e-07:
err_arr.append(1)
if checks[3] < -10e-13 or checks[3] > 10e-13:
err_arr.append(3)
if checks[5] < -10e-19 or checks[5] > 10e-19:
err_arr.append(5)
note = note.replace("'", "")
note = note.replace("[", "")
note = note.replace("]", "")
if len(err_arr) != 0: # if limits are exceeded, display exceeding coefficient
note += "; потенциально некачественные: "
for err in err_arr:
note += str(checks[err]) + ", "
note = note[:len(note)-2]
self.text.append(note)
QtWidgets.qApp.processEvents()
self.text.append("Скрипт завершен, сохраните файлы конфигурации")
QtWidgets.qApp.processEvents()
self.btn_save.setEnabled(True) # enable "Сохранить" button
else:
self.text.append("Обнаружены несовпадения в метаданных изображений")
QtWidgets.qApp.processEvents()
def save_file(self):
"""
Change current directory to the folder of user's choice. Get vignette parameters from centers and coefficients
lists and format them into .json and .ini files templates. Save files into the selected directory
"""
folder = QtWidgets.QFileDialog.getExistingDirectory(None, "Выберите директорию")
if folder:
os.chdir(folder)
params = (centers[0], coefficients[0], centers[1], coefficients[1], centers[2], coefficients[2], centers[3],
coefficients[3], centers[4], coefficients[4])
json_string = '''
{
"Cams": {
"0": {
"central_wavelength": 470,
"band_name": "Blue",
"wavelength_fwhm": 28,
"fnumber": 1.8,
"band_sensitivity": 0.83,
"vignetting_center": [%s],
"vignetting_polynomial": [%s],
"radiometric_calibration": [0.000119266,0]
},
"1": {
"central_wavelength": 560,
"band_name": "Green",
"wavelength_fwhm": 20,
"fnumber": 1.8,
"band_sensitivity": 0.8,
"vignetting_center": [%s],
"vignetting_polynomial": [%s],
"radiometric_calibration": [0.000123596,0]
},
"2": {
"central_wavelength": 665,
"band_name": "Red",
"wavelength_fwhm": 14,
"fnumber": 1.8,
"band_sensitivity": 0.4,
"vignetting_center": [%s],
"vignetting_polynomial": [%s],
"radiometric_calibration": [0.000246559,0]
},
"3": {
"central_wavelength": 720,
"band_name": "Rededge",
"wavelength_fwhm": 12,
"fnumber": 1.8,
"band_sensitivity": 0.307,
"vignetting_center": [%s],
"vignetting_polynomial": [%s],
"radiometric_calibration": [0.000322352,0]
},
"4": {
"central_wavelength": 840,
"band_name": "NIR",
"wavelength_fwhm": 40,
"fnumber": 1.8,
"band_sensitivity": 0.73,
"vignetting_center": [%s],
"vignetting_polynomial": [%s],
"radiometric_calibration": [0.000135683,0]
}
}
}
''' % params
ini_string = '''
[Cam0]
central_wavelength=470
band_name=Blue
wavelength_fwhm=28
fnumber=1.8
band_sensitivity=0.83
vignetting_center=%s
vignetting_polynomial=%s
radiometric_calibration=0.000119266;0
[Cam1]
central_wavelength=560
band_name=Green
wavelength_fwhm=20
fnumber=1.8
band_sensitivity=0.8
vignetting_center=%s
vignetting_polynomial=%s
radiometric_calibration=0.000123596;0
[Cam2]
central_wavelength=668
band_name=Red
wavelength_fwhm=14
fnumber=1.8
band_sensitivity=0.4
vignetting_center=%s
vignetting_polynomial=%s
radiometric_calibration=0.000246559;0
[Cam3]
central_wavelength=720
band_name=Rededge
wavelength_fwhm=12
fnumber=1.8
band_sensitivity=0.307
vignetting_center=%s
vignetting_polynomial=%s
radiometric_calibration=0.000322352;0
[Cam4]
central_wavelength=840
band_name=NIR
wavelength_fwhm=40
fnumber=1.8
band_sensitivity=0.73
vignetting_center=%s
vignetting_polynomial=%s
radiometric_calibration=0.000135683;0
''' % params
import json
json_result = json.loads(json_string) # create json object from json_string
import _make_iterencode
json.encoder._make_iterencode = _make_iterencode._make_iterencode
indent = (2, None)
with open('tags.json', 'w') as f: # write json object as tags.json file
json.dump(json_result, f, indent=indent)
import configparser
config = configparser.ConfigParser(allow_no_value=True) # setup config parser to work write .ini file
config.read_string(ini_string.replace(',', ';'))
with open('tags.ini', 'w') as f: # write parsed ini_string as tags.ini file
config.write(f)
self.text.append("Файлы были сохранены в директорию {}".format(folder))
self.btn_start.setEnabled(False) # disable "Запустить скрипт" button until the new directory is opened
QtWidgets.qApp.processEvents()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv) # initialize Qt app with system arguments
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show() # display Qt GUI using Ui_MainWindow Class methods
sys.exit(app.exec_()) # finish the program once app is closed