Search in sources :

Example 16 with CompositeImage

use of ij.CompositeImage in project bioformats by openmicroscopy.

the class ImporterTest method imagePlusLutTest.

// TODO : can I replace all calls to this to colorTests() passing numChannels == 1. Another way: modify lutTest() to
// use a standard ImagePlus rather than a CompImg and have it call getColorTable(). Then pass in just RED ramped
// test values.
private void imagePlusLutTest(ImagePlus imp, boolean indexed, boolean falseColor, Color color) {
    // When numCh < 2 or numCh > 7 the setColorMode() code for Composite and Colorize cannot create a CompositeImage.
    // Therefore it creates a one channel ImagePlus with a LUT that only ramps the red channel. Test this to be
    // the case.
    assertFalse(imp instanceof CompositeImage);
    if (indexed) {
        fail("not yet supporting indexed");
    }
    LUT lut = getColorTable(imp, 0);
    byte[] data = new byte[256];
    if (color.getRed() > 0) {
        lut.getReds(data);
        ascendingValuesTest(data, 256);
    }
    if (color.getGreen() > 0) {
        lut.getGreens(data);
        ascendingValuesTest(data, 256);
    }
    if (color.getBlue() > 0) {
        lut.getBlues(data);
        ascendingValuesTest(data, 256);
    }
}
Also used : CompositeImage(ij.CompositeImage) LUT(ij.process.LUT)

Example 17 with CompositeImage

use of ij.CompositeImage in project bioformats by openmicroscopy.

the class ImporterTest method colorColorizedTester.

/**
 * tests BF's options.setColorMode(colorized)
 */
