-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateTimeLapse.py
More file actions
72 lines (49 loc) · 2.04 KB
/
CreateTimeLapse.py
File metadata and controls
72 lines (49 loc) · 2.04 KB
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
import os
import shutil
import subprocess
import sys
# Input assumption: a list of n filenames
# The first n-2 are the input images in order
# The second to last is the mask
# The last is the output gif
command_dir = "/work/timelapse/interp/build/" # Location of executables
reg_command = "ImageSimilarityRegistration"
interp_command = "InterpByOpticalFlow"
convert_command = "/usr/bin/convert" #imagemagick convert binary
convert_args = "-delay 10 -loop 0"
out_dir = "/work/timelapse/out/" # Location to write images
def CreateTimeLapse():
input_images = sys.argv[1:-2]
mask_image = sys.argv[-2]
out_image = sys.argv[-1]
reg_images = DoSimilarityRegistration(input_images,mask_image)
interp_images = DoOpticalFlow(reg_images)
CreateGif(interp_images,out_image)
# Register all images to the first one
def DoSimilarityRegistration(images,mask):
basename = os.path.basename(images[0])
name,ext = os.path.splitext(basename)
reg_images = [out_dir + name + "_reg" + ext]
shutil.copy(images[0],reg_images[0])
for i in range(1, len(images)):
name,ext = os.path.splitext(os.path.basename(images[i]))
out_file = out_dir + name + "_reg" + ext
subprocess.call([command_dir + reg_command, images[0], images[i], mask, out_file])
reg_images.append(out_file)
return reg_images
# Use optical flow to interpolate between registered images
def DoOpticalFlow(images):
interp_images = [images[0]]
for i in range(0, len(images)-1):
subprocess.call([command_dir + interp_command, images[i], images[i+1]])
name,ext = os.path.splitext(images[i])
for i in range(1,10):
interp_images.append(name + "_" + str(i) + ext)
interp_images.append(images[-1])
return interp_images
# Create animated gif from interpolated images
def CreateGif(images,out_image):
subprocess.call(convert_command + " " + convert_args + " " +
" ".join(images) + " " + out_dir + out_image, shell=True)
if __name__ == "__main__":
CreateTimeLapse()