Monday, December 5, 2016

JS and ImagePlus


When writing a script, we start with the Recorder (Plugins > Macros > Record…), and then modify the automatically created code by redefining the variable names and by using methods (functions belonging to objects) that are much more customizable than the default equivalent plugins.



1. ImageJ API

In the ImageJ website [Link], go to the Developer Resources and look at the ImageJ API [Link].
API means Application Programming Interface and details all the classes (allowing the creation of objects) and methods (aka functions). 

This documentation (Fig.1) is organized in three panels:
  • top left panel: packages (or modules)
  • bottom left panel: All the classes corresponding to the packages chosen (by default, all the classes are displayed).
  • main right panel: the content of a class: Description, properties and methods.
Fig.1: Screenshot of the ImageJ API


In this documentation are summarized all the public methods and objects that you can use in your JS script.
Note: Some of the methods or properties are protected and in this case, they are not accessible in JavaScript.
1.1. Create an image
As shown in the Recorder, when you go to File > New > Image…, the following code is added in the Recorder:
var imp = IJ.createImage("Test", "8-bit black", 128, 128, 1); 
Now, if we look at the documentation of the createImage(..) method in the IJ class,

static ImagePlus createImage(java.lang.String title, java.lang.String type, int width, int height, int depth)
Creates a new imagePlus.

This method takes 5 arguments and returns one object of type ImagePlus. If you click on createImage, you jump to the detailed documentation describing all the arguments…

createImage
public static ImagePlus createImage(java.lang.String title,
                                    java.lang.String type,
                                    int width,
                                    int height,
                                    int depth)

Creates a new imagePlus. Type should contain "8-bit", "16-bit", "32-bit" or "RGB". In addition, it can contain "white", "black" or "ramp". Width and height specify the width and height of the image in pixels. Depth specifies the number of stack slices.

In conclusion, each time you create an image in ImageJ, an object ImagePlus is created. Thus, it is very important to understand how the latter is structured.

2. ImagePlus

An object ImagePlus is composed of several parts (Fig.2) whose the most important is the pixel array located in another object: the ImageProcessor.
Fig.2: Structure of a ImagePlus

2.1. Image Metadata
The ImagePlus contains several information (or metadata) like width, height, slices, endianness, etc.
These metadata are accessible by methods whose names begin with get…. For example, you can read metadata:
  • getWidth(): Returns the image width.
  • getHeight(): Returns the image height.
  • getNSlices(): Returns the image depth (corresponding to the slice number).
  • getBitDepth(): Returns the bit depth, 8, 16, 24 (RGB) or 32, or 0 if the bit depth is unknown.
  • getSlice(): Returns the current slice in a stack.
  • getTitle(): Returns the image title.
  • getShortTitle(): Returns the image title (without file extension if exists).
Some methods are displaying metadata groups like:
  • getDimensions(): Returns the dimensions of this image (width, height, nChannels, nSlices, nFrames) as a 5 element int array.
  • getFileInfo(): Returns a FileInfo object containing information, including the pixel array, needed to save this image.
var imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
IJ.log(imp.getFileInfo().samplePerPixel ); // ← 1
IJ.log(imp.getFileInfo() );
/*
Log window...
name=Untitled, dir=, width=128, height=128, nImages=1, offset=0, type=byte, byteOrder=big, format=0, url=, whiteIsZero=f, lutSize=256, comp=1, ranges=null, samples=1
*/
imp.getDimensions() // ← [1,256,254,1,1]
2.2. Using built-in methods rather than the equivalent plugins.
The ImagePlus contains a lot of useful methods for duplication, copy in the clipboard, paste, etc. These methods are must more powerful than the ImageJ commands.
The commands created by the Recorder:

var imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
var duplicate = new Duplicator().run(imp); // Since IJ version 1.51 replaced by crop()
IJ.run(imp, "Copy", "");
IJ.run(duplicate, "Paste", "");


... that you can replaced by methods available in the API...

var imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
var duplicate = img.crop(); // Available IJ version 1.51
imp.copy();
duplicate.paste();



3. Other crazybiocomputing posts

Further readings are available in …
  • Programming in JavaScript Series [ Link ]
  • Image Processing TOC [ Link ]


No comments:

Post a Comment