javaimage-processingimagejimagej-macro

Merge RGB channels in ImageJ Javascript macro


I'm reading in cropped areas from three channel .svs images and saving the crops as .tiff images. Currently, the images are saving with separate RGB channels.

I crop the image this way:

var path = currentFolder + "images" + File.separator + imageName; 
var options = new ImporterOptions(); 
options.setId(path); 
options.setAutoscale(true); 
options.setCrop(true); 
options.setCropRegion(0, new Region(X, Y, deltaX, deltaY)); 
options.setColorMode(ImporterOptions.COLOR_MODE_COMPOSITE); 
var croppedImage= new ImagePlus();
croppedImage= BF.openImagePlus(options); 

print("cropped image class: " + croppedImage.getClass());

gives

cropped image class: class [Lij.ImagePlus;

Then I save the images:

IJ.saveAs(cropedImage, "tif", outputFileName);

I end up with three channel images.

enter image description here

I want to merge the channels.

I found two potential ways to do this:

http://javadoc.imagej.net/ImageJ1/ij/plugin/RGBStackMerge.html

http://rsb.info.nih.gov/ij/developer/api/ij/ImagePlus.html#flatten--

I tried croppedImage.flatten() and got:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: Java class "[Lij.ImagePlus;" has no public instance field or method named "flatten".

I also tried importing the RGBStackMerge class:

importClass(Packages.ij.plugin.RGBStackMerge);

and doing

finalImage = new ImagePlus();
finalImage = RGBStackMerge.mergeChannels(croppedImage, false);
print ("final image class: " + finalImage.getClass() + " length: " + finalImage.length);

But this gives:

Started svs_to_cropped_tiffs.js at Fri Jan 22 22:58:10 PST 2016 javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot call method "getClass" of null 

Solution

  • From the BF class javadoc:

    static ImagePlus[] openImagePlus(ImporterOptions options)

    BF.openImagePlus() returns an array of ImagePlus objects (indicated by [L in your getClass() output). You should access your image by accessing the first element of the returned array: croppedImage[0]

    Both of your suggested methods should work then:

    finalImage = croppedImage[0].flatten();
    

    or

    finalImage = RGBStackMerge.mergeChannels(croppedImage[0], false);
    

    You can find examples how to open images using Bio-Formats on the Scripting toolbox page on the ImageJ wiki.