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
Next revisionBoth sides next revision
documents:120206pyip_cooking:python_imagej_cookbook [2017/12/03 13:03] – [Automatic Brightness/Contrast Button] updated github repo link kotadocuments:120206pyip_cooking:python_imagej_cookbook [2018/12/18 22:50] – [Python + ImageJ, Fiji Cookbook] kota
Line 3: Line 3:
 This page was last edited at: ~~LASTMOD~~ This page was last edited at: ~~LASTMOD~~
  
 +For learning image processing using Fiji and Jython scripting, go to excellent tutorials written by Albert Cardona, such as [[https://www.ini.uzh.ch/~acardona/fiji-tutorial/ | here in his website]] or [[https://imagej.net/Jython_Scripting | here in ImageJ.net]]. The former is in a tutorial style so if you want to learn how to do scripting using Jython, that's the place where you go. 
 +
 +This page is like a cookbook: there are no details about how to do programming, but more centered on how to use Classes built in ImageJ and its plugins. This is because the style how each algorithm is implemented is not consistent (they are written by 1000 different people!) so it takes a while to figure out how to use them when we are writing a bioimage analysis workflow. Examples below show how they are actually used in Jython scripts, to save our time for reading the source code of each. 
 +
 +Other resources: 
 +
 +[[https://jython.readthedocs.io/en/latest/|The Jython book "The Definitive Guide to Jython"]]: it's saying that the book is a version from 2009, but the latest commit is in Oct. 2018. 
 ===== Jython Interpreter ===== ===== Jython Interpreter =====
 A way to run jython script from jython interpreter, interactively. A way to run jython script from jython interpreter, interactively.
Line 95: Line 102:
 print op.getDirectory()+ op.getFileName() print op.getDirectory()+ op.getFileName()
 </code> </code>
 +
 +==== Getting the directory where the curently opened image is stored ====
 +<code python:1>
 +from ij import IJ
 +
 +imp = IJ.getImage()
 +print imp.getOriginalFileInfo().directory
 +</code>
 +
 +... can also be done by ''%%IJ.getDirectory("image")%%'', but with this IJ method, one cannot specify target ImagePlus object. See also the [[https://github.com/imagej/imagej1/blob/master/ij/IJ.java#L1652-L1663|source code of IJ.getDirectory]]
 +
 +Be careful not to mix with the usage of ''%%getFileInfo%%''. This does not hold directory information. 
 +
  
 ==== Regular Expression to get meta information from file name ==== ==== Regular Expression to get meta information from file name ====
Line 772: Line 792:
 imps[0].show() # Channel 1 imps[0].show() # Channel 1
 imps[1].show() # Channel 2 imps[1].show() # Channel 2
 +</code>
 +
 +==== Channel Merge ====
 +
 +[Image > Color > Merge Channels...]
 +
 +<code python linenums:1>
 +from ij import ImagePlus
 +from ij.plugin import RGBStackMerge, RGBStackConverter
 +
 +impc1 = ImagePlus("path/to/image.tif")
 +impc2 = ImagePlus("path/to/image.tif")
 +
 +mergeimp = RGBStackMerge.mergeChannels([impc2, None, impc1, None, None, None, None], True)
 +
 +# convert the composite image to the RGB image
 +RGBStackConverter.convertToRGB(mergeimp)
 +
 +mergeimp.show()
 </code> </code>
 ==== Z projection ==== ==== Z projection ====
Line 972: Line 1011:
       - if the count exceeds "limit", then count becomes 0       - if the count exceeds "limit", then count becomes 0
       - if the count is larger than "threshold", then that value is the maximum.        - if the count is larger than "threshold", then that value is the maximum. 
 +
 +Explanation in human language: this algorithm first determines two values "limit" and "Threshold"
 +
 +"Threshold" is the parameter that actually sets the min and the max of the contrast curve for the automatic enhancement. Starting from the lowest pixel value (0) or the highest value (255), the algorithm determines the number of pixels with that pixel value. If the number exceeds 0.02% of total pixel number, then that pixel value becomes wither the min or the max for enhancing the contrast. For this decision to ignore background pixels, the algorithm also needs to have another decision determining the background pixel value. 
 +
 +"limit" actually is a fixed number for each image to decide which of black (0) or white (255) is the background. If the number of pixels with current pixel value is more than 10% of total pixel number, then that value is considered to be the background of the image. For example, when the number of pixels with the value of 0 is 15%, then pixels with 0 values are background. In the next step, if pixels with the value of 1 are still over 10% like 11%, then pixels with values <= 1 are background. When the background is white, then similar decision takes place in the second loop. 
  
 The Jython script shown below is the re-written version based on original Java code. You could see the Java code of method "autoadjust"  [[https://github.com/imagej/imagej1/blob/master/ij/plugin/frame/ContrastAdjuster.java#L780-L829|in the git repo]], line 780-829.  The Jython script shown below is the re-written version based on original Java code. You could see the Java code of method "autoadjust"  [[https://github.com/imagej/imagej1/blob/master/ij/plugin/frame/ContrastAdjuster.java#L780-L829|in the git repo]], line 780-829. 
Line 1303: Line 1348:
  
 <code python linenums:1> <code python linenums:1>
 +from ij import IJ, ImagePlus
 from ij.plugin.filter import ParticleAnalyzer as PA from ij.plugin.filter import ParticleAnalyzer as PA
 +from ij.measure import ResultsTable
 +
 imp = IJ.getImage() imp = IJ.getImage()
  
Line 1513: Line 1561:
 cm.setPointSize(3) cm.setPointSize(3)
 </code> </code>
-===== Plugin: LOCI BioFormats, Replacing OME-TIFF XML =====+===== Plugin: LOCI BioFormats ===== 
 + 
 +==== Importing CZI file ==== 
 + 
 +<code python linenums:1> 
 +from loci.plugins import BF 
 +from loci.plugins.in import ImporterOptions 
 + 
 +filepath = "/path/to/image/my.czi" 
 + 
 +# Options for Bioformats pluginincludeing the image path 
 +options = ImporterOptions() 
 +options.setOpenAllSeries(True) 
 +options.setShowOMEXML(False) 
 +options.setStitchTiles(False) 
 +options.setId(filepath) 
 +     
 +fullimps = BF.openImagePlus(options) 
 + 
 +#fullimps now holds multiple images contained within the czi file.  
 +# open the first one.  
 +fullimps[0].show() 
 +</code> 
 + 
 +See here for more on metadata parsing and so on: [[https://gist.github.com/ctrueden/6282856|bio-formats.py]] 
 + 
 +==== Replacing OME-TIFF XML ====
  
 <code python linenums:1> <code python linenums:1>
Line 1832: Line 1906:
     print p, resmap.get(p)     print p, resmap.get(p)
 </code> </code>
 +
 +===== Plugin: MiToBo h-dome transformation =====
 +
 +h-dome is useful for spot detection in a noisy background. [[https://www.researchgate.net/publication/261206082_A_new_approach_for_spot_detection_in_total_internal_reflection_fluorescence_microscopy | For example, see this reference]]. The example here uses Plugin MiToBo, a huge collection of various components. 
 +
 +<code python>
 +from de.unihalle.informatik.MiToBo.core.datatypes.images import MTBImage
 +from de.unihalle.informatik.MiToBo.morphology import HDomeTransform3D
 +from ij import IJ
 +
 +imp = IJ.getImage()
 +mtb = MTBImage.createMTBImage( imp.duplicate() )
 +hdome = HDomeTransform3D(mtb, 10.0)
 +hdome.runOp()
 +mtbdone = hdome.getResultImage()
 +imp2 = mtbdone.getImagePlus()
 +imp2.show()
 +
 +</code>
 +
 +
 ===== R: Multi-Peak fitting using R ===== ===== R: Multi-Peak fitting using R =====
  
documents/120206pyip_cooking/python_imagej_cookbook.txt · Last modified: 2022/10/16 07:12 by kota

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki