Imp Variable and User Prompt

Hi,

Please can someone guide me on how to use the “imp” variable?

At the moment, my code is simple; open image and open the threshold setter (the end goal is to do all of this with gesture control, so the program is simple at the moment):

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;

public class Threshold_Plugin implements PlugIn {

	public void run(String arg) {
		// Opens Image
		imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");

		// Opens Threshold
		IJ.setAutoThreshold(imp, "Default");
		
		// Sets Threshold values
		IJ.setRawThreshold(imp, 123, 124, null);
		
		// Applies Threshold values 
		// IJ.run(imp, "Convert to Mask", "");

		// Closes Threshold window
		IJ.run("Close");
		
		// Closes image
		imp.close();

	}
}

When I run this code, it errors because the compiler doesn’t know what “imp” is, so I would like help with two things:

  • Setting the imp variable so the code above works
  • Creating a prompt when setting the variables so the user can enter the values instead of it being done from the plugin

Thanks,

Ryan

First of all, are you sure you want to develop a plugin in Java, when you can also use Javascript (or any other scripting language) directly in the script editor?

Java is a strictly typed language, so you have to declare imp to be of the correct type, ij.ImagePlus, which is what IJ.openImage() returns:

import ij.ImagePlus;

...

// Opens Image
ImagePlus imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");

In Javascript, you don’t need to declare the type like this.


You don’t need this, as this will just set the automatic threshold that you override in the subsequent line.

You can use script parameters for this (regardless of whether you work in Java or Javascript). Here’s an example Javascript:

// @Integer (label="Lower Threshold", value=120) thr_low
// @Integer (label="Upper Threshold", value=121) thr_high

importClass(Packages.ij.IJ);

// Open image
imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");

// Sets Threshold values
IJ.setRawThreshold(imp, thr_low, thr_high, null);

// Applies Threshold values 
IJ.run(imp, "Convert to Mask", "");

// Show the image
imp.show();

// Wait a second
IJ.wait(1000);

// Close the image
imp.close();

To run this script, just copy and paste it to the script editor, choose Language > JavaScript, and press Run.

Hi,

Thanks for your reply - it was very helpful.

I do have to do this project in Java I’m afraid, as it’s a language I’m using for work. I’ve managed to set the imp variable as suggested and that all works fine, I’m just struggling with getting the upper and lower threshold box to appear like it would if i was using a mouse… I need it to pop up so I can manually set the input but placing the @Integer notation in there hasn’t made a difference…

Thanks for your reply, it was very helpful.

Ryan

If you just want to invoke the Threshold dialog and wait for the user to confirm continuation, you can use something like this:

import ij.IJ;
import ij.gui.NonBlockingGenericDialog;

IJ.run("Threshold...");
NonBlockingGenericDialog dialog = new NonBlockingGenericDialog("Continue?");
dialog.showDialog();
if (dialog.wasOKed()) {
    
}

But to just get the lower and upper threshold as parameters in their own dialog, I recommend extending an ImageJ2 org.scijava.command.Command instead of ij.PlugIn and using the @Parameter notation:

import ij.ImagePlus;

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

@Plugin(type = Command.class, menuPath = "Plugins>My Plugin",)
public class Threshold_Plugin implements Command {

    // -- Parameters --
    @Parameter
    private ImagePlus imp;

    @Parameter(label="Lower Threshold", style="slider", min=0, max=255)
    private int lowerThreshold;

    @Parameter(label="Upper Threshold", style="slider", min=0, max=255)
    private int upperThreshold;

    ...