Commit 8cd258c6825fdb691a104db123e04ae2055985b6

Authored by David Mayerich
1 parent 4e0db0cd

updated for 4-channel images and OpenCV 3

Showing 2 changed files with 56 additions and 8 deletions   Show diff stats
python/imagestack.py
@@ -2,8 +2,10 @@ import glob @@ -2,8 +2,10 @@ import glob
2 import imageio 2 import imageio
3 import numpy 3 import numpy
4 import progressbar 4 import progressbar
  5 +import skimage.transform
  6 +import os
5 7
6 -def loadstack(fmask, dtype=numpy.float32): 8 +def load(fmask, dtype=numpy.float32):
7 #read the input files 9 #read the input files
8 F = glob.glob(fmask) 10 F = glob.glob(fmask)
9 if(len(F)==0): 11 if(len(F)==0):
@@ -14,13 +16,16 @@ def loadstack(fmask, dtype=numpy.float32): @@ -14,13 +16,16 @@ def loadstack(fmask, dtype=numpy.float32):
14 I = imageio.imread(F[0]) 16 I = imageio.imread(F[0])
15 17
16 #generate the image stack 18 #generate the image stack
17 - S = numpy.zeros((len(F), I.shape[0], I.shape[1], I.shape[2]), dtype) 19 + if I.ndim == 3:
  20 + S = numpy.zeros((len(F), I.shape[0], I.shape[1], I.shape[2]), dtype)
  21 + else:
  22 + S = numpy.zeros((len(F), I.shape[0], I.shape[1]), dtype)
18 23
19 bar = progressbar.ProgressBar(max_value=len(F)) 24 bar = progressbar.ProgressBar(max_value=len(F))
20 25
21 for i in range(0, len(F)): 26 for i in range(0, len(F)):
22 I = imageio.imread(F[i]) 27 I = imageio.imread(F[i])
23 - S[i, :, :, :] = I 28 + S[i, ...] = I
24 bar.update(i+1) 29 bar.update(i+1)
25 30
26 return S 31 return S
@@ -31,10 +36,35 @@ def rgb2gray(rgb): @@ -31,10 +36,35 @@ def rgb2gray(rgb):
31 36
32 return gray 37 return gray
33 38
34 -def savestack(I, fname, extension = ".bmp"): 39 +def save(I, fname, extension = "bmp"):
35 #output the final images 40 #output the final images
36 bar = progressbar.ProgressBar(max_value=I.shape[0]) 41 bar = progressbar.ProgressBar(max_value=I.shape[0])
37 #save the output files 42 #save the output files
38 for i in range(0, I.shape[0]): 43 for i in range(0, I.shape[0]):
39 - imageio.imwrite(fname + "%03d" % i + extension, I[i, :, :, :])  
40 - bar.update(i+1)  
41 \ No newline at end of file 44 \ No newline at end of file
  45 + imageio.imwrite(fname + "%03d" % i + "." + extension, I[i, ...])
  46 + bar.update(i+1)
  47 +
  48 +#rescale a stack of images along X and Y by an integer value n and save to directory dest
  49 +def resize(fmask, dest, n, outformat=None):
  50 + #read the input files
  51 + F = glob.glob(fmask)
  52 + if(len(F)==0):
  53 + print("No image files found.")
  54 + return
  55 +
  56 + #initialize a progress bar
  57 + bar = progressbar.ProgressBar(max_value=len(F))
  58 +
  59 + #for each file
  60 + for i in range(0, len(F)):
  61 +
  62 + #load the file to memory
  63 + I = imageio.imread(F[i])
  64 + R = skimage.transform.downscale_local_mean(I, (n, n))
  65 + if outformat is None:
  66 + imageio.imwrite(dest+"/"+os.path.basename(F[i]), R)
  67 + else:
  68 + imageio.imwrite(dest+"/"+os.path.basename(F[i]).split('.')[0] + "." + outformat, R)
  69 + bar.update(i+1)
  70 +
  71 +
42 \ No newline at end of file 72 \ No newline at end of file
stim/image/image.h
@@ -238,7 +238,7 @@ public: @@ -238,7 +238,7 @@ public:
238 238
239 239
240 #ifdef USING_OPENCV 240 #ifdef USING_OPENCV
241 - void from_opencv(unsigned char* buffer, size_t width, size_t height) { 241 + void from_opencv_888(unsigned char* buffer, size_t width, size_t height) {
242 allocate(width, height, 3); 242 allocate(width, height, 3);
243 T value; 243 T value;
244 size_t i; 244 size_t i;
@@ -252,6 +252,22 @@ public: @@ -252,6 +252,22 @@ public:
252 } 252 }
253 } 253 }
254 } 254 }
  255 + void from_opencv_8888(unsigned char* buffer, size_t width, size_t height) {
  256 + //std::cerr << "ERROR - this software is not yet enabled to handle RGBA images (alpha channel)." << std::endl;
  257 + //exit(1);
  258 + allocate(width, height, 4);
  259 + T value;
  260 + size_t i;
  261 + for (size_t c = 0; c < C(); c++) { //copy directly
  262 + for (size_t y = 0; y < Y(); y++) {
  263 + for (size_t x = 0; x < X(); x++) {
  264 + i = y * X() * C() + x * C() + (3 - c);
  265 + value = buffer[i];
  266 + img[idx(x, y, c)] = value;
  267 + }
  268 + }
  269 + }
  270 + }
255 #endif 271 #endif
256 //Copy N data points from source to dest, casting while doing so 272 //Copy N data points from source to dest, casting while doing so
257 template<typename S, typename D> 273 template<typename S, typename D>
@@ -280,7 +296,9 @@ public: @@ -280,7 +296,9 @@ public:
280 type_copy<unsigned char, T>(cv_ptr, img, size()); 296 type_copy<unsigned char, T>(cv_ptr, img, size());
281 //memcpy(img, cv_ptr, bytes()); 297 //memcpy(img, cv_ptr, bytes());
282 if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving 298 if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving
283 - from_opencv(cv_ptr, X(), Y()); 299 + from_opencv_888(cv_ptr, X(), Y());
  300 + if (C() == 4)
  301 + from_opencv_8888(cv_ptr, X(), Y());
284 #else 302 #else
285 stim::filename file(filename); 303 stim::filename file(filename);
286 if (file.extension() == "ppm") 304 if (file.extension() == "ppm")