xp1632 / VPE_IP

0 stars 0 forks source link

How to add new plugins/command to imagej2 on Jupyterlab? #42

Open xp1632 opened 9 months ago

xp1632 commented 9 months ago

1. Initialization of ImageJ context:

%classpath config resolver scijava.public https://maven.scijava.org/content/groups/public
%classpath add mvn net.imagej imagej 2.0.0-rc-71

// Creates a new ImageJ2 gateway: a variable to be used as the central enrty point to any ImageJ2 functionality
ij = new net.imagej.ImageJ()
"ImageJ v${ij.getVersion()} is ready to go."

2. Now we have two ways of adding the content of plugins:

You may check the customized plugin illustration here


import java.io.File; import java.util.ArrayList; import java.util.List;

@Plugin(type = Command.class, menuPath = "Plugins>Gauss Filtering") public class Gaus<T extends RealType> implements Command {

@Parameter
private Dataset currentData;

@Parameter
private UIService uiService;

@Parameter
private OpService opService;

@Override
public void run() {
    final Img<T> image = (Img<T>)currentData.getImgPlus();

    final double[] sigmas = [1.0, 3.0, 5.0];

    List<RandomAccessibleInterval<T>> results = new ArrayList<>();

    for (double sigma : sigmas) {
        results.add(opService.filter().gauss(image, sigma));
    }

    // display result
    for (RandomAccessibleInterval<T> elem : results) {
        uiService.show(elem);
    }
}

/**
 * This main function serves for development purposes.
 * It allows you to run the plugin immediately out of
 * your integrated development environment (IDE).
 *
 * @param args whatever, it's ignored
 * @throws Exception
 */
public static void main(final String... args) throws Exception {
    // create the ImageJ application context with all available services
    final ImageJ ij = new ImageJ();
    ij.ui().showUI();

    // ask the user for a file to open
    final File file = ij.ui().chooseFile(null, "open");

    if (file != null) {
        // load the dataset
        final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());

        // show the image
        ij.ui().show(dataset);

        // invoke the plugin
        ij.command().run(GaussFiltering.class, true);
    }
}

}


---

2.2 Or we can define it in our IDE and export the maven project as **JAR** file
You may check how to export plugin as JAR [here](https://github.com/Max-ChenFei/VPE_IP/issues/35#issuecomment-1828385150):

- We add the JAR files to classpath 
- it doesn't provide a direct confirmation message 
- so we check if the jar is added successfully by import the class inside the jar
- and print something

%classpath add jar 'E:/example-imagej2-command/target/GaussFiltering-0.1.0-SNAPSHOT.jar' // Import a class from the jar import com.mycompany.imagej.GaussFiltering

// If the import is successful, the jar has been added to the classpath println("Jar has been added to the classpath successfully.")


![image](https://github.com/Max-ChenFei/VPE_IP/assets/8528052/c974bc07-1a3d-4eb3-97d3-13c092685b15)

- remember when importing class we need to add the package name as prefix 

----
xp1632 commented 9 months ago

3. Manually Register the plugin on jupyter


// The @Plugin annotation is processed by the javac compiler,
// which is used to generate the metadata in class bytecode.
// Unfortunately, the Groovy compiler doesn't invoke the javac
// compiler, so we need to register the plugin manually!
GausInfo = new PluginInfo(Gaus.class, Command.class)
ij.plugin().addPlugin(GausInfo)
GausInfo

image

image


import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;
import org.scijava.plugin.PluginInfo
GaussFilteringClass = Class.forName("com.mycompany.imagej.GaussFiltering")
// we name it seperately bacause Groovy would interpret dot notation as object's property
// also remember to add package name before your plugin name
GausInfo = new PluginInfo(GaussFilteringClass, Command.class)
ij.plugin().addPlugin(GausInfo)
GausInfo
xp1632 commented 9 months ago

4. How to call the plugin/command we defined?

image

// Create a map with the input parameters
Map<String, Object> inputs = new HashMap<>();
inputs.put("currentData", grey_img); // replace 'dataset' with your actual Dataset

image

// Execute the GaussFiltering command
CommandModule module = ij.command().run(GaussFilteringClass, true, inputs).get();

import org.scijava.ItemIO; import org.scijava.command.Command; import org.scijava.plugin.Parameter; import org.scijava.plugin.Plugin;

@Plugin(type = Command.class) public class Hello implements Command {

@Parameter private String name;

@Parameter(type = ItemIO.OUTPUT) private String greeting;

@Parameter(type = ItemIO.OUTPUT) private int length;

@Override public void run() { greeting = "Hello, " + name + "!"; length = name.length(); } }

// Save a reference to the class, for use in the next cell. greetingCommand = Hello.class


- **Command Execution**

// Run the command, passing input key/value pairs using a map. inputs = ["name": "John Jacob Jingleheimer Schmidt"] module = ij.command().run(greetingCommand, true, inputs).get()

// Extract the module output values. ["greeting" : module.getOutput("greeting"), "length" : module.getOutput("length")]



---
xp1632 commented 9 months ago

we can see, to use new plugins of imageJ on jupyterlab, we need certain information provided