Search in sources :

Example 21 with ImagePlus

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

the class ResultsImageSampler method getSample.

/**
	 * Gets the sample image. The image is a stack of the samples with an overlay of the localisation positions. The
	 * info property is set with details of the localisations and the image is calibrated.
	 *
	 * @param nNo
	 *            the number of samples with no localisations
	 * @param nLow
	 *            the number of samples with low localisations
	 * @param nHigh
	 *            the number of samples with high localisations
	 * @return the sample image (could be null if no samples were made)
	 */
public ImagePlus getSample(int nNo, int nLow, int nHigh) {
    ImageStack out = new ImageStack(size, size);
    if (!isValid())
        return null;
    list.clearf();
    // empty
    for (int i : Random.sample(nNo, no.length, r)) list.add(ResultsSample.createEmpty(no[i]));
    // low
    for (int i : Random.sample(nLow, lower, r)) list.add(data[i]);
    // high
    for (int i : Random.sample(nHigh, upper, r)) list.add(data[i + lower]);
    if (list.isEmpty())
        return null;
    double nmPerPixel = 1;
    if (results.getCalibration() != null) {
        Calibration calibration = results.getCalibration();
        if (calibration.hasNmPerPixel()) {
            nmPerPixel = calibration.getNmPerPixel();
        }
    }
    // Sort descending by number in the frame
    ResultsSample[] sample = list.toArray(new ResultsSample[list.size()]);
    Arrays.sort(sample, rcc);
    int[] xyz = new int[3];
    Rectangle stackBounds = new Rectangle(stack.getWidth(), stack.getHeight());
    Overlay overlay = new Overlay();
    float[] ox = new float[10], oy = new float[10];
    StringBuilder sb = new StringBuilder();
    if (nmPerPixel == 1)
        sb.append("Sample X Y Z Signal\n");
    else
        sb.append("Sample X(nm) Y(nm) Z(nm) Signal\n");
    for (ResultsSample s : sample) {
        getXYZ(s.index, xyz);
        // Construct the region to extract
        Rectangle target = new Rectangle(xyz[0], xyz[1], size, size);
        target = target.intersection(stackBounds);
        if (target.width == 0 || target.height == 0)
            continue;
        // Extract the frame
        int slice = xyz[2];
        ImageProcessor ip = stack.getProcessor(slice);
        // Cut out the desired pixels (some may be blank if the block overruns the source image)
        ImageProcessor ip2 = ip.createProcessor(size, size);
        for (int y = 0; y < target.height; y++) for (int x = 0, i = y * size, index = (y + target.y) * ip.getWidth() + target.x; x < target.width; x++, i++, index++) {
            ip2.setf(i, ip.getf(index));
        }
        int size = s.size();
        if (size > 0) {
            int position = out.getSize() + 1;
            // Create an ROI with the localisations
            for (int i = 0; i < size; i++) {
                PeakResult p = s.list.get(i);
                ox[i] = p.getXPosition() - xyz[0];
                oy[i] = p.getYPosition() - xyz[1];
                sb.append(position).append(' ');
                sb.append(Utils.rounded(ox[i] * nmPerPixel)).append(' ');
                sb.append(Utils.rounded(oy[i] * nmPerPixel)).append(' ');
                // Z can be stored in the error field
                sb.append(Utils.rounded(p.error * nmPerPixel)).append(' ');
                sb.append(Utils.rounded(p.getSignal())).append('\n');
            }
            PointRoi roi = new PointRoi(ox, oy, size);
            roi.setPosition(position);
            overlay.add(roi);
        }
        out.addSlice(String.format("Frame=%d @ %d,%d px (n=%d)", slice, xyz[0], xyz[1], size), ip2.getPixels());
    }
    if (out.getSize() == 0)
        return null;
    ImagePlus imp = new ImagePlus("Sample", out);
    imp.setOverlay(overlay);
    // Note: Only the info property can be saved to a TIFF file
    imp.setProperty("Info", sb.toString());
    if (nmPerPixel != 1) {
        ij.measure.Calibration cal = new ij.measure.Calibration();
        cal.setUnit("nm");
        cal.pixelHeight = cal.pixelWidth = nmPerPixel;
        imp.setCalibration(cal);
    }
    return imp;
}
Also used : ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle) Calibration(gdsc.smlm.results.Calibration) ImagePlus(ij.ImagePlus) PeakResult(gdsc.smlm.results.PeakResult) ImageProcessor(ij.process.ImageProcessor) Overlay(ij.gui.Overlay) PointRoi(ij.gui.PointRoi)

Example 22 with ImagePlus

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

the class PCPALMMolecules method getRunMode.

private boolean getRunMode(boolean resultsAvailable) {
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    gd.addHelp(About.HELP_URL);
    // Build a list of all images with a region ROI
    List<String> titles = new LinkedList<String>();
    if (WindowManager.getWindowCount() > 0) {
        for (int imageID : WindowManager.getIDList()) {
            ImagePlus imp = WindowManager.getImage(imageID);
            if (imp != null && imp.getRoi() != null && imp.getRoi().isArea())
                titles.add(imp.getTitle());
        }
    }
    if (!resultsAvailable) {
        runMode = 3;
        gd.addMessage("Simulate molecules for cluster analysis.\nComputes a binary image from localisation data");
        gd.addNumericField("Molecules", nMolecules, 0);
        gd.addNumericField("Simulation_size (um)", simulationSize, 2);
        gd.addNumericField("Blinking_rate", blinkingRate, 2);
        gd.addChoice("Blinking_distribution", BLINKING_DISTRIBUTION, BLINKING_DISTRIBUTION[blinkingDistribution]);
        gd.addNumericField("Average_precision (nm)", sigmaS, 2);
        gd.addCheckbox("Show_histograms", showHistograms);
        gd.addCheckbox("Distance_analysis", distanceAnalysis);
        gd.addChoice("Cluster_simulation", CLUSTER_SIMULATION, CLUSTER_SIMULATION[clusterSimulation]);
        gd.addNumericField("Cluster_number", clusterNumber, 2);
        gd.addNumericField("Cluster_variation (SD)", clusterNumberSD, 2);
        gd.addNumericField("Cluster_radius", clusterRadius, 2);
        gd.addCheckbox("Show_cluster_mask", showClusterMask);
        Recorder.recordOption("Run_mode", RUN_MODE[runMode]);
    } else {
        gd.addMessage("Prepare molecules for cluster analysis.\nComputes a binary image from raw localisation data");
        ResultsManager.addInput(gd, inputOption, InputSource.MEMORY);
        if (!titles.isEmpty())
            gd.addCheckbox((titles.size() == 1) ? "Use_ROI" : "Choose_ROI", chooseRoi);
        gd.addChoice("Run_mode", RUN_MODE, RUN_MODE[runMode]);
    }
    gd.addMessage("Select options for low resolution image:");
    gd.addSlider("Image_size (px)", 512, 2048, lowResolutionImageSize);
    gd.addSlider("ROI_size (um)", 1.5, 4, roiSizeInUm);
    gd.addMessage("Select options for high resolution image:");
    gd.addCheckbox("Show_high_res_image", showHighResolutionImage);
    gd.addSlider("nm_per_pixel_limit", 0, 20, nmPerPixelLimit);
    gd.addMessage("Optionally remove all analysis results from memory");
    gd.addCheckbox("Clear_results", clearResults);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    if (!resultsAvailable) {
        nMolecules = (int) Math.abs(gd.getNextNumber());
        simulationSize = Math.abs(gd.getNextNumber());
        blinkingRate = Math.abs(gd.getNextNumber());
        blinkingDistribution = gd.getNextChoiceIndex();
        sigmaS = Math.abs(gd.getNextNumber());
        showHistograms = gd.getNextBoolean();
        distanceAnalysis = gd.getNextBoolean();
        clusterSimulation = gd.getNextChoiceIndex();
        clusterNumber = Math.abs(gd.getNextNumber());
        clusterNumberSD = Math.abs(gd.getNextNumber());
        clusterRadius = Math.abs(gd.getNextNumber());
        showClusterMask = gd.getNextBoolean();
    } else {
        inputOption = ResultsManager.getInputSource(gd);
        if (!titles.isEmpty())
            chooseRoi = gd.getNextBoolean();
        runMode = gd.getNextChoiceIndex();
    }
    lowResolutionImageSize = (int) gd.getNextNumber();
    roiSizeInUm = gd.getNextNumber();
    showHighResolutionImage = gd.getNextBoolean();
    nmPerPixelLimit = Math.abs(gd.getNextNumber());
    clearResults = gd.getNextBoolean();
    // Check arguments
    try {
        if (!resultsAvailable) {
            Parameters.isAboveZero("Molecules", nMolecules);
            Parameters.isAboveZero("Simulation size", simulationSize);
            Parameters.isEqualOrAbove("Blinking rate", blinkingRate, 1);
            Parameters.isEqualOrAbove("Cluster number", clusterNumber, 1);
        }
        Parameters.isAbove("Image scale", lowResolutionImageSize, 1);
        Parameters.isAboveZero("ROI size", roiSizeInUm);
    } catch (IllegalArgumentException ex) {
        IJ.error(TITLE, ex.getMessage());
        return false;
    }
    if (!titles.isEmpty() && chooseRoi && resultsAvailable) {
        if (titles.size() == 1) {
            roiImage = titles.get(0);
            Recorder.recordOption("Image", roiImage);
        } else {
            String[] items = titles.toArray(new String[titles.size()]);
            gd = new ExtendedGenericDialog(TITLE);
            gd.addMessage("Select the source image for the ROI");
            gd.addChoice("Image", items, roiImage);
            gd.showDialog();
            if (gd.wasCanceled())
                return false;
            roiImage = gd.getNextChoice();
        }
        ImagePlus imp = WindowManager.getImage(roiImage);
        roiBounds = imp.getRoi().getBounds();
        roiImageWidth = imp.getWidth();
        roiImageHeight = imp.getHeight();
    } else {
        roiBounds = null;
    }
    if (!resultsAvailable) {
        if (!getPValue())
            return false;
    }
    if (clearResults) {
        PCPALMAnalysis.results.clear();
        PCPALMFitting.previous_gr = null;
    }
    return true;
}
Also used : ExtendedGenericDialog(ij.gui.ExtendedGenericDialog) ImagePlus(ij.ImagePlus) LinkedList(java.util.LinkedList) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(gdsc.core.clustering.ClusterPoint)

