Starting with ImageJ plugins
A while ago, I wrote a script in ImageJ that counts the number of particles taken up by a cell. The script proved to be useful; many of my colleagues have used it in academic publications. Now, I need to update the script to improve particle identification and segmentation, so I’ve started coding again on the original script. However, at this point, the script had reached a critical size, where the standard tools provided in ImageJ are insufficient. Finding, editing, or adding code became difficult and slow, and debugging is a headache. Then, I decided to translate the script into a plugin. By doing this, I also expect to improve usability and deployment for users with no programming experience. But I have never written an ImageJ plugin. So here I’m keeping a log of the tips and tricks I’m finding while learning how to do it.
I’m skipping all the preliminaries on installation and configuration of Fiji, IDEs, Maven, etc., because this information is available everywhere.
Where to start
The first page to check is the main page for Developing ImageJ2 Plugins in the ImageJ documentation. If you need to start one step before that, the advice is to read the Writing Fiji Plugins: A Beginner’s Perspective.
It is always good to have at hand the developer resources, in particular, the API documentation.
Hello World
We don’t need to reinvent the wheel, let’s use the HelloWorld.java example from the ImageJ tutorials repository.
There are many things to learn from this:
-
@Plugin(type = Command.class, headless = true, menuPath = "Help>Hello, World!")defines the class as a plugin. The plugin is of type command, which is used to execute actions once and finish. Another type is service, which continues to run and provides actions while ImageJ is running. Theheadlessproperty allows the script to run without user intervention. I don’t think it has an effect, as the plugin explicitly requests user interaction. The last parameter ismenuPath, and it defines where in the ImageJ menu the plugin will be available. -
public class HelloWorld implements Commandsets the class/plugin name and derives it from an command abstract class. -
When calling the plugin, the
run()method runs. -
The
main()method is used for debugging only. It launches an ImageJ instance (ij.launch(args)) and calls the plugin (ij.command().run(HelloWorld.class, true)).ijis the instance name andcommands()refers to the plugin type. -
In Java, the
@symbol is called an annotation. Annotations provide information to the compiler regarding the class or method that follows them. There are built-in Java annotations, such as@Override.@Parameteris a custom annotation defined in sciJava, and indicates the plugin inputs and outputs. -
The dialogue window is created by declaring the string
name, which is annotated with the parameter label, defining a text field in the window dialogue. -
The Parameter annotation adds
type = ItemIO.OUTPUTto the string greeting, converting it to an output to write in a text window. -
The
main()function is intended for testing and debugging, but it is not necessary when the plugin is installed in Fiji. To install it, copy the jar file created in the project’s target folder to Fiji’s plugins folder.
The code
The original and complete HelloWorld.java code is hosted in the ImageJ tutorials repository. Below, I present a short version without comments.
import net.imagej.ImageJ;
import org.scijava.ItemIO;
import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
@Plugin(type = Command.class, headless = true, menuPath = "Help>Hello, World!")
public class HelloWorld implements Command {
@Parameter(label = "What is your name?")
private String name = "J. Doe";
@Parameter(type = ItemIO.OUTPUT)
private String greeting;
@Override
public void run() {
greeting = "Hello, " + name + "!";
}
public static void main(final String... args) {
// Launch ImageJ
final ImageJ ij = new ImageJ();
ij.launch(args);
// Launch our "Hello World" command.
ij.command().run(HelloWorld.class, true);
}
}
Enjoy Reading This Article?
Here are some more articles you might like to read next: