documents:120206pyip_cooking:python_imagej_cookbook
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| documents:120206pyip_cooking:python_imagej_cookbook [2025/10/05 17:59] – [ImagePlus Array] kota | documents: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. | ||
| - | < | + | < |
| import jarray | import jarray | ||
| import java.lang.Class | import java.lang.Class | ||
| Line 776: | Line 776: | ||
| </ | </ | ||
| - | " | + | " |
| + | |||
| + | ==== 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: | ||
| + | from jarray import zeros | ||
| + | from ij import ImagePlus | ||
| + | from ij.process import FloatProcessor | ||
| + | |||
| + | #generate 200 x 100 floating point matrix | ||
| + | matrix2D = [zeros(100, ' | ||
| + | |||
| + | # check the generated matrix | ||
| + | print(matrix2D) | ||
| + | # | ||
| + | rows = len(matrix2D) | ||
| + | cols = len(matrix2D[0]) | ||
| + | print(" | ||
| + | #rows 200 cols 100 | ||
| + | |||
| + | # instantiate FP image | ||
| + | fp = FloatProcessor(matrix2D) | ||
| + | |||
| + | #check created FP image | ||
| + | print(" | ||
| + | ImagePlus(' | ||
| + | </ | ||
| + | |||
| + | In short, as simple as: | ||
| + | <code python linenums: | ||
| + | from jarray import zeros | ||
| + | from ij import ImagePlus | ||
| + | from ij.process import FloatProcessor | ||
| + | |||
| + | matrix2D = [zeros(100, ' | ||
| + | ImagePlus(' | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| ==== Converting Java array types ==== | ==== Converting Java array types ==== | ||
| - | Sometimes we need to convert the type of Java array e.g. double[] to int[]. | + | Sometimes we need to convert the type of Java array e.g. double[] to int[]. |
| < | < | ||
| + | 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() |
| - | hist = jarray.zeros(len(histD), ' | + | print(type(histD[0])) |
| - | for i in range(len(histD)): | + | |
| - | hist[i] = int(histD[i]) | + | hist = map(int, histD) |
| + | histInt = jarray.array(hist, ' | ||
| + | print(type(histInt[0])) #Java int[] | ||
| </ | </ | ||
| Line 2581: | Line 2627: | ||
| The code below shows how to measure spherical 3D ROI in an image. We first create 3 3D spheres and then use them for measuring the mean pixel intensity of those 3D ROIs within a synthetic gradient 3D image. | The code below shows how to measure spherical 3D ROI in an image. We first create 3 3D spheres and then use them for measuring the mean pixel intensity of those 3D ROIs within a synthetic gradient 3D image. | ||
| - | <code:py> | + | <code py> |
| from ij import IJ, ImagePlus | from ij import IJ, ImagePlus | ||
| from mcib3d.geom import ObjectCreator3D | from mcib3d.geom import ObjectCreator3D | ||
| Line 2612: | Line 2658: | ||
| print "Obj3: ", | print "Obj3: ", | ||
| </ | </ | ||
| + | |||
| + | ===== Plugin: Ridge Detection ===== | ||
| + | |||
| + | [[https:// | ||
| + | [[https:// | ||
| + | |||
| + | <code python linenums: | ||
| + | from ij import IJ | ||
| + | from ij.process import ImageConverter | ||
| + | from ij.plugin.frame import RoiManager | ||
| + | from ij.gui import PolygonRoi | ||
| + | from de.biomedical_imaging.ij.steger import Lines, Position, Junctions, LinesUtil | ||
| + | from org.apache.commons.lang3.mutable import MutableInt | ||
| + | import jarray | ||
| + | import java.lang.Class | ||
| + | |||
| + | # parameter settings | ||
| + | sigma = 2.8 #estimated radius of structure | ||
| + | high = 4.0 #eigenvalue upper threshold | ||
| + | low = 2.0 #eigenvalue lower threshold | ||
| + | minLength = 0.0 #length filter | ||
| + | maxLength = 0.0 #length filter | ||
| + | doCorrectPosition = False | ||
| + | doEstimateWidth = False | ||
| + | doExtendLine = False | ||
| + | mode = LinesUtil.MODE_LIGHT #white signal with black back | ||
| + | |||
| + | |||
| + | # start processing | ||
| + | imp = IJ.getImage() | ||
| + | imp32=imp.duplicate() | ||
| + | ImageConverter(imp32).convertToGray32() | ||
| + | in_img = imp32.getProcessor() | ||
| + | |||
| + | cols = in_img.getWidth() | ||
| + | rows = in_img.getHeight() | ||
| + | |||
| + | imgpxls2 = in_img.getPixels() #alredy float | ||
| + | |||
| + | # prepare output data variables | ||
| + | contours = Lines(in_img.getSliceNumber()) | ||
| + | resultJunction = Junctions(in_img.getSliceNumber()) | ||
| + | hnum_cont = MutableInt() | ||
| + | |||
| + | # detection | ||
| + | p = Position() | ||
| + | p.detect_lines(imgpxls2, | ||
| + | hnum_cont, sigma, low, high, mode, doEstimateWidth, | ||
| + | doCorrectPosition, | ||
| + | |||
| + | # visualization of results | ||
| + | rm = RoiManager.getInstance() | ||
| + | if (rm is None): | ||
| + | rm = RoiManager() | ||
| + | else: | ||
| + | rm.reset() | ||
| + | for c in contours: | ||
| + | pr = PolygonRoi(c.getXCoordinates(), | ||
| + | c.getYCoordinates(), | ||
| + | PolygonRoi.POLYLINE) | ||
| + | rm.addRoi(pr) | ||
| + | rm.runCommand(imp, | ||
| + | |||
| + | </ | ||
| + | |||
documents/120206pyip_cooking/python_imagej_cookbook.1759687161.txt.gz · Last modified: 2025/10/05 17:59 by kota
