-
Notifications
You must be signed in to change notification settings - Fork 12
/
average.py
81 lines (58 loc) · 1.95 KB
/
average.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
"""
Average a series of images iteratively.
# SCRIPT : average.py
# POURPOSE : Average a series of images.
# AUTHOR : Caio Eadi Stringari
# DATE : 21/04/2021
# VERSION : 1.0
"""
import argparse
from glob import glob
from natsort import natsorted
import numpy as np
from skimage.io import imread, imsave
from skimage.util import img_as_float64
from tqdm import tqdm
if __name__ == "__main__":
print("\nComputing average, please wait...\n")
# Argument parser
parser = argparse.ArgumentParser()
# input file
parser.add_argument("--input", "-i",
action="store",
dest="input",
required=True,
help="Input folder with images file.",)
parser.add_argument("--output", "-o",
action="store",
dest="output",
default="average.png",
required=False,
help="Output average image name.",)
args = parser.parse_args()
# main()
imlist = natsorted(glob(args.input + "/*"))
# assuming all images are the same size, get dimensions of first image
h, w, c = imread(imlist[0]).shape
N = len(imlist)
# create a numpy array of floats to store the average (assume RGB images)
arr = np.zeros((h, w, c), np.float64)
# progress bar
pbar = tqdm(total=N)
# build up average pixel intensities, casting each image as an array of
# floats
for i, im in enumerate(imlist):
# ignore files that are not images
try:
img = imread(im)
except Exception:
pass
imarr = img_as_float64(img)
arr = arr + imarr / N
pbar.update()
pbar.close()
# scale the values and cast to integers
new_arr = ((arr - arr.min()) *
(1 / (arr.max() - arr.min()) * 255)).astype('uint8')
# save the output
imsave(args.output, new_arr)