/** Splits the specified image into separate channels. */ 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); }