Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Open a stack
- # get number of slices in stack (s)
- # ask for window size (w)
- # ask for directory (d_a) for substack sequence
- # ask for directory (d_b) for z-projection sequence
- # iterate (i) from (w) to (s-w)
- # Things to do here:
- # -make a substack from (i-(w/2)) to (i+(w/2))
- # -save substack in (d_a)
- # iterate (i) on sequence of substacks (w) to (s-w) in (d)
- # -do a z projection on each substack
- # -save z projection in (d_b)
- #
- # Or, if stack is already split into images:
- # Ask for directory containing images (d_a)
- # Ask for directory (d_b) for z-projection sequence
- # Determine number of images (s) in (d_a)
- # ask for window size (w)
- # ask for directory (d_b) for z-projection sequence
- # iterate (i) from (w) to (s-w)
- # Things to do here:
- # -make a substack from (i-(w/2)) to (i+(w/2))
- # -do a z projection on substack
- # -save z projection in (d_b)
- import os
- from ij.io import OpenDialog
- from ij.io import DirectoryChooser
- from ij.plugin import ZProjector
- from ij import IJ, ImagePlus, VirtualStack, ImageStack, WindowManager
- from ij import Menus
- from ij.gui import GenericDialog
- from java.awt import FileDialog
- def run():
- # Choose a source directory
- srcDir = DirectoryChooser("Choose source directory").getDirectory()
- if srcDir is None:
- #if this is true, indicates that cancel was clicked, returns from function
- return
- # Prints source directory to log
- IJ.log("Directory: " + srcDir)
- # Prompts user to choose target directory
- targetdir = DirectoryChooser("Choose target dictionary").getDirectory()
- if targetdir is None:
- #returns, same as for source directory
- return
- # Make a preferences dialog box
- gd = GenericDialog('Preferences')
- #initialize window
- window = 0
- #add a numeric field for the window
- gd.addNumericField('Window: ', window, 0)
- #initialize z_proj_choice
- z_proj_choice = ''
- # make a list of Z projection types
- z_proj_types = ['projection=[Average Intensity]','projection=[Max Intensity]','projection=[Min Intensity]','projection=[Sum Slices]','projection=[Standard Deviation]','projection=[Median]']
- # add menu item for z projection types
- gd.addChoice('Z-projection type: ', z_proj_types, 'Average')
- #show dialog box
- gd.showDialog()
- #if user cancels dialog, return
- if gd.wasCanceled():
- IJ.log('User canceled dialog.')
- return
- #put interger form of user input in numeric field into window variable
- window = int(gd.getNextNumber())
- #put z projection choice into z_proj_choice variable
- z_proj_choice = gd.getNextChoice()
- #display window in log box
- IJ.log("Window: " + str(window))
- # display z projection choice in IJ log
- IJ.log("Z projection type: " + z_proj_choice)
- #walks through source directory: determines number of files, makes list of files
- walk_result = os.walk(srcDir)
- lst_walk_result = list(walk_result)
- files = lst_walk_result[0][2]
- number_of_files = len(lst_walk_result[0][2])
- IJ.log("Number of files in source directory: " + str(number_of_files))
- #iterate over range (window/2), number of files-(window/2):
- for i in range(window, (number_of_files - window + 1)):
- #for each i, open files (i-(w/2)) to (i+(w/2)) indexed in files list
- for n in range((i - window), (i + window)):
- path = os.path.join(srcDir, files[n])
- img = IJ.openImage(path)
- img.show()
- IJ.run("Images to Stack", "stack")
- IJ.run("Z Project...", z_proj_choice)
- imp = IJ.getImage()
- IJ.saveAs(imp, "PNG", targetdir + str(i-(window/2)))
- IJ.run("Close All")
- run()
Add Comment
Please, Sign In to add comment