Blame view

python/imagestack.py 1.9 KB
4e0db0cd   David Mayerich   added imagestack ...
1
  import glob
4e0db0cd   David Mayerich   added imagestack ...
2
3
  import numpy
  import progressbar
2119be83   David Mayerich   updated libraries...
4
  import skimage.io
8cd258c6   David Mayerich   updated for 4-cha...
5
6
  import skimage.transform
  import os
4e0db0cd   David Mayerich   added imagestack ...
7
  
8cd258c6   David Mayerich   updated for 4-cha...
8
  def load(fmask, dtype=numpy.float32):
4e0db0cd   David Mayerich   added imagestack ...
9
10
11
12
13
14
15
      #read the input files
      F = glob.glob(fmask)
      if(len(F)==0):
          print("No image files found.")
          return
      
      #load the first file
2119be83   David Mayerich   updated libraries...
16
      I = skimage.io.imread(F[0])
4e0db0cd   David Mayerich   added imagestack ...
17
18
      
      #generate the image stack
8cd258c6   David Mayerich   updated for 4-cha...
19
20
21
22
      if I.ndim == 3:
      	S = numpy.zeros((len(F), I.shape[0], I.shape[1], I.shape[2]), dtype)
      else:
      	S = numpy.zeros((len(F), I.shape[0], I.shape[1]), dtype)
4e0db0cd   David Mayerich   added imagestack ...
23
24
25
26
      
      bar = progressbar.ProgressBar(max_value=len(F))
      
      for i in range(0, len(F)):
2119be83   David Mayerich   updated libraries...
27
          I = skimage.io.imread(F[i])
8cd258c6   David Mayerich   updated for 4-cha...
28
          S[i, ...] = I
4e0db0cd   David Mayerich   added imagestack ...
29
30
31
32
33
34
35
36
37
38
          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
  
8cd258c6   David Mayerich   updated for 4-cha...
39
  def save(I, fname, extension = "bmp"):
4e0db0cd   David Mayerich   added imagestack ...
40
41
42
43
      #output the final images
      bar = progressbar.ProgressBar(max_value=I.shape[0])
      #save the output files
      for i in range(0, I.shape[0]):        
2119be83   David Mayerich   updated libraries...
44
          skimage.io.imsave(fname + "%03d" % i + "." + extension, I[i, ...])
8cd258c6   David Mayerich   updated for 4-cha...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
          bar.update(i+1)
  
  #rescale a stack of images along X and Y by an integer value n and save to directory dest        
  def resize(fmask, dest, n, outformat=None):
      #read the input files
      F = glob.glob(fmask)
      if(len(F)==0):
          print("No image files found.")
          return
      
      #initialize a progress bar
      bar = progressbar.ProgressBar(max_value=len(F))
      
      #for each file
      for i in range(0, len(F)):
          
          #load the file to memory
2119be83   David Mayerich   updated libraries...
62
          I = skimage.io.imread(F[i])
8cd258c6   David Mayerich   updated for 4-cha...
63
64
          R = skimage.transform.downscale_local_mean(I, (n, n))
          if outformat is None:
2119be83   David Mayerich   updated libraries...
65
              skimage.io.imwrite(dest+"/"+os.path.basename(F[i]), R)
8cd258c6   David Mayerich   updated for 4-cha...
66
          else:
2119be83   David Mayerich   updated libraries...
67
              skimage.io.imwrite(dest+"/"+os.path.basename(F[i]).split('.')[0] + "." + outformat, R)
8cd258c6   David Mayerich   updated for 4-cha...
68
69
70
          bar.update(i+1)