Monday, December 5, 2016

JS and ImageProcessor

An object ImagePlus is composed of several parts whose the most important is the pixel array located in another object: the ImageProcessor.

1. The ImageProcessor

To work with the pixel array, first, you have to get the object ImageProcessor
var imp = IJ.openImage("");
var ip = imp.getProcessor(); // Get the ImageProcessor...

and now plenty of
1.1. The pixels
In ImageProcessor, the methods getPixel(..)and putPixel(..) allow to read and write pixels in the image.
// Read pixel value in `blobs.gif` at (50,100)
var x = 50;
var y = 100;
ip.getPixel(x,y); // ← 48
// Write the value 255 in the pixel at (50,100)
ip.getPixel(x,y); // ← 255
Fig.1: Write a white pixel (value: 255) in an image via its ImageProcessor
If your script uses a lot of write pixel operations, then it is faster to use the set(..)methods:
  • set(x,y,int_value)
  • setf(x,y,float_value)
Other pixel methods are also available like:
  • putRow(..): Set a row of pixels
  • putColumn(..): Set a column of pixels
1.2. Mathematical operations
Some mathematical operations are available in the ImageProcessor like add(k), multiply(k), abs(), ln(k), log(k), and(k), or(k), xor(k) where k is a value.

Here is a small example with the simple implementation of normalize(..):

function normalize(ip,min,max) {
  // Assume that image is 8-bit
  var delta = max - min;
  return ip;

var imp = IJ.getImage();
var ip = imp.getProcessor();
var stats = imp.getStatistics(); // basic stats among them: min and max
var ip = normalize(ip,stats.min,stats.max);
Note: However, if you want to compute an operation between two images, you must use the ImageCalculator object.
1.3. Transforms
All the classical geometric transforms are available in the ImageProcessor object:
  • rotate
  • rotateLeft
  • scale
  • translate
For example:
var ip = imp.getProcessor();
ip.scale(0.5,0.5); // Warning: Window size remains the same
ip.rotate(45); // angle in degree
ip.translate(20,100); // shifts in pixels
1.4. Basic image processing
erode, dilate

No comments:

Post a Comment