-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_tiffs.py
147 lines (114 loc) · 4.47 KB
/
make_tiffs.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
"""
Makes a series of tiff files from a cine
Or
Makes a series of tiff files from each cine in a directory (NOT IMPLEMENTED YET)
Dependencies:
- cine module
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from scipy import misc
import h5py
import subprocess
import argparse
import os
import sys
from tqdm import tqdm
from pymatbridge import Matlab
from PIL import Image
import cv2
sys.path.append('/Users/stephane/Documents/git/takumi/fapm/cine_local')
#----------LOCAL DEPENDENCES----------
import velocity as vel
import tflow.cine_local.cine.cine as cine # use local cine package
import tflow.cine_local.cine.tiff as tiff # use local cine package
# To find out where Matlab is installed, type matlabroot on Matlab.
faqm_dir = os.path.split(os.path.realpath(__file__))[0]
matlabdir = os.path.join(faqm_dir, 'matlab_codes')
def cine2tiff(cinepath, start=0, end=None, step=1, header='im', overwrite=False):
"""
Generate a list of tiff files extracted from a cinefile.
Different modes of processing can be used, that are typically useful for PIV processings :
test : log samples the i;ages, using the function test_sample. Default is 10 intervals log spaced, every 500 images.
Sample : standard extraction from start to stop, every step images, with an interval ctime between images A and B.
File : read directly the start, stop and ctime from a external file. Read automatically if the .txt file is in format :
'PIV_timestep'+cine_basename+.'txt'
INPUT
-----
file : str
filename of the cine file
mode : str.
Can be either 'test','Sample', 'File'
single : list of images specified
pair : pair of images, separated by a ctime interval
step : int
interval between two successive images to processed.
start : int. default 0
starting index
stop : int. default 0.
The cine will be processed 'till its end
ctime :
folder : str. Default '/Tiff_folder'
Name of the root folder where the images will be saved.
post : str. Default ''
post string to add to the title of the tiff folder name
OUTPUT
None
"""
cc = cine.Cine(cinepath)
cinepath_, ext = os.path.splitext(cinepath)
cinedir, cinename = os.path.split(cinepath_)
savedir = cinedir + '/Tiff_folder/' + cinename + '/'
# create a directory where images will be stored
if not os.path.exists(savedir):
os.makedirs(savedir)
if end is None:
end = len(cc)
indices_to_save = list(range(start, end, step))
for index in tqdm(indices_to_save):
# get frames from cine file
filename = savedir + header + '%05d.tiff' % index
# save only if the image does not already exist !
if not os.path.exists(filename) or overwrite:
# print (filename, index)
Tiff = tiff.Tiff(filename, 'w')
Tiff.close()
data = cc.get_frame(index)
im = Image.fromarray(data)
im.save(filename)
# misc.imsave(filename, data, 'tiff')
cc.close()
parser = argparse.ArgumentParser(description='Comprehensive interactive tool to analyze PIV cine')
parser.add_argument('-cine', '--cine', help='path to cine file')
parser.add_argument('-start', '--start', help='', default=0, type=int)
parser.add_argument('-end', '--end', help='', default=None, type=int)
parser.add_argument('-overwrite', '--overwrite', help='overwrite images', default=False)
args = parser.parse_args()
cinepath = args.cine
# DATA ARCHITECTURE
basedir, cinename = os.path.split(cinepath)
scriptdir, scriptname = os.path.split(os.path.realpath(__file__))
# OPEN A CINE FILE
cc = cine.Cine(args.cine)
time = cc.get_time_list()
# MAKE TIFFS FROM CINE
## 1. Make a multi-tiff file (using cine2tiff)
# cine2tiff_path = os.path.join(scriptdir, 'cine/cine2tiff')
# subprocess.call([cine2tiff_path, cinepath_sample])
## 2. Make n tiff files
cine2tiff(cinepath, start=args.start, end=args.end, overwrite=args.overwrite)
# ## Execute matlab script
# print matlabdir
# print os.path.exists(matlabdir + '/test2.m')
# mlab = Matlab(executable=matlab_path)
# mlabstart = mlab.start()
# # result = mlab.run_func(matlabdir + '/process_dir.m', {'Dirbase': basedir, 'codedir': matlabdir})
# # result = mlab.run_func(matlabdir + '/process_dir.m', {'arg1': 3, 'arg2': 5})
# result = mlab.run_func(matlabdir + '/test.m', {'arg1': 3, 'arg2': 5})
# print result['result']
# # mlab.quit()
#
#
cc.close()
# print time, len(time)