/** 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); }
/** Opens the image. Displays it if 'show' is true. Returns an ImagePlus object if successful. */ public ImagePlus open(boolean show) { ImagePlus imp = null; Object pixels; ProgressBar pb = null; ImageProcessor ip; ColorModel cm = createColorModel(fi); if (fi.nImages > 1) { return openStack(cm, show); } switch (fi.fileType) { case FileInfo.GRAY8: case FileInfo.COLOR8: case FileInfo.BITMAP: pixels = readPixels(fi); if (pixels == null) return null; ip = new ByteProcessor(width, height, (byte[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.GRAY16_SIGNED: case FileInfo.GRAY16_UNSIGNED: case FileInfo.GRAY12_UNSIGNED: pixels = readPixels(fi); if (pixels == null) return null; ip = new ShortProcessor(width, height, (short[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.GRAY32_INT: case FileInfo.GRAY32_UNSIGNED: case FileInfo.GRAY32_FLOAT: case FileInfo.GRAY24_UNSIGNED: case FileInfo.GRAY64_FLOAT: pixels = readPixels(fi); if (pixels == null) return null; ip = new FloatProcessor(width, height, (float[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.RGB: case FileInfo.BGR: case FileInfo.ARGB: case FileInfo.ABGR: case FileInfo.BARG: case FileInfo.RGB_PLANAR: pixels = readPixels(fi); if (pixels == null) return null; ip = new ColorProcessor(width, height, (int[]) pixels); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.RGB48: case FileInfo.RGB48_PLANAR: boolean planar = fi.fileType == FileInfo.RGB48_PLANAR; Object[] pixelArray = (Object[]) readPixels(fi); if (pixelArray == null) return null; ImageStack stack = new ImageStack(width, height); stack.addSlice("Red", pixelArray[0]); stack.addSlice("Green", pixelArray[1]); stack.addSlice("Blue", pixelArray[2]); imp = new ImagePlus(fi.fileName, stack); imp.setDimensions(3, 1, 1); if (planar) imp.getProcessor().resetMinAndMax(); imp.setFileInfo(fi); int mode = CompositeImage.COMPOSITE; if (fi.description != null) { if (fi.description.indexOf("mode=color") != -1) mode = CompositeImage.COLOR; else if (fi.description.indexOf("mode=gray") != -1) mode = CompositeImage.GRAYSCALE; } imp = new CompositeImage(imp, mode); if (!planar && fi.displayRanges == null) { for (int c = 1; c <= 3; c++) { imp.setPosition(c, 1, 1); imp.setDisplayRange(minValue, maxValue); } imp.setPosition(1, 1, 1); } break; } imp.setFileInfo(fi); setCalibration(imp); if (fi.info != null) imp.setProperty("Info", fi.info); if (fi.sliceLabels != null && fi.sliceLabels.length == 1 && fi.sliceLabels[0] != null) imp.setProperty("Label", fi.sliceLabels[0]); if (show) imp.show(); return imp; }
public void itemStateChanged(ItemEvent e) { ImagePlus imp = WindowManager.getCurrentImage(); if (imp == null) return; if (!imp.isComposite()) { int channels = imp.getNChannels(); if (channels == 1 && imp.getStackSize() <= 4) channels = imp.getStackSize(); if (imp.getBitDepth() == 24 || (channels > 1 && channels < CompositeImage.MAX_CHANNELS)) { GenericDialog gd = new GenericDialog(imp.getTitle()); gd.addMessage("Convert to multi-channel composite image?"); gd.showDialog(); if (gd.wasCanceled()) return; else IJ.doCommand("Make Composite"); } else { IJ.error( "Channels", "A composite image is required (e.g., " + moreLabel + " Open HeLa Cells),\nor create one using " + moreLabel + " Make Composite."); return; } } if (!imp.isComposite()) return; CompositeImage ci = (CompositeImage) imp; Object source = e.getSource(); if (source == choice) { int index = ((Choice) source).getSelectedIndex(); switch (index) { case 0: ci.setMode(IJ.COMPOSITE); break; case 1: ci.setMode(IJ.COLOR); break; case 2: ci.setMode(IJ.GRAYSCALE); break; } ci.updateAndDraw(); if (Recorder.record) { String mode = null; switch (index) { case 0: mode = "composite"; break; case 1: mode = "color"; break; case 2: mode = "grayscale"; break; } Recorder.record("Stack.setDisplayMode", mode); } } else if (source instanceof Checkbox) { for (int i = 0; i < checkbox.length; i++) { Checkbox cb = (Checkbox) source; if (cb == checkbox[i]) { if (ci.getMode() == IJ.COMPOSITE) { boolean[] active = ci.getActiveChannels(); active[i] = cb.getState(); if (Recorder.record) { String str = ""; for (int c = 0; c < ci.getNChannels(); c++) str += active[c] ? "1" : "0"; Recorder.record("Stack.setActiveChannels", str); Recorder.record("//Stack.toggleChannel", imp.getChannel()); } } else { imp.setPosition(i + 1, imp.getSlice(), imp.getFrame()); if (Recorder.record) Recorder.record("Stack.setChannel", i + 1); } ci.updateAndDraw(); return; } } } }