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 [2024/10/08 17:45] – [Accessing multiple files to load image sequences] kotadocuments:120206pyip_cooking:python_imagej_cookbook [2025/05/21 03:59] (current) 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'the place where you go. Recently, there is very good tutorial page for real beginners: [[https://learning.rc.virginia.edu/notes/fiji-scripting/|here (UVA Research Computing Learning Portal).]]  +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 on his website]] or [[https://imagej.net/Jython_Scripting | here on ImageJ.net]]. The former is in a tutorial styleso if you want to learn scripting using Jython, that's where you go. Recently, there has been perfect tutorial page for real beginners: [[https://learning.rc.virginia.edu/courses/fiji-image-processing/scripting/|here (UVA Research Computing Learning Portal).]]   
 + 
 +This page is like a cookbook: there are no details about how to do programming, but more centered on how to use the Classes built into ImageJ and its plugins. This is because the style 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 used in Jython scripts, to save us time for reading the source code of each.  
 + 
 +**HOW TO USE**: This page is not intended for reading from top to bottom. Just search this page (e.g. command-f or ctrl-f) for the term you are looking for. This cookbook is a single page, so this allows the full search of the whole book (a single-page "book"...)! 
  
-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:  Other resources: 
Line 11: Line 14:
 [[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.  [[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 a Jython script from the Jython interpreter, interactively.
 <code python> <code python>
 #simply do #simply do
Line 25: Line 28:
 </code> </code>
  
-Of course you could do this by clicking 'run' for scripts opened in script editor as well, but in this way, you could still interact with the objects created by the execfile.+Of courseyou could do this by clicking 'run' for scripts opened in script editor as well, but in this way, you could still interact with the objects created by the execfile.
  
-===== Singed/Unsigned value conversions =====+===== Signed/Unsigned value conversions =====
  
-Java normally uses signed values but images are generally not signed (for 8 bit16 bit images). For this reason, when you work with images as an single array, there will be some need for conversion between signed value to unsigned value, and vice versa. In Jython, one could use functions in [[http://www.jython.org/docs/library/struct.html|struct]]. Below is an example of converting an 8-bit pixel array extracted from image to unsigned 8-bit values.  +Java normally uses signed valuesbut images are generally not signed (for 8-bit and 16-bit images). For this reason, when you work with images as single array, there will be some need for conversion between signed values to unsigned values, and vice versa. In Jython, one could use functions in [[http://www.jython.org/docs/library/struct.html|struct]]. Below is an example of converting an 8-bit pixel array extracted from an image to unsigned 8-bit values.  
  
-<code python linenums:1>+<Code:py linenums>
 from ij import IJ from ij import IJ
 import struct import struct
Line 43: Line 46:
 pix = map(s2u8bit,signedpix) pix = map(s2u8bit,signedpix)
    
-#check that the conversion worked.  +Check that the conversion worked.  
-this example was made for binary image, to print only values 255  +This example was made for binary image, to print only values 255  
 for j in range(len(pix)): for j in range(len(pix)):
         curval = pix[j]         curval = pix[j]
Line 52: Line 55:
         else:         else:
             print curval             print curval
-</code>+</Code>
  
-===== Singed/Unsigned value conversions with bitwise & operator =====+===== Signed/Unsigned value conversions with bitwise & operator =====
  
-Here is another example of going back and forth between signed and unsigned values. The scripts load "blobs.gif" example image from NIH server, then replaces pixels with values between 50 and 200 to 0. A kind of density slicing workflow.  +Here is another example of going back and forth between signed and unsigned values. The scripts load "blobs.gif" example image from the NIH server, then replace pixels with values between 50 and 200 to 0. A kind of density slicing workflow.  
  
-<code python linenums:1>+<code:python linenums>
 from ij import IJ from ij import IJ
 import jarray import jarray
Line 88: Line 91:
  
 Same processing could be also done by replacing L7 to L23 by Same processing could be also done by replacing L7 to L23 by
-<code python linenums:1>+<code python nolinenums>
 for j in range(imp.getHeight()): for j in range(imp.getHeight()):
  
Line 116: Line 119:
 ==== Getting path to a file interactively ==== ==== Getting path to a file interactively ====
  
-<code python linenums:1>+<Code python nolinenum>
 from ij.io import OpenDialog from ij.io import OpenDialog
 +
 op = OpenDialog("Choose Track Data...", "") op = OpenDialog("Choose Track Data...", "")
 print op.getDirectory()+ op.getFileName() print op.getDirectory()+ op.getFileName()
-</code>+</Code>
  
-==== Getting the directory where the curently opened image is stored ====+==== Getting the directory where the currently opened image is stored ====
 <code python:1> <code python:1>
 from ij import IJ from ij import IJ
Line 130: Line 134:
 </code> </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]]+... can also be done by ''%%IJ.getDirectory("image")%%'', but with this IJ method, one cannot specify the 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.  Be careful not to mix with the usage of ''%%getFileInfo%%''. This does not hold directory information. 
documents/120206pyip_cooking/python_imagej_cookbook.1728409503.txt.gz · Last modified: 2024/10/08 17:45 by kota

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki