Created
November 18, 2014 00:56
-
-
Save hirokai/77679742e744d7f663a9 to your computer and use it in GitHub Desktop.
Fiji script: Cropping by csv files of ROIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ij import IJ, ImagePlus, ImageStack, WindowManager | |
from ij.plugin import ContrastEnhancer,StackCombiner,ImageCalculator | |
from ij.process import ImageConverter, ImageProcessor | |
from time import sleep | |
import csv | |
folder = "/Volumes/Macintosh HD/Google Drive/Groves/Scope 7/20141103/05-A FC4 488 568/" | |
def crop(imp,x,y,w,h): | |
imp.setRoi(x,y,w,h) | |
ip = imp.getProcessor() | |
return ip.crop(),imp | |
def process(count,imp1,imp2, x, y, w, h): | |
output = folder+"out/test_"+str(count)+"_488.tiff" | |
ip,imp = crop(imp1,x,y,w,h) | |
ContrastEnhancer().stretchHistogram(ip,0.35) | |
imp = ImagePlus("488",ip) | |
IJ.save(imp,output) | |
ImageConverter(imp).convertToRGB() | |
ip = imp.getProcessor() | |
output = folder+"out/test_"+str(count)+"_561.tiff" | |
ip2,imp2 = crop(imp2,x,y,w,h) | |
ContrastEnhancer().stretchHistogram(ip2,0.35) | |
imp2 = ImagePlus("561",ip2) | |
IJ.save(imp2,output) | |
imp3 = ImageCalculator().run("Add create", imp,imp2); | |
st = ImageStack(w,h) | |
st.addSlice(ip) | |
st2 = ImageStack(w,h) | |
st2.addSlice(ip2) | |
st3 = ImageStack(w,h) | |
st3.addSlice(imp3.getProcessor()) | |
ip4 = StackCombiner().combineHorizontally(st,st2) | |
ip5 = StackCombiner().combineHorizontally(ip4,st3) | |
output = folder+"out/test_"+str(count)+"_merge.tiff" | |
IJ.save(ImagePlus("merge",ip5),output) | |
# Assume the CSV file has five columns: file id, x, y, width, height | |
infile = folder + 'rois.csv' | |
csvfile = open(infile, 'rb') | |
try: | |
# reader is used for reading a csv line by line. | |
reader = csv.reader(csvfile) | |
reader.next() # Skip the first row | |
count = 0 | |
prev = None | |
imp = None | |
imp2 = None | |
for row in reader: | |
if len(row) >= 5: | |
count += 1 | |
if count > 4: | |
pass | |
# break | |
if prev != row[0]: | |
IJ.run('Close All') | |
imp = IJ.openImage(folder + "fixed_"+row[0]+"_488.tif") | |
imp2 = IJ.openImage(folder + "fixed_"+row[0]+"_561.tif") | |
process(count,imp,imp2, *map(int, row[1:5])) | |
else: | |
process(count,imp,imp2, *map(int, row[1:5])) | |
prev = row[0] | |
# finally block is executed in the end no matter if an error occurs or not. | |
finally: | |
csvfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment