Skip to content

Instantly share code, notes, and snippets.

@philipbjorge
Created May 23, 2012 02:34
Show Gist options
  • Save philipbjorge/2772930 to your computer and use it in GitHub Desktop.
Save philipbjorge/2772930 to your computer and use it in GitHub Desktop.
JACoP Fiji Colocalization Automation
from ij import IJ
from ij import WindowManager as wm
from ij import ImagePlus
import time
def open_file_dialogue(window_title="", path_restriction=None):
"""
Opens a file dialogue and returns the path to the chosen file.
Returns None on an invalid file or dialogue cancellation (prints to log).
window_title will appear in the file dialogue window title.
path_restriction will restrict file choices to a certain directory
This is a blocking function.
"""
od = OpenDialog(window_title, path_restriction)
if od.getFileName():
return od.getDirectory() + od.getFileName()
else:
print "Dialogue cancelled or no File?"
return None
def open_img(filename):
"""
Opens and shows the selected image.
Returns the image on success, otherwise it returns None and
prints to the log.
"""
try:
img = IJ.openImage(filename)
img.show()
return img
except Exception:
print "Couldn't open ImagePlus of %s" % filename
return None
def init_files():
"""
Initializes our two image files for object colocalization.
Presents dialogues and shows the selected files.
Returns a pair - (dendrite_img, psd_img)
"""
fn_dendrite = open_file_dialogue("Choose your dendrite image (CaMKII extension)", None)
fn_psd = open_file_dialogue("Choose your marker image (PSD extension)", None)
dendrite_img = open_img(fn_dendrite)
psd_img = open_img(fn_psd)
return (dendrite_img, psd_img)
def clean_up(*args):
"""
Assumes these are all images.
Closes/Hides them from the viewport so as to not interfere
with future JACoP runs.
"""
for img in args:
img.hide()
img.changes = False # prevents a file save dialogue from popping up
img.close()
def fill_selection(img):
# select our image (for the autocrop macro)
IJ.selectWindow(img.getID())
ip = img.getProcessor()
ip.setRoi(img.getRoi())
ip.setColor(0) # sets to black
ip.fillOutside(img.getRoi())
IJ.run("Auto Crop")
img.updateAndDraw()
def dendrite_select_wait(dendrite_img):
"""
Non-Blocking call to wait for user to select a dendrite.
"""
IJ.setTool("freehand")
IJ.selectWindow(dendrite_img.getID())
WaitForUserDialog("Select your dendrite in your CaMKII image.").show()
def crop_mask_selection(selected, other):
"""
Mask and crop out our images based on the selection in selected image.
"""
other.setRoi(selected.getRoi())
fill_selection(selected)
fill_selection(other)
def show_instructions():
WaitForUserDialog("Check only object based methods. Set Image A to PSD and Image B to CaMKII. Set the appropriate thresholds. Finally, in obj. tab set geometrical centre, show full table, and work on centres-particle coincidence.").show()
def __main__():
dendrite_img, psd_img = init_files()
# TODO: Image subtraction step with calibration image
# Get a selection
# TODO: Automatically highlight dendrite candidates and click to select.
# Treat each dendrite selected as a seperate image.
dendrite_select_wait(dendrite_img)
crop_mask_selection(dendrite_img, psd_img)
# TODO: Supply possible params here
# thresholding should be manual
jacop = IJ.runPlugIn("JACoP_", "")
show_instructions()
results = IJ.getLog()
IJ.append(results, "./output-%s.log" % (time.asctime( time.localtime(time.time()) )))
for im_id in wm.getIDList():
if "(Geometrical centres)" in wm.getImage(im_id).getWindow().getTitle():
IJ.save(wm.getImage(im_id), "./output-%s.tif" % (time.asctime( time.localtime(time.time()) )))
clean_up(dendrite_img, psd_img)
__main__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment