Created
October 21, 2015 07:05
-
-
Save hirokai/7775e866a87761530d64 to your computer and use it in GitHub Desktop.
Fiji image processing common tools
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
# tools_fiji.py | |
from ij import ImagePlus, ImageStack, IJ | |
print('tools_fiji.py loaded.') | |
# Image load/save | |
# Open a list of files. | |
# Cause error if not all images have the same dimensions. | |
# return: ImagePlus | |
def load_images(paths): | |
ip = IJ.openImage(paths[0]).getProcessor() | |
w = ip.getWidth() | |
h = ip.getHeight() | |
stk = ImageStack(w, h) | |
for path in paths[1:]: | |
ip = IJ.openImage(path).getProcessor() | |
stk.addSlice(ip) | |
return stk | |
# return: ImagePlus | |
def load_image_folder(path): | |
from ij.plugin import FolderOpener | |
return FolderOpener().openFolder(path) | |
# return: ImagePlus | |
def load_avi(path, virtual=True): | |
from ij.plugin import AVI_Reader | |
return AVI_Reader().open(path, virtual) | |
# | |
# CSV files load/save. | |
# | |
# Loading a commma-separated file (CSV). | |
# path: Path to a file to save. | |
# delimiter: Delimiter of csv file. | |
# auto_skip: Skip n rows (including the first row), | |
# where n (>= 1) is the value of the first column in the first row. | |
def load_csv(path, delimiter=',', auto_skip=True): | |
import csv | |
f = open(path, 'rU') | |
rows = [] | |
reader = csv.reader(f, delimiter=delimiter) | |
skipping = int(reader.next()[0]) if auto_skip else 0 | |
for _ in range(skipping - 1): | |
reader.next() | |
for row in reader: | |
rows.append(row) | |
f.close() | |
return rows | |
def save_csv(path, rows, delimiter=','): | |
import csv | |
f = open(path, 'wb') | |
print('Writing: %s' % path) | |
writer = csv.writer(f, delimiter=delimiter) | |
for row in rows: | |
writer.writerow(row) | |
f.close() | |
# | |
# Image measurements | |
# | |
# ip: ImageProcessor | |
# Return: [L, A, B] values. | |
def lab_mean_in_roi(ip, roi): | |
ip.setRoi(*roi) | |
ip2 = ip.crop() | |
imp = ImagePlus("", ip2) | |
IJ.run(imp, "RGB to CIELAB", "") | |
imp2 = IJ.getImage() | |
st = imp2.getStack() | |
vs = [] | |
for i in [1, 2, 3]: | |
ip = st.getProcessor(i).convertToFloatProcessor() | |
stat = ip.getStatistics() | |
vs.append(stat.mean) | |
imp2.close() | |
return vs | |
# | |
# Slice operations | |
# | |
# imp: ImagePlus | |
# n: 1-based index of slice to get | |
# Return: ImageProcessor | |
def get_slice(imp, n): | |
return imp.getImageStack().getProcessor(n) | |
# imp: ImagePlus | |
# slices: a list of slice indices (1-based) | |
def remove_slices(imp, slices): | |
stk = imp.getStack() | |
slices = sorted(slices, reverse=True) | |
num_slices = stk.getSize() | |
for s in slices: | |
if s <= num_slices: | |
stk.deleteSlice(s) | |
return imp | |
# imp: ImagePlus | |
# slices: a list of slice indices (1-based) | |
def take_slices(imp, slices): | |
stk = imp.getStack() | |
num_slices = stk.getSize() | |
slices = diff(range(1, num_slices + 1), slices) | |
slices = sorted(slices, reverse=True) | |
remove_slices(imp, slices) | |
return imp | |
def map_slices(func, imp, slices=None): | |
stk = imp.getStack() | |
num_slices = stk.getSize() | |
if slices is None: | |
slices = range(1, num_slices + 1) | |
res = [] | |
for s in slices: | |
imp.setSlice(s) | |
res.append(func(imp)) | |
return res | |
# This is used in combination with map_slices. | |
# Return: a function that takes ImagePlus to return a line scan profile). | |
def linescan(x1, y1, x2, y2): | |
from ij.gui import Line, ProfilePlot | |
def f(imp): | |
line = Line(x1, y1, x2, y2) | |
imp.setRoi(line) | |
vs = ProfilePlot(imp).getProfile() | |
imp.killRoi() | |
return vs | |
return f | |
# This is used in combination with map_slices. | |
# Return: a function that takes ImagePlus to return a polyline scan profile). | |
def linescan_poly(xs, ys): | |
def f(imp): | |
from ij.gui import Line, Roi, ProfilePlot, PolygonRoi | |
if len(xs) > 1: | |
print(xs, ys) | |
line = PolygonRoi(xs, ys, len(xs), Roi.POLYLINE) | |
imp.setRoi(line) | |
else: | |
line = Line(xs[0], ys[0], xs[1], ys[1]) | |
imp.setRoi(line) | |
vs = ProfilePlot(imp).getProfile() | |
imp.killRoi() | |
return vs | |
return f | |
def remove_scale(imp): | |
IJ.run(imp, "Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); | |
def crop(imp, w, h, x, y): | |
IJ.run(imp, "Specify...", "width=%d height=%d x=%d y=%d" % (w, h, x, y)) | |
IJ.run(imp, "Crop", "") | |
return imp | |
# | |
# List functions | |
# | |
def transpose(xss): | |
return [list(i) for i in zip(*xss)] | |
def diff(a, b): | |
b = set(b) | |
return [aa for aa in a if aa not in b] | |
# | |
# Utility | |
# | |
def mayint(s): | |
try: | |
r = int(s) | |
except: | |
r = None | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment