Created
December 22, 2014 18:39
-
-
Save hirokai/36b79b62dd2b7a3983a6 to your computer and use it in GitHub Desktop.
MicroManager images: Time lapse stitching by NumPy and Scipy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make time lapse of stitched image. | |
# Uses Numpy, Scipy, and tifffile | |
import numpy as np # numpy | |
import tifffile # tifffile | |
import cv2 # opencv | |
from scipy.ndimage.interpolation import zoom # scipy | |
import json | |
original_side = 1024 | |
size = 128 # Side of one frame | |
folder = "/path/to/rootfolder/" | |
infolder = folder + 'path/to/subfolder' | |
outpath = "%sprocess/out.tiff" % (folder) | |
grid_x = 4 | |
grid_y = 4 | |
positions = [['2-Pos_%03d_%03d' % (x, y) for y in range(grid_y)] for x in range(grid_x)] | |
def read_metadata(): | |
d = {} | |
imgs = [] | |
for y, row in enumerate(positions): | |
for x, pos in enumerate(row): | |
if not pos in d: | |
d[pos] = {} | |
path = infolder + pos + '/metadata.txt' | |
with open(path, 'r') as f: | |
obj = json.load(f) | |
ks = obj.keys() | |
for fr in ks: | |
if fr != 'Summary': | |
time = obj[fr]['ElapsedTime-ms'] | |
fr_idx = obj[fr]['FrameIndex'] | |
d[pos][fr_idx] = time | |
imgpath = '%s%s/img_000%06d_RICM_000.tif' % (infolder, pos, fr_idx) | |
imgs.append((imgpath, x, y, time)) | |
imgs = sorted(imgs, key=lambda a: a[3]) | |
return d, imgs | |
def stitch(ims, res, path, x, y, time): | |
im = zoom(tifffile.imread(path), 1.0 * size / original_side) | |
ims[y][x] = im | |
yy = y * size | |
xx = x * size | |
res[yy:yy + size, xx:xx + size] = ims[y][x] | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
res2 = np.copy(res) | |
cv2.putText(res2, '%3d sec' % (round(1.0 * time / 1000)), (10, 40), font, 0.8, (255, 255, 255), 2) | |
return res2 | |
def main(): | |
max_process = 1000 | |
count = 0 | |
imgs = [] | |
ims = [[np.zeros((size, size), np.uint16)] * grid_x] * grid_y | |
stitched = np.zeros((size * grid_y, size * grid_x), np.uint16) | |
times, images = read_metadata() | |
for path, x, y, time in images: | |
count += 1 | |
if count > max_process: | |
break | |
print(count) | |
imgs.append(stitch(ims, stitched, path, x, y, time)) | |
print('Writing at: %s' % (outpath)) | |
imgs = np.array(imgs) | |
tifffile.imsave(outpath, imgs) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment