Upload
cs1973
View
399
Download
3
Embed Size (px)
DESCRIPTION
Lab material for M.Tech. course
Citation preview
Introduction to ImageJ
Dr. C. SaravananNIT Durgapur
ImageJ
• Wayne Rasband, National Institute of Mental Health, Bethesda, Md. 23rd Sept, 1997.
• Operating System / Platform Independent.• Open Source• Java based• Full set of imaging functions available• Easy to use
Image types
• Supports common and important image formats.
• Acquires image directly from scanners, cameras and other video sources.
• BMP, Tiff, DICOM, GIF, Jpeg, PGM, PNG, etc.
Macros
• A macro is a simple program that automates a series of ImageJ commands.
• The easiest way to create a macro is to record a sequence of commands using the command recorder.
• A macro is saved as a text file (.txt or .ijm extension).
• Any macro file placed in ImageJ/plugins with an .ijm extension will be installed in the Plugins menu.
Macrosrun ("Close All");open();t=getTime();W = getWidth();H = getHeight();b = 0;c = 0;autoCorr = 0; for (j=0; j<H; j++) { for (i=0; i<W; i++) { p = getPixel(i,j); p1 = getPixel(i+1,j); p2 = getPixel(i+2,j); s = p*p1; t = p*p2; b += s; c += t; } }autoCorr = b-c; TotalTime=getTime()-t;wait (1500);print(autoCorr);
Variables
x = 2 ;y = 3 ;result = x * x + y + y ;student = "Amir"write (" Hello " + student);http://imagej.nih.gov/ij/developer/macro/func
tions.htmlhttp://fiji.sc/wiki/index.php/Introduction_into_
Macro_Programming
Colour
open();run("Duplicate...", "title=step-1");run("HSB Stack");setSlice(2);stack = getImageID();run("Duplicate...", "title=threshold");run("Gamma...", "value=0.30");run("Select All");run("Copy");close();selectImage(stack);run("Paste");setSlice(1);run("Duplicate...", "title=bilateral");run("Enhance Contrast", "saturated=0.4");run("Select All");run("Copy");close();selectImage(stack);run("Paste");run("RGB Color");
Histogram
open();getStatistics(area, mean, min, max, std,
histogram);write(area, mean, min, max, std);Plot.create("Histogram", "Value", "Count",
histogram);for (i=0; i<histogram.length; i++) { setResult("Value", i, i); setResult("Count", i, histogram[i]);}run("Enhance Contrast");saveAs("Jpeg");
Plugins
import ij.*;import ij.process.*;import ij.gui.*;import java.awt.*;import ij.plugin.*;import ij.plugin.frame.*;
public class My_Plugin implements PlugIn {
public void run(String arg) {IJ.open();ImagePlus imp = IJ.getImage();IJ.run(imp, "Invert", "");IJ.wait(1000);IJ.run(imp, "Invert", "");
}}
• Adding a new feature to ImageJ can be done writing a script or macro or plugin.
• Scripts and macros are easier to learn and faster to develop.
• Plugins are a dedicated mechanism for extending ImageJ.
• A plugin consists of one or more Java classes living inside a .jar file in plugins.
• All plugin .jar (or .class) files must contain an underscore in their name.
There are two different types of plugins:• Plugins which operate on one image• All other plugins (including plugins which require more
than one input images, or image format loaders)
• A filter plugin looks like this:public class My_Plugin implements PlugInFilter { public int setup(String arg, ImagePlus image) { return DOES_ALL; } public void run(ImageProcessor ip) { // Here is the action }}
• A general plugin looks like this:
public class My_Plugin implements PlugIn { public void run(String arg) { // Here is the action }}Compile and RunThe PlugInFilter interface verifyies that there is an
image and that it is of the correct type, and error handling than the Plugin interface.
Limitations of Plugins
• Plugins can only implement menu entries (in particular, they cannot provide tools in the toolbar)
• Some functions which are easy to call via macros are not available via the public Java API (e.g. Image>Stacks>Plot Z-axis profile...)
• Quicker to write macros.
Class IJ
The class ij.IJ is a convenience class with many static functions. Two of them are particularly useful for debugging:
• // output into the Log window• IJ.log(“Hello, World!”);• • // Show a message window• IJ.showMessage(“Hello, World!”);
Hierarchy of the classes representing an image
Example
• // get the current image• ImagePlus image = WindowManager.getCurrentImage(); • // get one pixel's value (slow)• float value = ip.getf(0, 0);
• // get all type-specific pixels (fast)• // in this example, a ByteProcessor
byte[] pixels = (byte[])ip.getPixels();int w = ip.getWidth(), h = ip.getHeight();for (int j = 0; j < h; j++) for (int i = 0; i < w; i++) { // Java has no unsigned 8-bit data type, so we need to perform Boolean
arithmetics int value = pixels[i + w * j] & 0xff; //treated as unsigned integer ... }
Colorprocessor
• // get all pixels of a ColorProcessorint[] pixels = (int[])ip.getPixels();int w = ip.getWidth(), h = ip.getHeight();for (int j = 0; j < h; j++) for (int i = 0; i < w; i++) { int value = pixels[i + w * j]; // value is a bit-packed RGB value int red = value & 0xff; int green = (value >> 8) & 0xff; int blue = (value >> 16) & 0xff;}
Histogram 16 bit Grayscaleimport ij.*;import ij.plugin.filter.PlugInFilter;import ij.process.*;import ij.measure.*;import java.awt.*;import java.awt.image.*;
/** Generates a tabular histogram of a 16-bit image. */public class SixteenBit_Histogram implements PlugInFilter {
Calibration cal;ImageStatistics stats;
public int setup(String arg, ImagePlus imp) {if (imp!=null) {
cal = imp.getCalibration();stats = imp.getStatistics();
}return DOES_16+NO_CHANGES;
}
public void run(ImageProcessor ip) {display16bitHistogram(ip);
}
void display16bitHistogram(ImageProcessor ip) {int[] hist = ip.getHistogram();StringBuffer sb = new StringBuffer();int min = (int)cal.getRawValue(stats.min);int max = (int)cal.getRawValue(stats.max);for (int i=min; i<=max; i++)
sb.append((int)cal.getCValue(i)+"\t"+hist[i]+"\n");new ij.text.TextWindow("Histogram", "Value\tCount", new String(sb), 300, 400);
}
}