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 revision Previous revision
Next revision
Previous revision
documents:120206pyip_cooking:python_imagej_cookbook [2017/12/03 06:08]
kota [Automatic Brightness/Contrast Button] human language explanation added
documents:120206pyip_cooking:python_imagej_cookbook [2018/12/12 15:28] (current)
kota [Python + ImageJ, Fiji Cookbook]
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 build 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 1309: 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 1519: 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 1838: 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.1512310080.txt.gz · Last modified: 2017/12/03 06:08 by kota