Example 23 with ImagePlus

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

the class PCPALMMolecules method displayImage.

static ImagePlus displayImage(String title, ImageProcessor ip, double nmPerPixel) {
    ImagePlus imp = Utils.display(title, ip);
    Calibration cal = new Calibration();
    cal.setUnit("um");
    cal.pixelWidth = cal.pixelHeight = nmPerPixel / 1000;
    imp.setCalibration(cal);
    return imp;
}
Also used : Calibration(ij.measure.Calibration) ImagePlus(ij.ImagePlus)

Example 24 with ImagePlus

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

the class ImageROIPainter method mouseClicked.

public void mouseClicked(MouseEvent e) {
    // Show the result that was double clicked in the result table
    if (e.getClickCount() > 1) {
        ImagePlus imp = WindowManager.getImage(title);
        if (imp == null)
            return;
        int index = textPanel.getSelectionStart();
        if (index == -1)
            return;
        double[] position = coordProvider.getCoordinates(textPanel.getLine(index));
        if (position == null || position.length < 3)
            return;
        int slice = (int) position[0];
        double x = position[1];
        double y = position[2];
        addRoi(imp, slice, new PointRoi(x, y));
        Utils.adjustSourceRect(imp, 0, (int) x, (int) y);
    }
}
Also used : ImagePlus(ij.ImagePlus) PointRoi(ij.gui.PointRoi)

Example 25 with ImagePlus

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

the class FolderOpenerDialog method openImage.

private ImagePlus openImage(String filename) {
    //System.out.printf("Opening %s %s\n", path, filename);
    Opener opener = new Opener();
    opener.setSilentMode(true);
    Utils.setShowProgress(false);
    ImagePlus imp = opener.openImage(path, filename);
    Utils.setShowProgress(true);
    if (imp != null) {
        // Initialise dimensions using first image 
        if (width == -1) {
            width = imp.getWidth();
            height = imp.getHeight();
        }
        // Check dimensions
        if (!variableSize) {
            if (width != imp.getWidth() || height != imp.getHeight())
                imp = null;
        }
    }
    return imp;
}
Also used : ImagePlus(ij.ImagePlus) Opener(ij.io.Opener) FolderOpener(ij.plugin.FolderOpener)

Aggregations

ImagePlus (ij.ImagePlus)66 ImageStack (ij.ImageStack)13 Rectangle (java.awt.Rectangle)13 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)11 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)11 LinkedList (java.util.LinkedList)10 PeakResult (gdsc.smlm.results.PeakResult)9 Point (java.awt.Point)9 ImageProcessor (ij.process.ImageProcessor)8 ArrayList (java.util.ArrayList)7 Overlay (ij.gui.Overlay)6 PointRoi (ij.gui.PointRoi)6 IJImageSource (gdsc.smlm.ij.IJImageSource)5 GenericDialog (ij.gui.GenericDialog)5 ExecutorService (java.util.concurrent.ExecutorService)5 Future (java.util.concurrent.Future)5 PSFSettings (gdsc.smlm.ij.settings.PSFSettings)4 Calibration (gdsc.smlm.results.Calibration)4 ImageWindow (ij.gui.ImageWindow)4 File (java.io.File)4