-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
135 lines (105 loc) · 3.96 KB
/
utilities.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
import cv2 as ocv
import time_stamp as ts
import os
import re
import numpy as np
import tifffile as tiff
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# RECEIVES A LIST OF IMAGES AND WRITES IT AS A SEQUENCE #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def write_maps(maps, base_nom):
ts.start_function("writer")
written = []
for index, map in enumerate(maps):
nom = base_nom + "_" + str(index) + ".png"
written.append(nom)
maxi = map.max()
if (maxi > 1.0):
pass
else:
map = map * 255
entiers = map.astype(int)
ocv.imwrite(nom, entiers)
ts.tour("maps written", "writer")
def write_maps_tiff(maps, base_nom):
written = []
for index, map in enumerate(maps):
nom = base_nom + "__" + str(index) + "__" + ".tiff"
written.append(nom)
ocv.imwrite(nom, map.astype("float"))
return written
def write_maps_png(maps, base_nom):
written = []
for index, map in enumerate(maps):
nom = base_nom + "__" + str(index) + "__" + ".png"
written.append(nom)
ocv.imwrite(nom, map * 255)
del map
return written
def write_tiff(map, base_nom, index):
written = base_nom + "__" + str(index) + "__" + ".tiff"
tiff.imwrite(written, map.astype('float'))
return written
def write_png(map, base_nom, index):
written = base_nom + "__" + str(index) + "__" + ".png"
ocv.imwrite(written, map * 255)
return written
def rename_sequence(direc, seq_ini):
entries = os.listdir(direc)
detached = []
for entry in entries: # We save only the extension, in lower case
temp = entry.split('.')
temp[-1] = temp[-1].lower()
detached.append(temp[-1])
index = 0 # Future index of the files
for ext, entry in zip(detached, entries):
os.rename(direc+entry, direc+seq_ini+"-"+str(index)+"."+ext)
index += 1
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
def open_sequence(direc, raw=False):
try:
entries = os.listdir(direc)
sequence = []
entries.sort(key=natural_keys)
f_format = entries[0].split('.')
tf = (f_format[-1] == "tif") or (f_format[-1] == "tiff")
for entry in entries:
if tf:
img = tiff.imread(direc+entry)
img = ocv.cvtColor(img, ocv.COLOR_RGB2BGR)
if raw:
sequence.append(img)
else:
sequence.append(img / 255.0)
else:
if raw:
sequence.append(ocv.imread(direc+entry, -1))
else:
sequence.append(np.float32(ocv.imread(direc+entry, -1)) / 255.0)
print(direc+entry+" opened.")
return sequence
except FileNotFoundError:
return []
def reduce_res():
seq = open_sequence("testing7/")
shp = (int(seq[0].shape[1] / 3), int(seq[0].shape[0] / 3))
print(shp)
for idx, img in enumerate(seq):
buffer = ocv.normalize(ocv.resize(src=img, dsize=shp, fx=0.33, fy=0.33, interpolation=ocv.INTER_LINEAR), None, 0, 255, ocv.NORM_MINMAX, ocv.CV_8UC3)
ocv.imwrite("testing7/stitch_panel-"+str(idx)+".png", buffer)
def profile_through_mono(c, l, sequence, file):
val = []
for img in sequence:
val.append(int(img[c][l] * 255))
f = open(file + ".plt", "w")
for index, entry in enumerate(val):
f.write(str(index) + " " + str(entry)+"\n")
f.close()