Skip to content

Instantly share code, notes, and snippets.

@kevin-keraudren
Created July 3, 2015 10:27
Show Gist options
  • Save kevin-keraudren/858fa70e71ca571ec682 to your computer and use it in GitHub Desktop.
Save kevin-keraudren/858fa70e71ca571ec682 to your computer and use it in GitHub Desktop.
ImageJ Jython: draft cell segmentation protocol
#!/usr/bin/env jython
# \begin{enumerate}
# \item Process $\rightarrow$ Gaussian Blur
# \item Process $\rightarrow$ Enhance Contrast (normalize \& equalize histogram)
# \item Image $\rightarrow$ Type $\rightarrow$ HSB stack
# \item Image $\rightarrow$ Stacks $\rightarrow$ Stack to images
# \item keep only the Hue image
# \item Process $\rightarrow$ Find maxima... (noise tolerance 60)
# \end{enumerate}
from ij import IJ, measure
from ij.plugin.filter import ParticleAnalyzer
from ij.plugin.frame import RoiManager
from ij.process import ImageProcessor
from java.lang import Double
import sys
# http://pacific.mpi-cbg.de/wiki/index.php/Jython_Scripting#Jython_tutorials_for_ImageJ
def cell_counter(stack_file):
img_stack = IJ.openImage( stack_file )
IJ.run( img_stack, "Smooth", "stack" )
IJ.run( img_stack, "Sharpen", "stack" )
IJ.setAutoThreshold(img_stack, "Default")
IJ.run( img_stack, "Convert to Mask", "calculate black")
img_stack.getProcessor().invert()
img_stack.show()
# Create a table to store the results
#table = measure.ResultsTable()
# Create a hidden ROI manager, to store a ROI for each blob or cell
#roim = RoiManager(True)
# Create a ParticleAnalyzer, with arguments:
# 1. options (could be SHOW_ROI_MASKS, SHOW_OUTLINES, SHOW_MASKS, SHOW_NONE, ADD_TO_MANAGER, and others; combined with bitwise-or)
# 2. measurement options (see [http://rsb.info.nih.gov/ij/developer/api/ij/measure/Measurements.html Measurements])
# 3. a ResultsTable to store the measurements
# 4. The minimum size of a particle to consider for measurement
# 5. The maximum size (idem)
# 6. The minimum circularity of a particle
# 7. The maximum circularity
stack = img_stack.getStack()
for i in range( 1,img_stack.getStackSize()+1):
stack.getProcessor(i).invert()
table = measure.ResultsTable()
pa = ParticleAnalyzer( ParticleAnalyzer.SHOW_OVERLAY_OUTLINES | ParticleAnalyzer.INCLUDE_HOLES,
measure.Measurements.AREA | measure.Measurements.AREA_FRACTION | measure.Measurements.MEAN,
table,
200,
Double.POSITIVE_INFINITY,
0.0,
1.0 )
#pa.setHideOutputImage(True)
pa.analyze(img_stack, stack.getProcessor(i))
#print table.getRowAsString(-1)
# k=0
# while (table.getColumnHeading(k)!= None):
# if table.columnExists(k):
# print k, table.getColumnHeading(k), table.getColumn(k)
# k += 1
if table.columnExists(0):
print i, len(table.getColumn(0))
# if pa.analyze(img_stack):
# print "All ok"
# else:
# print "There was a problem analyzing", img_stack
# IJ.run( img_stack, "Analyze Particles...", "size=200-Infinity circularity=0.00-1.00 show=Outlines include summarize record stack")
# IJ.run("Measurements...", "toto" + ".pts")
#return table
table = cell_counter(sys.argv[1])
# print table.getColumnHeadings()
# for row in table.getColumn(0):
# print row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment