Skip to content

Instantly share code, notes, and snippets.

@hirokai
Last active August 29, 2015 14:10
Show Gist options
  • Save hirokai/c3b3a57bcc19a0829b9f to your computer and use it in GitHub Desktop.
Save hirokai/c3b3a57bcc19a0829b9f to your computer and use it in GitHub Desktop.
Fiji script: Crop and merge images by ROIs. Adds green and red colors to images with no LUT.
from ij import IJ, ImagePlus, ImageStack
from ij.process import ColorProcessor, ImageConverter
from ij.plugin import ContrastEnhancer, ImageCalculator, StackCombiner
import csv
folder = "/Volumes/Macintosh HD/Google Drive/Groves/Scope 7/20141126 Immunostaining/"
def in_path(folder,num):
return [folder + "01 FC4 of 1125/02 tirf and confocal/tirf_"+row[0]+"/Pos0/img_000000000_488tirf_100x_000.tif",
folder + "01 FC4 of 1125/02 tirf and confocal/tirf_"+row[0]+"/Pos0/img_000000000_561tirf_100x_000.tif"]
def out_path(folder,count):
return [folder+"01B 02 tirf/out_"+str(count)+"_488.tiff",
folder+"01B 02 tirf/out_"+str(count)+"_561.tiff",
folder+"01B 02 tirf/out_"+str(count)+"_merge.tiff"]
# Crop a single image
def crop(imp,x,y,w,h):
imp.setRoi(x,y,w,h)
ip = imp.getProcessor()
return ip.crop(),imp
# Return a cropped ImagePlus, color added, contrast adjusted.
def process_channel(imp,x,y,w,h,color,title):
cr,o = crop(imp,x,y,w,h);
imc = ImagePlus(title,cr)
ContrastEnhancer().stretchHistogram(imc,0.35)
IJ.run(imc,color,"")
return imc
def combine_multiple(w,h,imp):
res = ImageStack(w,h)
res.addSlice(imp[0].getProcessor())
for im in imp[1:]:
st = ImageStack(w,h)
st.addSlice(im.getProcessor())
res = StackCombiner().combineHorizontally(res,st)
return res
def process(count,imp,imp2,x,y,w,h):
f1,f2,f3 = out_path(folder,count)
crop488 = process_channel(imp,x,y,w,h,'Green','488')
IJ.save(crop488,f1)
crop561 = process_channel(imp2,x,y,w,h,'Red','561')
IJ.save(crop561,f2)
# Make a combined image of 488, 561, and merged.
ImageConverter(crop488).convertToRGB()
merge = ImageCalculator().run("Add create", crop488,crop561);
three = combine_multiple(w,h,[crop488,crop561,merge])
IJ.save(ImagePlus("merge",three),f3)
def get_images(path):
return IJ.openImage(path[0]), IJ.openImage(path[1])
# Assume the CSV file has five columns: file id, x, y, width, height
infile = folder + '01B rois.csv'
csvfile = open(infile, 'rU')
maxProcess = 1000
try:
# reader is used for reading a csv line by line.
reader = csv.reader(csvfile)
# Skip the title row
reader.next()
count = 0
prev = None
imp = None
for row in reader:
if len(row) >= 5:
count += 1
if count > maxProcess:
break
if prev != row[0]:
# This macro does not open files in GUI, and the line below is not needed.
# IJ.run('Close All')
path = in_path(folder,row[0])
print(path[0])
imp, imp2 = get_images(path)
prev = row[0]
process(count, imp, imp2, *map(int, row[1:5]))
print(count)
# finally block is executed in the end no matter if an error occurs or not.
finally:
csvfile.close()
# IJ.run('Close All')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment