Using ImageJ in a task type¶
ImageJ can be used in a task type if Core.Host.ImageJ
is installed.
For example Example.ImageJ.Segmentation
uses the ImageJ function Make Binary
:
The main script.py
contains the boilerplate XamFlow input and output handling,
and uses runutil.run()
to execute an ImageJ script:
import aimio
import xfworkerutil
import runutil
import numpy
worker = xfworkerutil.XFWorkerJob()
inputs = worker.job['inputs']
size = inputs['image']['size']
# Download input files
image_path = worker.download_input_file(inputs['image'])
raw_image_path = worker.create_related_path(image_path, '.raw')
raw_seg_path = worker.create_related_path(image_path, '_seg.raw')
seg_path = worker.create_related_path(image_path, '_seg.aim')
# Convert aim to raw
header, pixels, log = aimio.load_aim(str(image_path))
pixels.tofile(str(raw_image_path))
# Run ImageJ script
def run_imagej_script(filename, *args, **kwargs):
script_parameters = ','.join(args)
runutil.run("ImageJ", [
"ImageJ-win64.exe",
"--ij2",
"--headless",
"--console",
"--run", filename, script_parameters,
], **kwargs)
run_imagej_script("seg.py",
f"raw_image_path='{raw_image_path}'",
f"raw_seg_path='{raw_seg_path}'",
f"sizex='{size[0]}'",
f"sizey='{size[1]}'",
f"sizez='{size[2]}'")
# Convert RAW uint8 (0 or 255) to AIM int8 (0 or 127)
seg_pixels = numpy.fromfile(str(raw_seg_path), dtype=numpy.uint8)
seg_pixels = seg_pixels.clip(max=127).astype(dtype=numpy.int8)
seg_pixels = seg_pixels.reshape(pixels.shape)
seg_log = aimio.ProcessingLog()
seg_log.append_min_max_data_values(0, 127)
seg_log.append_parameter_seg()
aimio.save_aim(str(seg_path), seg_pixels, log=seg_log)
# Upload output files
worker.upload_output_file(seg_path)
# Save outputs
outputs = {
"segments": worker.create_output_image(seg_path.name, size),
}
worker.finish(outputs)
The seg.py
script contains the ImageJ commands:
#@String raw_image_path
#@String raw_seg_path
#@String sizex
#@String sizey
#@String sizez
from ij import IJ
IJ.run("Raw...",
"open=" + raw_image_path +
" image=[16-bit Signed]" +
" width=" + sizex +
" height=" + sizey +
" number=" + sizez +
" little-endian");
imp = IJ.getImage()
IJ.run(imp, "Make Binary",
"method=Default" +
" background=Default"
" calculate");
IJ.saveAs(imp, "Raw Data", raw_seg_path);