Skip to content

Instantly share code, notes, and snippets.

@hirokai
Created November 29, 2014 00:51
Show Gist options
  • Save hirokai/11dd6cf2da0337e255e2 to your computer and use it in GitHub Desktop.
Save hirokai/11dd6cf2da0337e255e2 to your computer and use it in GitHub Desktop.
Fiji script: Crop confocal images (2 channels)
from ij import IJ,ImagePlus
from ij.process import ColorProcessor
from ij.plugin import ContrastEnhancer
import csv
folder = "/Volumes/Macintosh HD/Google Drive/Groves/Scope 7/20141126 Immunostaining/"
def crop(imp,x,y,w,h):
imp.setRoi(x,y,w,h)
st = imp.getStack()
n = st.getSize()
m = n / 2
return st.crop(x,y,0,w,h,n),st
def process(count,imp,x,y,w,h):
st,orig = crop(imp,x,y,w,h);
imp2 = ImagePlus("stack",st)
IJ.run(imp2,"Stack to Hyperstack...", "order=xyzct channels=2 slices=29 frames=1 display=Color");
imp3 = IJ.getImage()
imp3.setC(2);
ContrastEnhancer().stretchHistogram(imp3.getStack().getProcessor(1),0.35)
IJ.run(imp3,"Cyan","");
#imp3.show()
IJ.save(imp3,folder+"01A 02 confocal/out_"+str(count)+".tiff")
# Assume the CSV file has five columns: file id, x, y, width, height
infile = folder + '01A rois.csv'
csvfile = open(infile, 'rb')
maxProcess = 1000
try:
# reader is used for reading a csv line by line.
reader = csv.reader(csvfile)
reader.next() # Skip the title row
count = 0
prev = None
imp = None
for row in reader:
if len(row) >= 5:
count += 1
if count > maxProcess:
break
if prev != row[0]:
IJ.run('Close All')
path = folder + "01 FC4 of 1125/02 tirf and confocal/confocal_"+row[0]+"/Pos0"
print(path)
print(count)
IJ.run("Image Sequence...", "open=["+path+"] sort");
imp = IJ.getImage()
prev = row[0]
process(count, imp, *map(int, row[1:5]))
else:
print(count)
prev = row[0]
process(count, imp, *map(int, row[1:5]))
# 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