// This macro will use Pairwise Stitching to merge mosaic z-stack timelapses into movies. // This works on one slice at a time xTiles = 2; yTiles = 2; numFrames = 1; numSlices = 3; directory = '\\\\129.98.104.245\\d$\\Images\\Dave\\2015.11.24 - Survival Lung Window POD 1\\02 5x5 Mosaic'; baseFileName = "02 5x5 Mosaic"; overlapPixels = 100; xBaseShift = 379.72928; yBaseShift = 399.19302; //Loop through all of the slices of the mosaic for(sliceCount=1; sliceCount<=numSlices; sliceCount++) { //Loop through all of the frames of the mosaic numTiles = xTiles * yTiles; currentSlice = IJ.pad(sliceCount,4); print("Current slice: " + currentSlice); for(frameCount=1; frameCount<=numFrames; frameCount++) { currentFrame = IJ.pad(frameCount,3); for (yCount=1; yCount<=yTiles; yCount++) { yShift = yBaseShift * (yCount - 1); for(xCount=1; xCount<=xTiles; xCount++) { xShift = xBaseShift * (xCount - 1); // Determine which image to open. currentTile = IJ.pad(xCount+((yCount-1)*xTiles),3); //Load up the Red, Green and Blue channels of the image open(directory + "\\" + baseFileName + "-" + currentFrame + "-Red-" + currentTile + "-" + currentSlice + ".tif"); rename("Red"); open(directory + "\\" + baseFileName + "-" + currentFrame + "-Green-" + currentTile + "-" + currentSlice + ".tif"); rename("Green"); open(directory + "\\" + baseFileName + "-" + currentFrame + "-Blue-" + currentTile + "-" + currentSlice + ".tif"); rename("Blue"); run("Merge Channels...", "c1=Red c2=Green c3=Blue create"); selectWindow("Composite"); rename("Tile-" + currentTile); getDimensions(width, height, channels, slices, frames); if(xCount==1) { rename("Tile-" + IJ.pad(1+((yCount-1)*xTiles),3)); } else { selectWindow("Tile-" + IJ.pad(1+((yCount-1)*xTiles),3)); getDimensions(width1, height1, channels1, slices1, frames1); makeRectangle(width1-overlapPixels, 0, overlapPixels, height); selectWindow("Tile-" + currentTile); makeRectangle(0, 0, overlapPixels, height); run("Pairwise stitching", "first_image=Tile-" + IJ.pad(1+((yCount-1)*xTiles),3) + " second_image=Tile-" + currentTile + " fusion_method=[Max. Intensity] fused_image=Fused check_peaks=5 ignore x=&xShift y=0 registration_channel_image_1=[Average all channels] registration_channel_image_2=[Average all channels]"); selectWindow("Tile-" + IJ.pad(1+((yCount-1)*xTiles),3)); close(); selectWindow("Tile-" + currentTile); close(); selectWindow("Fused"); rename("Tile-" + IJ.pad(1+((yCount-1)*xTiles),3)); } } selectWindow("Tile-" + IJ.pad(1+((yCount-1)*xTiles),3)); rename("Row-" + IJ.pad(yCount,2)); if(yCount>1) { selectWindow("Row-01"); getDimensions(width, height, channels, slices, frames); makeRectangle(0, height-overlapPixels, width, overlapPixels); selectWindow("Row-" + IJ.pad(yCount,2) ); makeRectangle(0, 25, width, overlapPixels); run("Pairwise stitching", "first_image=Row-" + IJ.pad(1,2) + " second_image=Row-" + IJ.pad(yCount,2) + " fusion_method=[Max. Intensity] fused_image=Fused check_peaks=5 ignore x=0 y=&yShift registration_channel_image_1=[Average all channels] registration_channel_image_2=[Average all channels]"); selectWindow("Row-01"); close(); selectWindow("Row-" + IJ.pad(yCount,2) ); close(); selectWindow("Fused"); rename("Row-01"); } } selectWindow("Row-01"); rename("Frame-" + currentFrame); if (frameCount==1) { selectWindow("Frame-" + currentFrame); rename("All Frames"); } else { selectWindow("All Frames"); getDimensions(width1, height1, channels1, slices1, frames1); selectWindow("Frame-" + currentFrame); getDimensions(width2, height2, channels2, slices2, frames2); newWidth = maxOf(width1, width2); newHeight = maxOf(height1, height1); selectWindow("All Frames"); run("Canvas Size...", "width=&newWidth height=&newHeight position=Top-Left zero"); selectWindow("Frame-" + currentFrame); run("Canvas Size...", "width=&newWidth height=&newHeight position=Top-Left zero"); concatFrameString = "image1='All Frames' " + "image2=Frame-" + currentFrame; run("Concatenate...", " title=[All Frames] " + concatFrameString + " image3=[-- None --]"); } } selectWindow("All Frames"); rename("Slice-" + currentSlice); run("Hyperstack to Stack"); if (sliceCount==1) { concatSliceString = "image1=Slice-" + currentSlice; } else { concatSliceString = concatSliceString + " image" + sliceCount + "=Slice-" + currentSlice; } } if(numSlices>1) { run("Concatenate...", " title=[&baseFileName] " + concatSliceString + " image" + numSlices+1 + "=[-- None --]"); } run("Stack to Hyperstack...", "order=xyctz channels=3 slices=" + currentSlice + " frames=" + numFrames + " display=Composite"); rename(baseFileName);