Difference between revisions of "ImageJ2 Python Scripts"

(Correct grammar, Add resources, categories and the Scripting template)
(Solve TODO on first script)
Line 13: Line 13:
 
# @ImageJ ij
 
# @ImageJ ij
 
# @Dataset data
 
# @Dataset data
 +
# @String(label="Projection Type",choices={"Max","Mean","Median","Min", "StdDev", "Sum"}) proj_type
  
# TODO : Find a way to use script parameters to let user choose between
 
# "max", "mean", "min", "median", etc.
 
  
 
from net.imagej.axis import Axes
 
from net.imagej.axis import Axes
 
from net.imagej.ops import Ops
 
from net.imagej.ops import Ops
 +
 +
 +
def proj_method(method):
 +
    return {
 +
        'Max': Ops.Stats.Max,
 +
        'Mean': Ops.Stats.Mean,
 +
        'Median': Ops.Stats.Median,
 +
        'Min': Ops.Stats.Min,
 +
        'StdDev': Ops.Stats.StdDev,
 +
        'Sum': Ops.Stats.Sum,
 +
    }.get(method, Ops.Stats.Max)
  
  
 
def main():
 
def main():
  
# Select which dimension to project
+
    # Select which dimension to project
z_dim = data.dimensionIndex(Axes.Z)
+
    z_dim = data.dimensionIndex(Axes.Z)
  
if z_dim == -1:
+
    if z_dim == -1:
print("Z dimension not found.")
+
        print("Z dimension not found.")
return
+
        return
  
if data.dimension(z_dim) == 1:
+
    if data.dimension(z_dim) < 2:
print("Z dimension has only one frame.")
+
        print("Z dimension has only one frame.")
return
+
        return
  
# Write the output dimensions
+
    # Write the output dimensions
projected_dimensions = [data.dimension(d) for d in range(0, data.numDimensions()) if d != z_dim]
+
    projected_dimensions = [data.dimension(d) for d in range(0, data.numDimensions()) if d != z_dim]
  
# Create the output image
+
    # Create the output image
z_projected = ij.op().create().img(projected_dimensions)
+
    z_projected = ij.op().create().img(projected_dimensions)
  
# Create the op and run it
+
    # Create the op and run it
max_op = ij.op().op(Ops.Stats.Max, data)
+
    proj_op = ij.op().op(proj_method(proj_type), data)
ij.op().transform().project(z_projected, data, max_op, z_dim)
+
    ij.op().transform().project(z_projected, data, proj_op, z_dim)
  
# Create a dataset
+
    # Create a dataset
z_projected = ij.dataset().create(z_projected)
+
    z_projected = ij.dataset().create(z_projected)
  
# Set the correct axes (is that needed ?)
+
    # Set the correct axes (is that needed ?)
axes = [data.axis(d) for d in range(0, data.numDimensions()) if d != z_dim]
+
    axes = [data.axis(d) for d in range(0, data.numDimensions()) if d != z_dim]
z_projected.setAxes(axes)
+
    z_projected.setAxes(axes)
  
print(z_projected)
+
    print(z_projected)
ij.ui().show("z_projected", z_projected)
+
    ij.ui().show("z_projected", z_projected)
  
 
main()
 
main()
 +
 
</source>
 
</source>
  

Revision as of 14:01, 14 November 2016

Template:Scripting

Introduction

This page is a primer of ImageJ2 only Python scripts. It means that the examples included here avoid IJ1 as much as possible, unless it's really necessary.

Scripts

Z Projection

Do a Z projection.

# @ImageJ ij
# @Dataset data
# @String(label="Projection Type",choices={"Max","Mean","Median","Min", "StdDev", "Sum"}) proj_type


from net.imagej.axis import Axes
from net.imagej.ops import Ops


def proj_method(method):
    return {
        'Max': Ops.Stats.Max,
        'Mean': Ops.Stats.Mean,
        'Median': Ops.Stats.Median,
        'Min': Ops.Stats.Min,
        'StdDev': Ops.Stats.StdDev,
        'Sum': Ops.Stats.Sum,
    }.get(method, Ops.Stats.Max)


def main():

    # Select which dimension to project
    z_dim = data.dimensionIndex(Axes.Z)

    if z_dim == -1:
        print("Z dimension not found.")
        return

    if data.dimension(z_dim) < 2:
        print("Z dimension has only one frame.")
        return

    # Write the output dimensions
    projected_dimensions = [data.dimension(d) for d in range(0, data.numDimensions()) if d != z_dim]

    # Create the output image
    z_projected = ij.op().create().img(projected_dimensions)

    # Create the op and run it
    proj_op = ij.op().op(proj_method(proj_type), data)
    ij.op().transform().project(z_projected, data, proj_op, z_dim)

    # Create a dataset
    z_projected = ij.dataset().create(z_projected)

    # Set the correct axes (is that needed ?)
    axes = [data.axis(d) for d in range(0, data.numDimensions()) if d != z_dim]
    z_projected.setAxes(axes)

    print(z_projected)
    ij.ui().show("z_projected", z_projected)

main()

Apply Threshold

# @ImageJ ij
# @Dataset data

method_threshold = "otsu"

# Get the histogram
histo = ij.op().run("image.histogram", data.getImgPlus())

# Get the threshold
threshold = ij.op().run("threshold.%s" % method_threshold, histo)

# Apply the threshold
thresholded = ij.op().run("threshold.apply", data.getImgPlus(), threshold)

# Create output
thresholded = ij.dataset().create(thresholded)

ij.ui().show("thresholded", thresholded))

A more direct way if you don't need to modify the threshold is:


# @ImageJ ij
# @Dataset data

method_threshold = "otsu"

# Apply the threshold
thresholded = ij.op().run("threshold.%s" % method_threshold, data.getImgPlus())

# Create output
thresholded = ij.dataset().create(thresholded)

ij.ui().show("thresholded", thresholded)

Resources