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 [2025/10/05 20:45] – [Plugin: Ridge Detection] kotadocuments:120206pyip_cooking:python_imagej_cookbook [2025/10/05 22:53] (current) – [Converting Java array types] kota
Line 751: Line 751:
 To initialize a Java native 2D array (e.g. float[][]), create a Python 2D array first, then convert it to a Java 2D array using jarray. See the example code below.  To initialize a Java native 2D array (e.g. float[][]), create a Python 2D array first, then convert it to a Java 2D array using jarray. See the example code below. 
  
-<code>+<code python>
 import jarray import jarray
 import java.lang.Class import java.lang.Class
Line 776: Line 776:
 </code> </code>
  
-"java.lang.Class.forName("[F")" is the reflection, and the name of the Java native float class is "[F"+"java.lang.Class.forName("[F")" is the reflection, and the name of the Java native float class is "[F". 
 + 
 +==== Creating floating point processor image ==== 
 + 
 +Java float 2D array can also be made using jarray.zeros (like numpy.zeros).  
 +Here is an example of creating a floating-point image from 2D array.  
 + 
 +<code python linenums:1> 
 +from jarray import zeros 
 +from ij import ImagePlus 
 +from ij.process import FloatProcessor 
 + 
 +#generate 200 x 100 floating point matrix 
 +matrix2D = [zeros(100, 'f')]*200 
 + 
 +# check the generated matrix 
 +print(matrix2D) 
 +#[array('f', [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ... 
 +rows = len(matrix2D) 
 +cols = len(matrix2D[0]) 
 +print("rows {} cols {}".format(rows, cols)) 
 +#rows 200 cols 100 
 + 
 +# instantiate FP image 
 +fp = FloatProcessor(matrix2D) 
 + 
 +#check created FP image 
 +print("image width {} height {}".format(fp.getWidth(), fp.getHeight())) 
 +ImagePlus('fp', fp).show() 
 +</code> 
 + 
 +In short, as simple as: 
 +<code python linenums:1> 
 +from jarray import zeros 
 +from ij import ImagePlus 
 +from ij.process import FloatProcessor 
 + 
 +matrix2D = [zeros(100, 'f')]*200 
 +ImagePlus('fp', FloatProcessor(matrix2D)).show() 
 +</code> 
 + 
 + 
 + 
 ==== Converting Java array types ==== ==== Converting Java array types ====
  
-Sometimes we need to convert the type of Java array e.g. double[] to int[]. For this, there is no magic trick and need to run a loop. Below is a code fragment. +Sometimes we need to convert the type of Java array e.g. double[] to int[]. In Java we can do this by for-loop with casting from float to int, but in Jython we can use the map function 
  
 <code:py>  <code:py> 
 +from ij import IJ
 from ij.process import StackStatistics from ij.process import StackStatistics
 import jarray import jarray
  
 +imp = IJ.getImage() #stack
 stackstats = StackStatistics(imp) stackstats = StackStatistics(imp)
-histD = stackstats.histogram() +histD = stackstats.histogram() #double[] returned, taken as floats in python 
-hist = jarray.zeros(len(histD), 'i'+print(type(histD[0])) 
-for i in range(len(histD)): + 
-    hist[iint(histD[i])+hist = map(int, histD) 
 +histInt = jarray.array(hist, 'i'
 +print(type(histInt[0])) #Java int[]
 </code> </code>
  
Line 2675: Line 2721:
 rm.runCommand(imp, 'Show All') rm.runCommand(imp, 'Show All')
  
-<\code>+</code>
  
  
documents/120206pyip_cooking/python_imagej_cookbook.1759697110.txt.gz · Last modified: 2025/10/05 20:45 by kota

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki