imagestack.py
1021 Bytes
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
import glob
import imageio
import numpy
import progressbar
def loadstack(fmask, dtype=numpy.float32):
#read the input files
F = glob.glob(fmask)
if(len(F)==0):
print("No image files found.")
return
#load the first file
I = imageio.imread(F[0])
#generate the image stack
S = numpy.zeros((len(F), I.shape[0], I.shape[1], I.shape[2]), dtype)
bar = progressbar.ProgressBar(max_value=len(F))
for i in range(0, len(F)):
I = imageio.imread(F[i])
S[i, :, :, :] = I
bar.update(i+1)
return S
def rgb2gray(rgb):
r, g, b = rgb[...,0], rgb[...,1], rgb[...,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
def savestack(I, fname, extension = ".bmp"):
#output the final images
bar = progressbar.ProgressBar(max_value=I.shape[0])
#save the output files
for i in range(0, I.shape[0]):
imageio.imwrite(fname + "%03d" % i + extension, I[i, :, :, :])
bar.update(i+1)