diff --git a/python/imagestack.py b/python/imagestack.py index 83da7f2..19441b1 100644 --- a/python/imagestack.py +++ b/python/imagestack.py @@ -2,8 +2,10 @@ import glob import imageio import numpy import progressbar +import skimage.transform +import os -def loadstack(fmask, dtype=numpy.float32): +def load(fmask, dtype=numpy.float32): #read the input files F = glob.glob(fmask) if(len(F)==0): @@ -14,13 +16,16 @@ def loadstack(fmask, dtype=numpy.float32): I = imageio.imread(F[0]) #generate the image stack - S = numpy.zeros((len(F), I.shape[0], I.shape[1], I.shape[2]), dtype) + 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) bar = progressbar.ProgressBar(max_value=len(F)) for i in range(0, len(F)): I = imageio.imread(F[i]) - S[i, :, :, :] = I + S[i, ...] = I bar.update(i+1) return S @@ -31,10 +36,35 @@ def rgb2gray(rgb): return gray -def savestack(I, fname, extension = ".bmp"): +def save(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) \ No newline at end of file + imageio.imwrite(fname + "%03d" % i + "." + extension, I[i, ...]) + 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 + I = imageio.imread(F[i]) + R = skimage.transform.downscale_local_mean(I, (n, n)) + if outformat is None: + imageio.imwrite(dest+"/"+os.path.basename(F[i]), R) + else: + imageio.imwrite(dest+"/"+os.path.basename(F[i]).split('.')[0] + "." + outformat, R) + bar.update(i+1) + + \ No newline at end of file diff --git a/stim/image/image.h b/stim/image/image.h index f85baca..9c9e42c 100644 --- a/stim/image/image.h +++ b/stim/image/image.h @@ -238,7 +238,7 @@ public: #ifdef USING_OPENCV - void from_opencv(unsigned char* buffer, size_t width, size_t height) { + void from_opencv_888(unsigned char* buffer, size_t width, size_t height) { allocate(width, height, 3); T value; size_t i; @@ -252,6 +252,22 @@ public: } } } + void from_opencv_8888(unsigned char* buffer, size_t width, size_t height) { + //std::cerr << "ERROR - this software is not yet enabled to handle RGBA images (alpha channel)." << std::endl; + //exit(1); + allocate(width, height, 4); + T value; + size_t i; + for (size_t c = 0; c < C(); c++) { //copy directly + for (size_t y = 0; y < Y(); y++) { + for (size_t x = 0; x < X(); x++) { + i = y * X() * C() + x * C() + (3 - c); + value = buffer[i]; + img[idx(x, y, c)] = value; + } + } + } + } #endif //Copy N data points from source to dest, casting while doing so template @@ -280,7 +296,9 @@ public: type_copy(cv_ptr, img, size()); //memcpy(img, cv_ptr, bytes()); if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving - from_opencv(cv_ptr, X(), Y()); + from_opencv_888(cv_ptr, X(), Y()); + if (C() == 4) + from_opencv_8888(cv_ptr, X(), Y()); #else stim::filename file(filename); if (file.extension() == "ppm") -- libgit2 0.21.4