-
Notifications
You must be signed in to change notification settings - Fork 4
/
temporal_median.py
285 lines (221 loc) · 9.97 KB
/
temporal_median.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
from __future__ import print_function
import argparse
import numpy
import os
import glob
import sys
from timeit import default_timer as timer
from multiprocessing import Process, Pool
from PIL import Image
import imageio
IMAGE_EXTENSIONS = ["*.tiff", "*.tif", "*.jpeg", "*.png", "*.jpg", "*.dng"]
MOVIE_EXTENSIONS = ["*.mp4", "*.mov"]
def get_frame_data(input_data, frame_number):
""" Gets the frame with the specified index """
if isinstance(input_data, list):
return Image.open(input_data[frame_number])
return input_data.get_data(frame_number)
def get_number_of_frames(input_data):
if isinstance(input_data, list):
return len(input_data)
return round(input_data.get_meta_data()['fps'] * input_data.get_meta_data()['duration']) -1
def make_a_glob(root_dir):
""" Creates a glob of images from specified path. Checks for JPEG, PNG, TIFF, DNG
Args:
root_dir (str): path to a video or directory of images
Returns:
(list, imageio.core.format.Reader): glob of images from input path
"""
# video file, return here
if os.path.isfile(root_dir):
return imageio.get_reader(root_dir)
# start hunting for an image sequence
if not os.path.exists(root_dir):
raise IOError("No such path: %s" % root_dir)
if not root_dir.endswith("/"):
root_dir += "/"
input_data = glob.glob(root_dir + "*.tif")
for ext in IMAGE_EXTENSIONS:
if len(input_data) == 0:
input_data = glob.glob(root_dir + ext)
if len(input_data) == 0:
input_data = glob.glob(root_dir + ext.upper())
if ext == IMAGE_EXTENSIONS[(len(IMAGE_EXTENSIONS)-1)] and len(input_data) == 0:
raise IOError("No images found in directory: %s" % root_dir)
else:
break
print("First image is: " + input_data[0])
print("Number of frames found: ", len(input_data))
return input_data
def get_frame_limit(limit_frames, globsize):
""" Determines a limit on the number of frames
Args:
limit_frames:
globsize:
Returns:
int: total frames to run TMF on
"""
if limit_frames != -1:
if globsize > limit_frames:
total_frames = limit_frames
print("Frames limited to ", limit_frames)
else:
print("Frame limit of ", limit_frames, "is higher than total # of frames: ", globsize)
total_frames = globsize
else:
total_frames = globsize
return total_frames
def make_output_dir(output_dir):
""" Creates uniquely-named new folder along specified path
Args:
output_dir: output path
Returns:
str: path to new folder
"""
if not output_dir.endswith("/"):
output_dir += "/"
output_path = output_dir
# **************************** make a new directory to write new image sequence ************************
slitscan_current = 0
while os.path.exists(output_path + "tmf" + str(slitscan_current) + "/"):
slitscan_current += 1
os.mkdir(output_path + "tmf" + str(slitscan_current) + "/")
frame_path = output_path + "tmf" + str(slitscan_current) + "/"
print("Made directory: ", frame_path)
return frame_path
def do_sizing(input_data):
if isinstance(input_data, list):
first = Image.open(input_data[0])
width, height = first.size
print("width is: ", width, " height is: ", height)
else:
return input_data.get_meta_data()['size']
return width, height
# handy code by Vladimir Ignatyev, found here: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
def progress(count, total, suffix=''):
""" Creates and displays a progress bar in console log.
Args:
count: parts completed
total: parts to complete
suffix: any additional descriptors
"""
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix))
sys.stdout.flush()
def temporal_median_filter_multi2(input_data, output_dir, limit_frames, output_format, frame_offset=8, simultaneous_frames=8):
"""
Uses multiprocessing to efficiently calculate a temporal median filter across set of input images.
DIAGRAM:
f.o = offset (you actually get 2x what you ask for)
s.o = simultaneous offset (cuz we do multiple frames at the SAME TIME)
randframes = we make some random frames for before/after so that we don't run out of frames to use
|_____________________total frames______________________|
randframes_----0 |--f.o----|s.o|---f.o---|
Args:
input_data: globbed input directory
output_dir: path to output
limit_frames: put a limit on the number of frames
output_format: select PNG, TIFF, or JPEG (default)
frame_offset: Number of frames to use for median calculation
simultaneous_frames: Number of frames to process simultaneously
Returns:
str: path to final frames
"""
start2 = timer()
frame_path = make_output_dir(output_dir)
width, height = do_sizing(input_data)
total_frames = get_frame_limit(limit_frames, get_number_of_frames(input_data))
median_array = numpy.zeros((frame_offset+simultaneous_frames+frame_offset, height, width, 3),numpy.uint8)
for frame in range(frame_offset):
median_array[frame, :, :, :] = numpy.random.randint(low=0, high=255, size=(height, width, 3))
# read all the frames into big ol' array
for frame_number in range(simultaneous_frames+frame_offset):
next_im = get_frame_data(input_data, frame_number)
next_array = numpy.array(next_im, numpy.uint8)
del next_im
median_array[frame_offset+frame_number, :, :, :] = next_array
del next_array
# |_____________________total frames______________________|
# randframes_----0 |--f.o----|s.o|---f.o---|
# whole_array = numpy.zeros((total_frames, height, width, 3), numpy.uint8)
p = Pool(processes=8)
current_frame = 0
filtered_array = numpy.zeros((simultaneous_frames, height, width, 3), numpy.uint8)
while current_frame < total_frames:
if current_frame == 0:
pass
else:
median_array = numpy.roll(median_array, -simultaneous_frames, axis=0)
for x in range(simultaneous_frames):
if (current_frame+frame_offset+x) > total_frames:
next_array = numpy.random.randint(low=0, high=255, size=(height, width, 3))
else:
next_im = get_frame_data(input_data, frame_offset+current_frame+x)
next_array = numpy.array(next_im, numpy.uint8)
median_array[frame_offset+frame_offset+x, :, :, :] = next_array
slice_list = []
for x in range(simultaneous_frames):
if (x+current_frame) > total_frames:
break
else:
slice_list.append(median_array[x:(x+frame_offset+frame_offset)])
# calculate medians in our multiprocessing pool
results = p.map(median_calc, slice_list)
for frame in range(len(results)):
filtered_array[frame, :, :, 0] = results[frame][0]
filtered_array[frame, :, :, 1] = results[frame][1]
filtered_array[frame, :, :, 2] = results[frame][2]
img = Image.fromarray(filtered_array[frame, :, :, :])
frame_name = frame_path + str(current_frame+frame) + "." + output_format
img.save(frame_name, format=output_format)
progress(current_frame, total_frames)
current_frame += simultaneous_frames
end2 = timer()
print("\nTotal Time was: %.02f sec. %.02f sec per frame." % (end2-start2, ((end2-start2)/total_frames)))
return frame_path
def median_calc(median_array):
return numpy.median(median_array[:, :, :, 0], axis=0), \
numpy.median(median_array[:, :, :, 1], axis=0), \
numpy.median(median_array[:, :, :, 2], axis=0)
def make_a_video(output_dir, output_format, name):
""" Use ffmpeg to make a video out of our cool frames
Args:
output_dir (str):
output_format (str):
name (str):
Returns:
None
"""
if not output_dir.endswith("/"):
output_dir += "/"
os.system('ffmpeg -r 24 -i ' + output_dir + '%d.' + output_format + ' -c:v libx264 ' + output_dir + name)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser._optionals.title = 'arguments'
parser.add_argument("-i", "--input_dir",
help="Input directory. Set of frames or a single video. Tests for frames first.", required=True)
parser.add_argument("-o", "--output_dir", default=os.getcwd(),
help="Path for output frames (optional) Default: Subdir of input)")
parser.add_argument("-offset", "--frame_offset", default=8, type=int,
help="Number of Frames to use for TMF (optional)")
parser.add_argument("-l", "--frame_limit", default=-1, type=int,
help="Limit number of frames to specified int (optional)")
parser.add_argument("-format", "--output_format", default="JPEG", help="Output image format. (optional)")
parser.add_argument("-simul", "--simultaneous_frames",type=int, default="8",
help="Number of frames to process on each iteration (faster performance using more cores)")
parser.add_argument("-v", "--video", action="store_true", default=False, dest="video",
help="Optional: Encode h.264 video of resulting frames. Defaults to False.")
args = parser.parse_args()
output_path = temporal_median_filter_multi2(
make_a_glob(args.input_dir),
args.output_dir,
args.frame_limit,
args.output_format,
args.frame_offset,
args.simultaneous_frames
)
if args.video:
make_a_video(output_path, args.output_format, "TMF.mp4")