Using bio-formats and ImageJ2 for plugin

Hello
I’m just starting to develop plugins using imagej2 but I’m having trouble getting Bioformats and Imagej2 to work together. I’ve had a look on this forum and some of the posts have got me to the point where I can open an image but I cannot get it to be useable in FIJI. My code is below, Methods 1 and 3 both work in that they open the image but its headless so I can’t use any functions in FIJI. For example I often get users to fine tune the thresholding before the plugin progresses but that can’t happen in headless mode.
Method 2 doesn’t throw any errors but doesn’t open the image either. I’m using Eclipse to develop my plugins so I can see that the image is there in Method 2 I just can’t figure out how to show it.

My guess is that this a context problem but I don’t know how to get the headless version of the image to be recognised by FIJI in my plugin.

Any help greatly appreciated

Regards

David

package net.imagej.imagej;

import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.ops.OpService;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.type.numeric.RealType;

import ij.ImagePlus;
import ij.io.OpenDialog;
import loci.plugins.BF;
import loci.plugins.in.ImporterOptions;
import loci.common.Region;
import java.io.File;

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

@Plugin(type = Command.class, menuPath = “Plugins>Opener”)
public class Opener<T extends RealType> implements Command {

@Override
public void run() {

}

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();
  
    //Method 1
    
    //Ask user for file to open
	OpenDialog od = new OpenDialog("Open Image File...");
    String dir = od.getDirectory();
    String name = od.getFileName();
    //Convert path to String variable
    String id = dir + name;

    //Open and display the image
    ImagePlus[] img = BF.openImagePlus(id);
    for (ImagePlus imp : img) { 
    	imp.show();
    }
	
    //Method 2
    
    // ask the user for a file to open
    final File file = ij.ui().chooseFile(null, "Open Image");
    String stringName = file.getAbsolutePath();	
    if (file != null) {
        // load the image
    	ImagePlus[] img2 = BF.openImagePlus(stringName);
        // show the image
        ij.ui().show(img2[0]);
    }
    
  //Method 3
    final File file2 = ij.ui().chooseFile(null, "Open Image");
    String stringName2 = file2.getAbsolutePath();	
    String path = stringName2;
    ImporterOptions options = new ImporterOptions();
    options.setId(path);
    options.setAutoscale(true);
    options.setColorMode(ImporterOptions.COLOR_MODE_COMPOSITE);
    ImagePlus[] imps = BF.openImagePlus(options);
     
    imps[0].show();
}

}

For method 2, does it work if you add a call to showUI first?:
ij.ui().showUI("swing");

Hello David

Thanks for replying. Unfortunately it still doesn’t work. If I try any sort of interaction with the image from FIJI, for example threshold, I get a null pointer exception in the console. However if I close FIJI the image closes with it so its somehow linked but I can’t figure out how and where.

Regards
David

The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.