Difference between revisions of "ImageJ1-ImageJ2 cheat sheet"

(Add ImageJ2 options for retrieving, opening and saving images)
(Streamline and improve the sheet; make it less wide)
Line 7: Line 7:
 
|-
 
|-
 
|Starting ImageJ
 
|Starting ImageJ
|<source lang="java">ij.ImageJ</source>
+
|<source lang="java">new ij.ImageJ();</source>
|<source lang="java">ImageJ ij = new net.imagej.ImageJ();
+
|<source lang="java">
ij.ui().showUI();</source>
+
ImageJ ij = new net.imagej.ImageJ();
 +
ij.ui().showUI();
 +
</source>
 
|-
 
|-
 
|Show images
 
|Show images
|imp is an ImagePlus object <source lang="java">imp.show()</source>
+
|<code>imp</code> is an <code>ImagePlus</code> object
|<source lang="java">ij.ui().show(testImg);</source>
+
<source lang="java">imp.show()</source>
<source lang="java">ImageJFunctions.show(testImg);</source>
+
|
<source lang="java">ImageJFunctions.wrap(testImg,"testImg").show();</source>
+
<source lang="java">ij.ui().show(imp);</source>
 +
<source lang="java">ImageJFunctions.show(imp);</source>
 +
<source lang="java">ImageJFunctions.wrap(imp,"Image").show();</source>
 
|-
 
|-
 
|Retrieve an active image object
 
|Retrieve an active image object
Line 22: Line 26:
 
<source lang="java">#@ Img image</source>
 
<source lang="java">#@ Img image</source>
 
In Java code:
 
In Java code:
<source lang="java">@Parameter
+
<source lang="java">
private Img image;</source>
+
@Parameter
 +
private Img image;
 +
</source>
 
Using <code>ImageDisplayService</code>:
 
Using <code>ImageDisplayService</code>:
 
<source lang="java">Dataset image = ij.imageDisplay().getActiveDataset();</source>
 
<source lang="java">Dataset image = ij.imageDisplay().getActiveDataset();</source>
 
|-
 
|-
 
|Open an image file
 
|Open an image file
|<code>IJ.openImage()</code> returns an <code>ImagePlus</code> object without showing. <source lang="java">ImagePlus imp = IJ.openImage("/path/to/image.tif");
+
|<code>IJ.openImage()</code> returns an <code>ImagePlus</code> object without showing.
imp.show();</source>
+
<source lang="java">
<source lang="java">ImagePlus imp = IJ.openImage("http://www.example.org/path/to/image.tif"); 
+
ImagePlus imp = IJ.openImage(urlOrFilePath);
imp.show();</source>
+
imp.show();
<code>IJ.open()</code> automatically shows the image without returning ImagePlus.
+
</source>
<source lang="java">IJ.open("/path/to/image.tif");
+
<code>IJ.open()</code> automatically shows the image without returning <code>ImagePlus</code>.
ImagePlus imp = IJ.getImage()</source>
+
<source lang="java">
<source lang="java">IJ.open("http://www.example.org/path/to/image.tif");
+
IJ.open(urlOrFilePath);
ImagePlus imp = IJ.getImage()</source>
+
ImagePlus imp = IJ.getImage();
|Using <code>DatasetIOService</code>:
+
</source>
<source lang="java">Dataset image = ij.scifio().datasetIO().open(path);</source>
+
|Using <code>IOService</code>:
 +
<source lang="java">Object image = ij.io().open(urlOrFilePath);</source>
 +
Using <code>DatasetIOService</code> (for type safety):
 +
<source lang="java">Dataset image = ij.scifio().datasetIO().open(urlOrFilePath);</source>
 
|-
 
|-
 
|Save an image file
 
|Save an image file
 
|<source lang="java">IJ.saveasTiff(imp, "/path/to/image.tif")</source>
 
|<source lang="java">IJ.saveasTiff(imp, "/path/to/image.tif")</source>
|Using <code>DatasetIOService</code>:
+
|Using <code>IOService</code>:
 +
<source lang="java">ij.io().save(dataset, "/path/to/image.tif");</source>
 +
Using <code>DatasetIOService</code>:
 
<source lang="java">ij.scifio().datasetIO().save(dataset, "/path/to/image.tif");</source>
 
<source lang="java">ij.scifio().datasetIO().save(dataset, "/path/to/image.tif");</source>
 
|-
 
|-
 
|Convert image types
 
|Convert image types
|Convert from ImageJ2 Img object to ImageJ1 ImagePlus object: <source lang="java">ImagePlus imp = ImageJFunctions.wrap(img,"Title");</source>
+
|Convert from ImageJ2 Img object to ImageJ1 <code>ImagePlus</code> object: <source lang="java">ImagePlus imp = ImageJFunctions.wrap(img,"Title");</source>
|Convert from ImageJ1 ImagePlus object to ImageJ Img object: <source lang="java">Img<T> realImg = ImageJFunctions.wrapReal(imp);</source>
+
|Convert from ImageJ1 <code>ImagePlus</code> object to ImgLib2 <code>Img</code> object: <source lang="java">Img<T> realImg = ImageJFunctions.wrapReal(imp);</source>
 
<source lang="java">Img<FloatType> floatImg = ImageJFunctions.convertFloat(imp);</source>
 
<source lang="java">Img<FloatType> floatImg = ImageJFunctions.convertFloat(imp);</source>
 
<source lang="java">Img<FloatType> realImg2 = ImageJFunctions.wrap(imp);</source>
 
<source lang="java">Img<FloatType> realImg2 = ImageJFunctions.wrap(imp);</source>
Line 63: Line 74:
 
|Run plugins
 
|Run plugins
 
|<source lang="java">IJ.run(imagePlus,"Normalisation","");</source>
 
|<source lang="java">IJ.run(imagePlus,"Normalisation","");</source>
|<source lang="java">ij.command().run(ImageNormalizerIJ2Plugin.class,false, new Object[]{"input", img, "ij", ij});
+
|<source lang="java">
IJ.run(imagePlus,"Normalisation (IJ2)", "");
+
ij.command().run(ImageNormalizerIJ2Plugin.class, true, "input", img, "ij", ij);
// don't forget the IJ legacy dependency
 
<dependency>
 
  <groupId>net.imagej</groupId>
 
  <artifactId>imagej-legacy</artifactId>
 
</dependency></source>
 
 
|-
 
|-
 
|Define plugins
 
|Define plugins
|<source lang="java">public class ImageNormalizerPlugin implements PluginFilter {
+
|<source lang="java">
 +
public class ImageNormalizerPlugin implements PluginFilter {
 +
  ...
 +
}
 +
</source>
 
resources/plugins.config:
 
resources/plugins.config:
Pluins>Filtering, "Normalisation", NormalizerPlugin
+
<source>
}</source>
+
Plugins>Filtering, "Normalisation", NormalizerPlugin
|<source lang="java">@Plugin(type = Command.class, menuPath = "Plugins>Normalization")
+
</source>
public class ImageNormalizerIJ2Plugin implements Command {</source>
+
|<source lang="java">
 +
@Plugin(type = Command.class, menuPath = "Plugins>Normalization")
 +
public class ImageNormalizerIJ2Plugin implements Command {
 +
  ...
 +
}
 +
</source>
 
|}
 
|}
  

Revision as of 07:41, 21 November 2018

This page summarizes translation of basic operations of ImageJ1 and ImageJ2 API. Based on the work of Robert Hasse, Scientific Computing Facility, MPI CBG Dresden.

Task ImageJ1 ImageJ2
Starting ImageJ
new ij.ImageJ();
ImageJ ij = new net.imagej.ImageJ();
ij.ui().showUI();
Show images imp is an ImagePlus object
imp.show()
ij.ui().show(imp);
ImageJFunctions.show(imp);
ImageJFunctions.wrap(imp,"Image").show();
Retrieve an active image object
ImagePlus imp = IJ.getImage();
Script parameter (the same for Dataset, ImagePlus, etc.):
#@ Img image

In Java code:

@Parameter
private Img image;

Using ImageDisplayService:

Dataset image = ij.imageDisplay().getActiveDataset();
Open an image file IJ.openImage() returns an ImagePlus object without showing.
ImagePlus imp = IJ.openImage(urlOrFilePath);
imp.show();

IJ.open() automatically shows the image without returning ImagePlus.

IJ.open(urlOrFilePath);
ImagePlus imp = IJ.getImage();
Using IOService:
Object image = ij.io().open(urlOrFilePath);

Using DatasetIOService (for type safety):

Dataset image = ij.scifio().datasetIO().open(urlOrFilePath);
Save an image file
IJ.saveasTiff(imp, "/path/to/image.tif")
Using IOService:
ij.io().save(dataset, "/path/to/image.tif");

Using DatasetIOService:

ij.scifio().datasetIO().save(dataset, "/path/to/image.tif");
Convert image types Convert from ImageJ2 Img object to ImageJ1 ImagePlus object:
ImagePlus imp = ImageJFunctions.wrap(img,"Title");
Convert from ImageJ1 ImagePlus object to ImgLib2 Img object:
Img<T> realImg = ImageJFunctions.wrapReal(imp);
Img<FloatType> floatImg = ImageJFunctions.convertFloat(imp);
Img<FloatType> realImg2 = ImageJFunctions.wrap(imp);
Show regions
imagePlus.setRoi(roi)
Img<BitType> mask; // = ...
ImagePlus maskImp =ImageJFunctions.wrap(mask, "mask");
// threshold the mask to get an ROI
ImageProcessor imageProcessor = maskImp.getProcessor();
imageProcessor.setThreshold(128,128,ImageProcessor NO_LUT_UPDATE);
Roi roi = new ThresholdToSelection().convert(imageProcessor);
imagePlus.setRoi(roi);
Run plugins
IJ.run(imagePlus,"Normalisation","");
ij.command().run(ImageNormalizerIJ2Plugin.class, true, "input", img, "ij", ij);
|-
|Define plugins
|<source lang="java">
public class ImageNormalizerPlugin implements PluginFilter {
  ...
}

resources/plugins.config:

Plugins>Filtering, "Normalisation", NormalizerPlugin
@Plugin(type = Command.class, menuPath = "Plugins>Normalization")
public class ImageNormalizerIJ2Plugin implements Command {
  ...
}


See also

https://github.com/mpicbg-scicomp/ij2course-images/blob/master/slides/ij_legacy_cheetsheet.pdf