Search code examples
javascriptimagej

ImageJ: .show() does not show an image


I very new to javascript and I would like to process some images in Fiji. I have been using the macro language for a while, but I am trying to get familiar with the formal ImageJ/Fiji API. I am trying to run the folowing simplistic piece of code, it runs with no errors but it does not show any image in the end. What's going wrong?

importClass(Packages.ij.plugin.filter.GaussianBlur);


var image = IJ.openImage("/home/.../KNIIC_BC_Cam2_AirBubble2_Image1038.bmp");
IJ.run(image, "8-bit", "");

var dpl = image.getProcessor().duplicate();
var gs = new GaussianBlur();
gs.blur(dpl,20);
new ImagePlus(gs).show();

Thanks in advance


Solution

  • The problem is the way how you deal with the ImagePlus: in the last line, you try to create a new ImagePlus, but there is no chance that this contains any information of your loaded image.

    GaussianBlur processes an ImageProcessor that you'll get via the ImagePlus#getProcessor() method. If you look at the API documentation, you'll also see that blur(ImageProcessor,double) is deprecated in favor of one of the other methods: you might want to use blurGaussian(ImageProcessor, double, double, double)here.

    This code would work:

    importClass(Packages.ij.plugin.filter.GaussianBlur);
    
    var imp = IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");
    IJ.run(imp, "8-bit", "");
    
    var ip = imp.getProcessor();
    var gs = new GaussianBlur();
    gs.blurGaussian(ip,20,20,0.01);
    imp.show();
    

    however it uses the low level way of interfering with the GaussianBlur class. To make your life easy, you can also record the javascript command in the GUI via Plugins > Macros > Record... and then choosing Record: Javascript before performing the Gaussian blur via Process > Filters > Gaussian Blur.... This would make your code much shorter:

    var imp = IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");
    IJ.run(imp, "8-bit", "");
    IJ.run(imp, "Gaussian Blur...", "sigma=20");
    
    imp.show();
    

    For general help with Javascript scripting in ImageJ, see these two links to the Fiji wiki.

    Edit: Starting from ImageJ 1.47n5, ImageProcessor has a new method blurGaussian(double sigma), shortening the above (low level) code to:

    var imp = IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");
    IJ.run(imp, "8-bit", "");
    imp.getProcessor().blurGaussian(20);
    imp.show();