private void colorColorizedTester(boolean virtual, int pixType, boolean indexed, int channels, int chanPerPlane, boolean falseColor, int numSeries) {
    int sizeX = 55, sizeY = 71, sizeZ = 3, sizeT = 4;
    String path = constructFakeFilename("colorColorized", pixType, sizeX, sizeY, sizeZ, channels, sizeT, numSeries, indexed, chanPerPlane, falseColor, -1);
    LOGGER.debug("colorColorizedTester: {}", path);
    ImagePlus[] imps = null;
    try {
        ImporterOptions options = new ImporterOptions();
        options.setAutoscale(false);
        options.setVirtual(virtual);
        options.setColorMode(ImporterOptions.COLOR_MODE_COLORIZED);
        options.setId(path);
        imps = BF.openImagePlus(options);
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (FormatException e) {
        fail(e.getMessage());
    }
    impsCountTest(imps, 1);
    ImagePlus imp = imps[0];
    int lutLen = 3;
    int expectedSizeC = effectiveC(channels, chanPerPlane, lutLen, indexed, falseColor);
    xyzctTest(imp, sizeX, sizeY, sizeZ, expectedSizeC, sizeT);
    if ((expectedSizeC >= 2) && (expectedSizeC <= 7)) {
        assertTrue(imp.isComposite());
        CompositeImage ci = (CompositeImage) imp;
        assertTrue(ci.hasCustomLuts());
        assertEquals(CompositeImage.COLOR, ci.getMode());
        colorTests(ci, expectedSizeC, DEFAULT_COLOR_ORDER);
    } else // expectedSizeC < 2 or > 7 - we should have gotten back a regular ImagePlus
    {
        assertFalse(imp.isComposite());
        imagePlusLutTest(imp, indexed, falseColor, DEFAULT_COLOR_ORDER[0]);
    }
    stackInCztOrderTest(imp, sizeZ, expectedSizeC, sizeT, indexed, falseColor);
// TODO : i've done no pixel testing
}
Also used : CompositeImage(ij.CompositeImage) IOException(java.io.IOException) ImagePlus(ij.ImagePlus) FormatException(loci.formats.FormatException)

Example 18 with CompositeImage

use of ij.CompositeImage in project bioformats by openmicroscopy.

the class Colorizer method applyColors.

// -- Colorizer methods --
public List<ImagePlus> applyColors(List<ImagePlus> imps) {
    final ImporterOptions options = process.getOptions();
    final ImageProcessorReader reader = process.getReader();
    final DimensionSwapper dimSwapper = process.getDimensionSwapper();
    final ChannelFiller channelFiller = process.getChannelFiller();
    final ImageReader imageReader = process.getImageReader();
    for (int i = 0; i < imps.size(); i++) {
        ImagePlus imp = imps.get(i);
        final int series = (Integer) imp.getProperty(ImagePlusReader.PROP_SERIES);
        reader.setSeries(series);
        // get LUT for each channel
        final String stackOrder = dimSwapper.getDimensionOrder();
        final int zSize = imp.getNSlices();
        final int cSize = imp.getNChannels();
        final int tSize = imp.getNFrames();
        final int stackSize = imp.getStackSize();
        final LUT[] channelLUTs = new LUT[cSize];
        boolean hasChannelLUT = false;
        for (int c = 0; c < cSize; c++) {
            final int index = FormatTools.getIndex(stackOrder, zSize, cSize, tSize, stackSize, 0, c, 0);
            channelLUTs[c] = (LUT) imp.getProperty(ImagePlusReader.PROP_LUT + index);
            if (channelLUTs[c] != null)
                hasChannelLUT = true;
        }
        // compute color mode and LUTs to use
        int mode = -1;
        LUT[] luts;
        if (options.isColorModeDefault()) {
            // NB: Default color mode behavior depends on the situation.
            final boolean isRGB = reader.isRGB() || imageReader.isRGB();
            if (isRGB || channelFiller.isFilled()) {
                // NB: The original data had more than one channel per plane
                // (e.g., RGB image planes), so we use the composite display mode.
                mode = CompositeImage.COMPOSITE;
                // preserve original LUTs
                luts = makeLUTs(channelLUTs, true);
            } else if (hasChannelLUT) {
                // NB: The original data had only one channel per plane,
                // but had at least one lookup table defined. We use the color
                // display mode, with missing LUTs as grayscale.
                mode = CompositeImage.COLOR;
                // preserve original LUTs
                luts = makeLUTs(channelLUTs, true);
            } else {
                // NB: The original data had only one channel per plane,
                // and had no lookup tables defined, so we use the grayscale mode.
                mode = CompositeImage.GRAYSCALE;
                luts = null;
            }
        } else if (options.isColorModeComposite()) {
            mode = CompositeImage.COMPOSITE;
            // preserve existing channel LUTs
            luts = makeLUTs(channelLUTs, true);
        } else if (options.isColorModeColorized()) {
            mode = CompositeImage.COLOR;
            // preserve existing channel LUTs
            luts = makeLUTs(channelLUTs, true);
        } else if (options.isColorModeGrayscale()) {
            mode = CompositeImage.GRAYSCALE;
            // use default (grayscale) channel LUTs
            luts = null;
        } else if (options.isColorModeCustom()) {
            mode = CompositeImage.COLOR;
            // override any existing channel LUTs
            luts = makeLUTs(series);
        } else {
            throw new IllegalStateException("Invalid color mode: " + options.getColorMode());
        }
        // apply color mode and LUTs
        final boolean doComposite = !options.isViewStandard() && mode != -1 && cSize > 1 && cSize <= 7;
        if (doComposite) {
            final ImagePlus toClose = imp;
            CompositeImage compImage = new CompositeImage(imp, mode) {

                @Override
                public void close() {
                    super.close();
                    toClose.close();
                }

                @Override
                public void show(String message) {
                    super.show(message);
                    // see ticket #12267
                    if (toClose instanceof VirtualImagePlus) {
                        int channel = getChannel();
                        double min = getDisplayRangeMin();
                        double max = getDisplayRangeMax();
                        for (int c = 0; c < cSize; c++) {
                            setPositionWithoutUpdate(c + 1, getSlice(), getFrame());
                            setDisplayRange(min, max);
                        }
                        reset();
                        setPosition(channel, getSlice(), getFrame());
                    }
                }
            };
            compImage.setProperty(ImagePlusReader.PROP_SERIES, series);
            if (luts != null)
                compImage.setLuts(luts);
            imps.set(i, compImage);
            imp = compImage;
        } else {
            // NB: Cannot use CompositeImage for some reason.
            if (luts != null && luts.length > 0 && luts[0] != null) {
                if (imp instanceof VirtualImagePlus) {
                    ((VirtualImagePlus) imp).setLUTs(luts);
                } else if (cSize == 1)
                    imp.getProcessor().setColorModel(luts[0]);
            }
            if (mode != -1 && cSize > 7) {
                // NB: Cannot use CompositeImage with more than seven channels.
                BF.warn(options.isQuiet(), "Data has too many channels for " + options.getColorMode() + " color mode");
            }
        }
        applyDisplayRanges(imp, series);
    }
    return imps;
}
Also used : ImageProcessorReader(loci.plugins.util.ImageProcessorReader) VirtualImagePlus(loci.plugins.util.VirtualImagePlus) ChannelFiller(loci.formats.ChannelFiller) LUT(ij.process.LUT) VirtualImagePlus(loci.plugins.util.VirtualImagePlus) ImagePlus(ij.ImagePlus) CompositeImage(ij.CompositeImage) DimensionSwapper(loci.formats.DimensionSwapper) ImageReader(loci.formats.ImageReader)

Example 19 with CompositeImage

use of ij.CompositeImage in project bioformats by openmicroscopy.

the class Slicer method reslice.

// -- Slicer methods --
public ImagePlus[] reslice(ImagePlus imp, boolean sliceC, boolean sliceZ, boolean sliceT, String stackOrder) {
    ImageStack stack = imp.getImageStack();
    boolean hyperstack = imp.isHyperStack();
    Calibration calibration = imp.getCalibration();
    int sizeZ = imp.getNSlices();
    int sizeC = imp.getNChannels();
    int sizeT = imp.getNFrames();
    int slicesPerStack = stack.getSize();
    if (sliceZ)
        slicesPerStack /= sizeZ;
    if (sliceC)
        slicesPerStack /= sizeC;
    if (sliceT)
        slicesPerStack /= sizeT;
    int realSizeZ = sliceZ ? 1 : sizeZ;
    int realSizeC = sliceC ? 1 : sizeC;
    int realSizeT = sliceT ? 1 : sizeT;
    BFVirtualStack virtualStack = null;
    if (stack instanceof BFVirtualStack) {
        virtualStack = (BFVirtualStack) stack;
    }
    ImageStack[] newStacks = new ImageStack[stack.getSize() / slicesPerStack];
    for (int i = 0; i < newStacks.length; i++) {
        newStacks[i] = makeStack(stack);
        if (newStacks[i] == null)
            return null;
    }
    int stackZ = sliceZ ? sizeZ : 1;
    int stackC = sliceC ? sizeC : 1;
    int stackT = sliceT ? sizeT : 1;
    int[][] planeIndexes = new int[newStacks.length][slicesPerStack];
    for (int i = 0; i < sizeZ * sizeC * sizeT; i++) {
        int[] zct = FormatTools.getZCTCoords(stackOrder, sizeZ, sizeC, sizeT, stack.getSize(), i);
        int stackNdx = FormatTools.getIndex(stackOrder, stackZ, stackC, stackT, newStacks.length, sliceZ ? zct[0] : 0, sliceC ? zct[1] : 0, sliceT ? zct[2] : 0);
        String label = stack.getSliceLabel(i + 1);
        if (virtualStack != null) {
            ((BFVirtualStack) newStacks[stackNdx]).addSlice(label);
            int sliceNdx = FormatTools.getIndex(stackOrder, realSizeZ, realSizeC, realSizeT, slicesPerStack, sliceZ ? 0 : zct[0], sliceC ? 0 : zct[1], sliceT ? 0 : zct[2]);
            planeIndexes[stackNdx][sliceNdx] = i;
        } else {
            newStacks[stackNdx].addSlice(label, stack.getProcessor(i + 1));
        }
    }
    ImagePlus[] newImps = new ImagePlus[newStacks.length];
    for (int i = 0; i < newStacks.length; i++) {
        if (virtualStack != null) {
            ((BFVirtualStack) newStacks[i]).setPlaneIndexes(planeIndexes[i]);
        }
        int[] zct = FormatTools.getZCTCoords(stackOrder, stackZ, stackC, stackT, newStacks.length, i);
        if (imp.isComposite()) {
            CompositeImage composite = (CompositeImage) imp;
            if (composite.getMode() == CompositeImage.COLOR) {
                LUT lut = composite.getChannelLut(zct[1] + 1);
                newStacks[i].setColorModel(lut);
            }
        }
        String title = imp.getTitle();
        title += " -";
        if (sliceZ)
            title += " Z=" + zct[0];
        if (sliceT)
            title += " T=" + zct[2];
        if (sliceC)
            title += " C=" + zct[1];
        ImagePlus p = null;
        if (virtualStack != null) {
            p = new VirtualImagePlus(title, newStacks[i]);
            ((VirtualImagePlus) p).setReader(virtualStack.getReader());
        } else {
            p = new ImagePlus(title, newStacks[i]);
        }
        p.setProperty(ImagePlusReader.PROP_SERIES, imp.getProperty(ImagePlusReader.PROP_SERIES));
        p.setProperty("Info", imp.getProperty("Info"));
        p.setDimensions(realSizeC, realSizeZ, realSizeT);
        p.setCalibration(calibration);
        p.setFileInfo(imp.getOriginalFileInfo());
        if (!p.isComposite()) {
            p.setOpenAsHyperStack(hyperstack);
        }
        if (imp.isComposite() && !sliceC) {
            p = reorder(p, stackOrder, "XYCZT");
            int mode = ((CompositeImage) imp).getMode();
            newImps[i] = new CompositeImage(p, mode);
        } else
            newImps[i] = p;
        double max = imp.getDisplayRangeMax();
        double min = imp.getDisplayRangeMin();
        newImps[i].setDisplayRange(min, max);
        if (imp.isComposite() && newImps[i].isComposite()) {
            for (int c = 1; c < newImps[i].getNChannels(); c++) {
                LUT originalLut = ((CompositeImage) imp).getChannelLut(c);
                LUT lut = ((CompositeImage) newImps[i]).getChannelLut(c);
                lut.min = originalLut.min;
                lut.max = originalLut.max;
            }
        }
    }
    return newImps;
}
Also used : ImageStack(ij.ImageStack) VirtualImagePlus(loci.plugins.util.VirtualImagePlus) LUT(ij.process.LUT) Calibration(ij.measure.Calibration) BFVirtualStack(loci.plugins.util.BFVirtualStack) VirtualImagePlus(loci.plugins.util.VirtualImagePlus) ImagePlus(ij.ImagePlus) CompositeImage(ij.CompositeImage)

Example 20 with CompositeImage

use of ij.CompositeImage in project GDSC-SMLM by aherbert.

the class CameraModelManager method runViewCameraModel.

private void runViewCameraModel(CameraModelSettings settings) {
    final GenericDialog gd = new GenericDialog(TITLE);
    final String[] models = listCameraModels(false);
    gd.addChoice("Model", models, pluginSettings.getSelected());
    gd.addHelp(HelpUrls.getUrl("camera-model-manager-view"));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return;
    }
    final String name = gd.getNextChoice();
    pluginSettings.setSelected(name);
    // Try and get the named resource
    final CameraModelResource resource = settings.getCameraModelResourcesMap().get(name);
    if (resource == null) {
        IJ.error(TITLE, "Failed to find camera data for model: " + name);
        return;
    }
    // Try and load the resource.
    // Do not use loadFromFile as that validates the model data. We just want
    // to view the raw image.
    ImagePlus imp = IJ.openImage(resource.getFilename());
    // Remove the status from the ij.io.ImageWriter class
    IJ.showStatus("");
    if (imp == null) {
        IJ.error(TITLE, "Failed to load camera data for model: " + name);
        return;
    }
    final ImageStack stack = imp.getImageStack();
    if (stack.getSize() != 3) {
        IJ.error(TITLE, "Failed to load camera data for model: " + name + ".\nExpecting stack of size 3 but it was " + stack.getSize());
        return;
    }
    ImageJUtils.log("Camera model: %s\n%s", name, resource);
    for (int n = 1; n <= stack.getSize(); n++) {
        logStats(stack.getSliceLabel(n), stack.getProcessor(n));
    }
    // Show normalised variance: var/g^2
    final float[] varG2 = new float[stack.getWidth() * stack.getHeight()];
    try {
        final float[] gain = (float[]) stack.getPixels(2);
        final float[] variance = (float[]) stack.getPixels(3);
        final ExtendedStatistics stats1 = new ExtendedStatistics();
        final ExtendedStatistics stats2 = new ExtendedStatistics();
        for (int i = 0; i < gain.length; i++) {
            final double v1 = variance[i] / MathUtils.pow2(gain[i]);
            varG2[i] = (float) v1;
            stats1.add(Math.sqrt(v1));
            stats2.add(v1);
        }
        logStats("var/g^2", stats2);
        logStats("sqrt(var/g^2)", stats1);
    } catch (final Exception ex) {
        ImageJUtils.log("Failed to load camera model %s from file: %s. %s", name, resource.getFilename(), ex.getMessage());
    }
    stack.addSlice("var/g^2", varG2);
    // Create as a hyper stack
    imp = new ImagePlus(name, stack);
    imp.setDimensions(4, 1, 1);
    imp.setOpenAsHyperStack(true);
    final CompositeImage cimp = new CompositeImage(imp, CompositeImage.GRAYSCALE);
    cimp.resetDisplayRanges();
    for (int n = 1; n <= 4; n++) {
        cimp.setSliceWithoutUpdate(n);
        ImageJUtils.autoAdjust(cimp, true);
    }
    cimp.setSliceWithoutUpdate(1);
    imp = WindowManager.getImage(name);
    if (imp != null) {
        imp.setImage(cimp);
    } else {
        cimp.show();
    }
}
Also used : CameraModelResource(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraModelResource) ImageStack(ij.ImageStack) CompositeImage(ij.CompositeImage) GenericDialog(ij.gui.GenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) ImagePlus(ij.ImagePlus) ExtendedStatistics(uk.ac.sussex.gdsc.core.utils.ExtendedStatistics)

Aggregations

CompositeImage (ij.CompositeImage)20 ImagePlus (ij.ImagePlus)15 IOException (java.io.IOException)10 FormatException (loci.formats.FormatException)10 LUT (ij.process.LUT)9 ImageStack (ij.ImageStack)5 Calibration (ij.measure.Calibration)4 ImageProcessor (ij.process.ImageProcessor)4 VirtualImagePlus (loci.plugins.util.VirtualImagePlus)3 GenericDialog (ij.gui.GenericDialog)2 ColorProcessor (ij.process.ColorProcessor)2 Window (java.awt.Window)2 IndexColorModel (java.awt.image.IndexColorModel)2 ImageProcessorReader (loci.plugins.util.ImageProcessorReader)2 FileInfo (ij.io.FileInfo)1 ByteProcessor (ij.process.ByteProcessor)1 FloatProcessor (ij.process.FloatProcessor)1 ShortProcessor (ij.process.ShortProcessor)1 Color (java.awt.Color)1 Image (java.awt.Image)1