How to save as tif file with channel splitted?

I open a slide in QuPath and want to save it as tif images with channel splitted.

I use the following code:

RegionRequest request = RegionRequest.createInstance(imgpath, downsample, xi, yi, wi, hi, z, t)
imp = IJTools.convertToImagePlus(server, request).getImage()
IJ.save(imp, file.getAbsolutePath())

Then I get a single tif file that has all the channels.
Is there a way to use code to save each channel seperately?

Thanks.

I found the java code for ImageJ split channel.
Is there a way to call this method and get a list of imps,then each channel can be saved seperately?

   public static ImagePlus[] split(ImagePlus imp) {
        if (imp.getType()==ImagePlus.COLOR_RGB) {
            ImageStack[] stacks = splitRGB(imp.getStack(), true);
            ImagePlus[] images = new ImagePlus[3];
            images[0] = new ImagePlus("red", stacks[0]);
            images[1] = new ImagePlus("green", stacks[1]);
            images[2] = new ImagePlus("blue", stacks[2]);
            return images;
        }
        int width = imp.getWidth();
        int height = imp.getHeight();
        int channels = imp.getNChannels();
        int slices = imp.getNSlices();
        int frames = imp.getNFrames();
        int bitDepth = imp.getBitDepth();
        int size = slices*frames;
        Vector images = new Vector();
        HyperStackReducer reducer = new HyperStackReducer(imp);
        for (int c=1; c<=channels; c++) {
            ImageStack stack2 = new ImageStack(width, height, size); // create empty stack
            stack2.setPixels(imp.getProcessor().getPixels(), 1); // can't create ImagePlus will null 1st image
            ImagePlus imp2 = new ImagePlus("C"+c+"-"+imp.getTitle(), stack2);
            stack2.setPixels(null, 1);
            imp.setPosition(c, 1, 1);
            imp2.setDimensions(1, slices, frames);
            imp2.setCalibration(imp.getCalibration());
            reducer.reduce(imp2);
            if (imp.isComposite() && ((CompositeImage)imp).getMode()==IJ.GRAYSCALE)
                IJ.run(imp2, "Grays", "");
            if (imp2.getNDimensions()>3)
                imp2.setOpenAsHyperStack(true);
            images.add(imp2);
        }
        ImagePlus[] array = new ImagePlus[images.size()];
        return (ImagePlus[])images.toArray(array);
    }

If looks like you have all the pieces you need - if you copy the method into your Groovy script you can call it. You don’t usually need to make (m)any changes to Java to make it work as Groovy.

I’d also suggest looking at https://github.com/qupath/qupath/wiki/Advanced-scripting-with-IntelliJ

You can call methods from ImageJ1 through QuPath directly, but it helps to have IntelliJ (or another IDE) to write the code and get the import statements correct.

I’m not sure how familiar you are with scripting generally, but if you can specify exactly what you want to do and what you’ve tried then it may be possible to help some more.

Hi,Peter,
Is the java method split(ImagePlus imp) is the build-in method in the ImageJ?
If it is, then there is no need to add this method?

I tried calling this method, but error appears:
imp.split

ERROR: Error at line 542: No such property: split for class: ij.ImagePlus
Possible solutions: slice, lut

ERROR: Script error
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:65)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:298)
    at Script17.run(Script17.groovy:543)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
    at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:767)
    at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:697)
    at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:677)
    at qupath.lib.gui.scripting.DefaultScriptEditor$2.run(DefaultScriptEditor.java:1034)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

It’s built-in to ImageJ, but not part of the ImagePlus class. Seems to be in ChannelSplitter.java

So I expect you’d need something like this:

def imps = ij.plugin.ChannelSplitter.split(imp)

Yeah,it worked!
Thank you vey much!