User Tools

Site Tools


documents:120206pyip_cooking:python_imagej_cookbook

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
documents:120206pyip_cooking:python_imagej_cookbook [2022/10/03 17:42] – [Threshold to create a mask (Binary)] kotadocuments:120206pyip_cooking:python_imagej_cookbook [2024/10/08 17:45] (current) – [Accessing multiple files to load image sequences] kota
Line 410: Line 410:
 </code> </code>
 ==== Accessing multiple files to load image sequences ==== ==== Accessing multiple files to load image sequences ====
 +
 +A simple way is to use glob package (file not loaded in this example).
 +<code python linenums:1>
 +import glob, os
 +from ij.io import DirectoryChooser 
 +
 +srcDir = DirectoryChooser("Choose!").getDirectory()
 +for filename in glob.glob(os.path.join(srcDir, "*.tif")):
 +print(os.path.basename(filename))
 +</code>
  
 Here is a cool script written by Christian Tischer for loading image series using file prefix as dictionary keys.  Here is a cool script written by Christian Tischer for loading image series using file prefix as dictionary keys. 
Line 787: Line 797:
 zip command creates a list of tuples from elements of arrays a and b. zip command creates a list of tuples from elements of arrays a and b.
  
 +==== Using Java8 Stream ====
 +
 +Stream-related syntax introduced from Java8 is useful for writing clear codes, but cannot be directly used in Jython. Below is a way to use StreamAPI, by introducing Jython classes implementing the Consumer interface. I took this idea from [[https://stackoverflow.com/questions/45417732/jython-function-to-java-consumer|here]]. For the Java Stream API, this page is useful: [[https://www.baeldung.com/java-8-streams|The Java 8 Stream API Tutorial]]
 +
 +<code python>
 +from java.util.Arrays import asList
 +from java.util.function import Predicate, Consumer, Function
 +from java.util.stream import Collectors
 +from java.util import Arrays
 +
 +
 +class jc(Consumer):
 +    def __init__(self, fn):
 +        self.accept=fn
 +
 +class jf(Function):
 +    def __init__(self, fn):
 +        self.apply = fn
 +
 +class jp(Predicate):
 +    def __init__(self, fn):
 +        self.test = fn
 +        
 +def jprint(x):
 + print x
 +
 +tt = ["a", "b", "c"]
 +c = Arrays.stream(tt).count()
 +print c
 +
 +print "forEach printing"
 +Arrays.stream(tt).forEach(jc(lambda x: jprint("="+x)))
 +
 +print "forEach printing parallelly"
 +Arrays.stream(tt).parallel().forEach(jc(lambda x: jprint("="+x)))
 +
 +print "has b?", Arrays.stream(tt).anyMatch(jp(lambda x: x=="b"))
 +print "has z?", Arrays.stream(tt).anyMatch(jp(lambda x: x=="z"))
 +
 +# convert to Java List<>
 +jtt = Arrays.asList(tt)
 +
 +jtt.stream().forEach(jc(lambda x: jprint("+"+x)))
 +</code>
 ===== Event Listener ===== ===== Event Listener =====
  
Line 1039: Line 1093:
 === Stack to Mask by a threshold value === === Stack to Mask by a threshold value ===
  
-Here is an example of using pixel array to binarize an 8-bit stack. +Here is an example script to create a mask from an 8-bit stack using intensity thresholding
 The threshold value is derived by the Otsu algorithm using the full stack histogram.  The threshold value is derived by the Otsu algorithm using the full stack histogram. 
  
Line 1057: Line 1111:
 stats = StackStatistics(imp1bin) stats = StackStatistics(imp1bin)
 histdouble = stats.histogram() histdouble = stats.histogram()
 +
 +# need this conversion from double to int
 histint = map(lambda x:int(x), histdouble) histint = map(lambda x:int(x), histdouble)
 th = Auto_Threshold.Otsu(histint) th = Auto_Threshold.Otsu(histint)
Line 1068: Line 1124:
 </code> </code>
  
-To do this by accessing the pixel array of the stack, here is the way. It takes a longer time, so this is just to show the technique to process by pixel values using float processor pixel array object. +To do this by accessing the pixel array of the stack, here is the way. It takes a longer time than above, so this is just to show the technique to process by pixel values using float processor pixel array object. 
  
 <code python> <code python>
Line 2335: Line 2391:
 ===== Plugin: MorphoLibJ ===== ===== Plugin: MorphoLibJ =====
  
 +Javadoc: [[http://ijpb.github.io/MorphoLibJ/javadoc/]]
 ==== Distance Transform Watershed 3D ==== ==== Distance Transform Watershed 3D ====
  
documents/120206pyip_cooking/python_imagej_cookbook.1664818945.txt.gz · Last modified: 2022/10/03 17:42 by kota

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki