ImageJ2 way to check Image type at runtime

In ImageJ1 plugin the function setup allows to check the image type to make sure it corresponds to the expected type, before showing up any GUI.

What’s the best way to do that in ImageJ2 style?
I tried to use a callback but it does not seems to be working.
Extract from the IJ2 plugin :

    @Parameter (callback="CheckType")
    private ImagePlus imp;
    
    /**
     * Function associated to Imp input: check image type
     * @param Imp
     */
    protected void CheckType(ImagePlus Imp) {
    	ImageProcessor ImProc = Imp.getProcessor();
    	if ( !ImProc.isBinary() ) IJ.error("Image must be a 8-bit binary image (0-255 exclusively)");
    }

Edit: Any suggestions @ctrueden @imagejan? :stuck_out_tongue:

1 Like

Ideally, you write a command that works independent of the image type :wink:

I know that’s not always possible. But when expecting a binary image, this image could be “binary” in the ImageJ1 way (i.e. 8-bit UnsignedByteType with only 0s and 255s), or it could be BitType or BoolType. If you want to create a command that’s reusable in various scenarios (e.g. headless, or callable from #knime), you shouldn’t restrict it to the types used in the legacy UI of ImageJ 1.x.

In theory, you should be able to specify:

@Parameter
private Img<BitType> input;

… but I don’t think the generic parameter would have any effect on which input images can be selected.


In what way doesn’t it work? Instead of calling IJ.error, you could try adding a message-type parameter and change that accordingly.

There’s also validater in the Parameter API:

… but I’m unsure how to use it (@ctrueden ?). For use cases like yours, I’d expect to have some boolean validate() function that, when returning false, leads to a grayed-out inactive OK button or some such. But I don’t think that exists in SciJava currently.

I think the reason why it does not work is that callback works for parameters which value can be changed when the GUI is showing up, which is not the case for the image.

The validater “works” though :smiley:
Like calling IJ.error in the validater shows up a nice readable error message before the GUI shows up.
However I could not find a user-friendly way to prevent the GUI to show up at all after the error message, and to prevent the execution of the run method when the image type is not as expected.

Indeed IJ.error is not stopping the execution at all.
While if I throw an exception in the validater it is indeed stopping the all thing but the stack trace is not really user friendly.

[ERROR] Module threw exception
java.lang.RuntimeException: org.scijava.module.MethodCallException: Error executing method: ijopencv.examples.HoughLinesJ_#CheckType
---I SKIP A BUNCH OF LINES ----
Caused by: java.lang.IllegalArgumentException: Image must be binary // My custom message :D ! 
	at ijopencv.examples.HoughLinesJ_.CheckType(HoughLinesJ_.java:58)
	... 17 more

I find a way to prevent the execution of run by setting a Boolean isImageOK to false in the validater and just having an empty return statement in run when this is the case.
But it still does not prevent the GUI to show up :sweat_smile: