-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_in_one.py
459 lines (378 loc) · 13 KB
/
all_in_one.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
import argparse, os, cv2, traceback
import numpy as np
from time import time
from images.images import (
loadImages,
filterLowContrast,
save_image,
shrink_images,
)
from denoise.denoise import denoiser
from sharpen.sharpen import sharpen
def setup_args():
parser = argparse.ArgumentParser(description="Process Image")
parser.add_argument("input_dir", help="Input directory of images")
parser.add_argument(
"--internal_image_extension",
default="png",
help="Extension of images to process",
)
parser.add_argument(
"--quality",
default=95,
type=int,
help="Quality of output images",
)
parser.add_argument("--single_image", help="Single image mode", action="store_true")
parser.add_argument(
"--histogram_method",
default="none",
help="histogram method to use",
choices=["histogram_clahe", "histogram_equalize"],
)
parser.add_argument(
"--clip_limit",
default=1.2,
type=float,
help="Clip limit for histogram_clahe",
)
parser.add_argument(
"--tile_grid_size",
default=8,
type=int,
help="Tile grid size for histogram_clahe",
)
parser.add_argument(
"--dehaze_method",
default="none",
help="Dehaze method to use on all images",
choices=["none", "darktables"],
)
parser.add_argument(
"--color_method",
default="none",
help="Color method to use on final images",
choices=["none", "image_adaptive_3dlut"],
)
parser.add_argument("--auto_stack", help="Auto stack images", action="store_true")
parser.add_argument(
"--stack_amount",
default=3,
type=int,
help="Amount of images to stack at a time",
)
parser.add_argument(
"--stack_method",
help="Stacking method ORB (faster) or ECC (more precise)",
choices=["ORB", "ECC"],
default="ECC",
)
parser.add_argument(
"--denoise_all",
help="Denoise all images prior to stacking",
action="store_true",
)
parser.add_argument(
"--denoise_all_method",
help="Denoise method for all images prior to stacking",
choices=["fast", "fddnet", "ircnn"],
default="fast",
)
parser.add_argument(
"--denoise_all_amount",
help="Denoise amount for all images prior to stacking",
type=int,
default=2,
)
parser.add_argument(
"--denoise",
help="Denoise image",
action="store_true",
)
parser.add_argument(
"--denoise_method",
help="Denoise image",
choices=["fast", "fddnet", "ircnn", "none"],
default="fast",
)
parser.add_argument("--denoise_amount", help="Denoise amount", type=int, default=2)
parser.add_argument(
"--super_resolution", help="Super resolution", action="store_true"
)
parser.add_argument(
"--super_resolution_method",
help="Super Resolution method",
choices=["ESPCN", "FSRCNN"],
default="ESPCN",
)
parser.add_argument(
"--super_resolution_scale",
help="Super Resolution Scale",
choices=[2, 4],
type=int,
default=2,
)
parser.add_argument("--shrink_images", help="Shrink image", action="store_true")
parser.add_argument(
"--scale_down",
type=int,
default=720,
help="Scale down image to the following resolution for stacking and filter contrast",
)
parser.add_argument(
"--parallel_raw",
type=int,
default=None,
help="Number of parallel pyraw processes to use",
)
parser.add_argument(
"--sharpen",
help="Sharpen the postprocess image",
choices=["filter_kernel", "unsharp_mask"],
type=str,
)
parser.add_argument(
"--sharpen_amount",
type=float,
default=1.0,
help="Sharpen amount for unsharp_mask",
)
parser.add_argument(
"--auto_white_balance",
help="Auto white balance the image",
action="store_true",
)
parser.add_argument(
"--half_size",
help="Half the size of the image",
action="store_true",
)
return parser.parse_args()
# Create main and do any processing if needed
def single_image(
image,
input_dir,
histogram_method,
clip_limit=1.2,
tile_grid_size=(8, 8),
image_extension="png",
quality=95,
denoise=False,
denoise_method="fast",
denoise_amount=2,
sharpen_method=None,
sharpen_amount=1.0,
):
if histogram_method != "none":
image = single_histogram_processing(
image, histogram_method, clip_limit, tile_grid_size
)
if denoise:
image = denoiser(image, denoise_method, denoise_amount)
if sharpen_method:
image = sharpen(image, sharpen_method, sharpen_amount)
output_image = os.path.join(input_dir, f"main.{image_extension}")
save_image(output_image, image, image_extension, quality)
print(f"Saved {output_image}")
def single_histogram_processing(
image, histogram_method, clip_limit=1.2, tile_grid_size=(8, 8)
):
"""
Equalize the histogram of a single image.
Parameters:
image (np.ndarray): The image to process.
histogram_method (str): The method to use for histogram enhancement.
Returns:
np.ndarray: The processed image.
"""
# Check if the image is a grayscale image or has multiple channels
if image.ndim == 2:
# If the image is grayscale, apply the histogram enhancement method directly
if histogram_method == "histogram_clahe":
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
image = clahe.apply(image)
elif histogram_method == "histogram_equalize":
image = cv2.equalizeHist(image)
else:
raise Exception("ERROR: Unknown histogram method")
else:
# If the image has multiple channels, convert it to the YUV color space
yuv_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
# Apply the histogram enhancement method to the Y channel
if histogram_method == "histogram_clahe":
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
yuv_image[:, :, 0] = clahe.apply(yuv_image[:, :, 0])
elif histogram_method == "histogram_equalize":
yuv_image[:, :, 0] = cv2.equalizeHist(yuv_image[:, :, 0])
else:
raise Exception("ERROR: Unknown histogram method")
# Convert the image back to the RGB color space
image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2RGB)
return image
def histogram_processing(
numpy_array, histogram_method, clip_limit=1.2, tile_grid_size=(8, 8)
):
"""
Equalize the histograms of the images in a numpy array.
Parameters:
numpy_array (np.ndarray): A numpy array containing the images to process.
histogram_method (str): The method to use for histogram enhancement.
Returns:
np.ndarray: A numpy array containing the processed images.
"""
print("Histogram equalizing images")
# Create a new numpy array to store the processed images
processed_array = np.empty(numpy_array.shape, dtype=numpy_array.dtype)
# Iterate over the images in the numpy array
for i, image in enumerate(numpy_array):
# Process the image
processed_array[i] = single_histogram_processing(
image, histogram_method, clip_limit, tile_grid_size
)
return processed_array
# ===== MAIN =====
def main(args):
# Flag to indicate if any processing was done on the image
processed_image = False
loading_tic = time()
image_folder = args.input_dir
# Load main image
numpy_images = loadImages(
image_folder,
args.parallel_raw,
args.half_size,
args.auto_white_balance,
)
# Filter low contrast images
numpy_images = filterLowContrast(numpy_images, args.scale_down)
print(f"Loaded {len(numpy_images)} images in {(time() - loading_tic)} seconds")
main_tic = time()
print("Creating main image")
single_image(
numpy_images[0],
args.input_dir,
args.histogram_method,
args.clip_limit,
(args.tile_grid_size, args.tile_grid_size),
args.internal_image_extension,
args.quality,
args.denoise,
args.denoise_method,
args.denoise_amount,
args.sharpen,
args.sharpen_amount,
)
print(f"Single image {time() - main_tic} seconds")
if args.shrink_images:
shrink_tic = time()
numpy_images = shrink_images(numpy_images)
processed_image = True
print(f"Shunk in {time() - shrink_tic} seconds")
if args.histogram_method != "none":
equalize_tic = time()
numpy_images = histogram_processing(
numpy_images,
args.histogram_method,
args.clip_limit,
(args.tile_grid_size, args.tile_grid_size),
)
processed_image = True
print(f"Histogram equalized in {time() - equalize_tic} seconds")
if args.denoise_all:
try:
denoise_all_tic = time()
from denoise.denoise import denoise_images
numpy_images = denoise_images(
numpy_images,
method=args.denoise_all_method,
amount=args.denoise_all_amount,
)
processed_image = True
print(f"Denoised all images in {time() - denoise_all_tic} seconds")
except Exception as e:
raise Exception(f"ERROR: Could not denoise all images\n{e}")
if args.auto_stack:
try:
stack_tic = time()
from stacking.stacking import stacker
image = stacker(
numpy_images, args.stack_amount, args.stack_method, args.scale_down
)
processed_image = True
print(f"Stacked images in {time() - stack_tic} seconds")
except Exception as e:
raise Exception(f"ERROR: Could not stack images\n{e}")
else:
image = numpy_images[0]
if args.sharpen:
print("Sharpen")
sharp_tic = time()
image = sharpen(image, args.sharpen, args.sharpen_amount)
processed_image = True
print(f"Sharpen image in {time() - sharp_tic} seconds")
if args.dehaze_method != "none":
dehaze_tic = time()
print("Dehazing images")
dehazed_images = []
from dehaze.dehaze import dehaze_image
image = dehaze_image(image, args.dehaze_method)
processed_image = True
print(f"Dehazed {len(dehazed_images)} images in {time() - dehaze_tic} seconds")
if args.denoise:
try:
denoise_tic = time()
denoiser(image, args.denoise_method, args.denoise_amount)
processed_image = True
print(f"Denoised image in {time() - denoise_tic} seconds")
except Exception as e:
raise Exception(f"ERROR: Could not denoise image\n{e}")
if args.color_method != "none":
try:
color_tic = time()
print("Color adjusting image")
if args.color_method == "image_adaptive_3dlut":
from color.image_adaptive_3dlut.image_adaptive_3dlut import (
image_adaptive_3dlut,
)
image = image_adaptive_3dlut(image, "sRGB")
processed_image = True
print(f"Color adjusted image in {time() - color_tic} seconds")
except Exception as e:
raise Exception(f"Error: Failed to adjuts color\n{e}")
if args.super_resolution:
from super_resolution.super_resolution import super_resolution
print("Super resolution")
super_tic = time()
image = super_resolution(
image, args.super_resolution_method, args.super_resolution_scale
)
processed_image = True
print(f"Super resolution image in {time() - super_tic} seconds")
if args.shrink_images:
from super_resolution.super_resolution import super_resolution
print("Resize shrunk image")
super_tic = time()
image = super_resolution(image, args.super_resolution_method, 2)
processed_image = True
print(f"Super resolution image in {time() - super_tic} seconds")
if processed_image:
process_tic = time()
print("Save process image")
output_image = os.path.join(
args.input_dir, f"main_processed.{args.internal_image_extension}"
)
save_image(output_image, image, args.internal_image_extension)
print(f"Saved {output_image} in {time() - process_tic}")
if __name__ == "__main__":
try:
total_tic = time()
args = setup_args()
main(args)
print(f"Total {time() - total_tic} seconds")
except Exception as error:
if isinstance(error, list):
for message in error:
print(message)
else:
print(error)
print(traceback.format_exc())