Search in sources :

Example 1 with Roi

use of ij.gui.Roi in project GDSC-SMLM by aherbert.

the class DrawClusters method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (MemoryPeakResults.isMemoryEmpty()) {
        IJ.error(TITLE, "No localisations in memory");
        return;
    }
    if (!showDialog())
        return;
    // Load the results
    MemoryPeakResults results = ResultsManager.loadInputResults(inputOption, false);
    if (results == null || results.size() == 0) {
        IJ.error(TITLE, "No results could be loaded");
        return;
    }
    // Get the traces
    Trace[] traces = TraceManager.convert(results);
    if (traces == null || traces.length == 0) {
        IJ.error(TITLE, "No traces could be loaded");
        return;
    }
    // Filter traces to a min size
    int maxFrame = 0;
    int count = 0;
    final int myMaxSize = (maxSize < minSize) ? Integer.MAX_VALUE : maxSize;
    final boolean myDrawLines = (myMaxSize < 2) ? false : drawLines;
    for (int i = 0; i < traces.length; i++) {
        if (expandToSingles)
            traces[i].expandToSingles();
        if (traces[i].size() >= minSize && traces[i].size() <= myMaxSize) {
            traces[count++] = traces[i];
            traces[i].sort();
            if (maxFrame < traces[i].getTail().getFrame())
                maxFrame = traces[i].getTail().getFrame();
        }
    }
    if (count == 0) {
        IJ.error(TITLE, "No traces achieved the size limits");
        return;
    }
    String msg = String.format(TITLE + ": %d / %s (%s)", count, Utils.pleural(traces.length, "trace"), Utils.pleural(results.size(), "localisation"));
    IJ.showStatus(msg);
    //Utils.log(msg);
    Rectangle bounds = results.getBounds(true);
    ImagePlus imp = WindowManager.getImage(title);
    boolean isUseStackPosition = useStackPosition;
    if (imp == null) {
        // Create a default image using 100 pixels as the longest edge
        double maxD = (bounds.width > bounds.height) ? bounds.width : bounds.height;
        int w, h;
        if (maxD == 0) {
            // Note that imageSize can be zero (for auto sizing)
            w = h = (imageSize == 0) ? 20 : imageSize;
        } else {
            // Note that imageSize can be zero (for auto sizing)
            if (imageSize == 0) {
                w = bounds.width;
                h = bounds.height;
            } else {
                w = (int) (imageSize * bounds.width / maxD);
                h = (int) (imageSize * bounds.height / maxD);
            }
        }
        ByteProcessor bp = new ByteProcessor(w, h);
        if (isUseStackPosition) {
            ImageStack stack = new ImageStack(w, h, maxFrame);
            for (int i = 1; i <= maxFrame; i++) // Do not clone as the image is empty
            stack.setPixels(bp.getPixels(), i);
            imp = Utils.display(TITLE, stack);
        } else
            imp = Utils.display(TITLE, bp);
        // Enlarge
        ImageWindow iw = imp.getWindow();
        for (int i = 9; i-- > 0 && iw.getWidth() < 500 && iw.getHeight() < 500; ) {
            iw.getCanvas().zoomIn(imp.getWidth() / 2, imp.getHeight() / 2);
        }
    } else {
        // Check if the image has enough frames for all the traces
        if (maxFrame > imp.getNFrames())
            isUseStackPosition = false;
    }
    final float xScale = (float) (imp.getWidth() / bounds.getWidth());
    final float yScale = (float) (imp.getHeight() / bounds.getHeight());
    // Create ROIs and store data to sort them
    Roi[] rois = new Roi[count];
    int[][] frames = (isUseStackPosition) ? new int[count][] : null;
    int[] indices = Utils.newArray(count, 0, 1);
    double[] values = new double[count];
    for (int i = 0; i < count; i++) {
        Trace trace = traces[i];
        int nPoints = trace.size();
        float[] xPoints = new float[nPoints];
        float[] yPoints = new float[nPoints];
        int j = 0;
        if (isUseStackPosition)
            frames[i] = new int[nPoints];
        for (PeakResult result : trace.getPoints()) {
            xPoints[j] = (result.getXPosition() - bounds.x) * xScale;
            yPoints[j] = (result.getYPosition() - bounds.y) * yScale;
            if (isUseStackPosition)
                frames[i][j] = result.getFrame();
            j++;
        }
        Roi roi;
        if (myDrawLines) {
            roi = new PolygonRoi(xPoints, yPoints, nPoints, Roi.POLYLINE);
            if (splineFit)
                ((PolygonRoi) roi).fitSpline();
        } else {
            roi = new PointRoi(xPoints, yPoints, nPoints);
            ((PointRoi) roi).setShowLabels(false);
        }
        rois[i] = roi;
        switch(sort) {
            case 0:
            default:
                break;
            case // Sort by ID
            1:
                values[i] = traces[i].getId();
                break;
            case // Sort by time
            2:
                values[i] = traces[i].getHead().getFrame();
                break;
            case // Sort by size descending
            3:
                values[i] = -traces[i].size();
                break;
            case // Sort by length descending
            4:
                values[i] = -roi.getLength();
                break;
            case // Mean Square Displacement
            5:
                values[i] = -traces[i].getMSD();
                break;
            case // Mean / Frame
            6:
                values[i] = -traces[i].getMeanPerFrame();
                break;
        }
    }
    if (sort > 0)
        Sort.sort(indices, values);
    // Draw the traces as ROIs on an overlay
    Overlay o = new Overlay();
    LUT lut = LUTHelper.createLUT(DrawClusters.lut);
    final double scale = 256.0 / count;
    if (isUseStackPosition) {
        // Add the tracks on the frames containing the results
        final boolean isHyperStack = imp.isDisplayedHyperStack();
        for (int i = 0; i < count; i++) {
            final int index = indices[i];
            final Color c = LUTHelper.getColour(lut, (int) (i * scale));
            final PolygonRoi roi = (PolygonRoi) rois[index];
            roi.setFillColor(c);
            roi.setStrokeColor(c);
            final FloatPolygon fp = roi.getNonSplineFloatPolygon();
            // For each frame in the track, add the ROI track and a point ROI for the current position
            for (int j = 0; j < frames[index].length; j++) {
                addToOverlay(o, (Roi) roi.clone(), isHyperStack, frames[index][j]);
                //PointRoi pointRoi = new PointRoi(pos.x + fp.xpoints[j], pos.y + fp.ypoints[j]);
                PointRoi pointRoi = new PointRoi(fp.xpoints[j], fp.ypoints[j]);
                pointRoi.setPointType(3);
                pointRoi.setFillColor(c);
                pointRoi.setStrokeColor(Color.black);
                addToOverlay(o, pointRoi, isHyperStack, frames[index][j]);
            }
        }
    } else {
        // Add the tracks as a single overlay
        for (int i = 0; i < count; i++) {
            final Roi roi = rois[indices[i]];
            roi.setStrokeColor(new Color(lut.getRGB((int) (i * scale))));
            o.add(roi);
        }
    }
    imp.setOverlay(o);
    IJ.showStatus(msg);
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageWindow(ij.gui.ImageWindow) Rectangle(java.awt.Rectangle) PeakResult(gdsc.smlm.results.PeakResult) PolygonRoi(ij.gui.PolygonRoi) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) Overlay(ij.gui.Overlay) PointRoi(ij.gui.PointRoi) ImageStack(ij.ImageStack) Color(java.awt.Color) LUT(ij.process.LUT) ImagePlus(ij.ImagePlus) PolygonRoi(ij.gui.PolygonRoi) PointRoi(ij.gui.PointRoi) Roi(ij.gui.Roi) Trace(gdsc.smlm.results.Trace) FloatPolygon(ij.process.FloatPolygon)

Example 2 with Roi

use of ij.gui.Roi in project GDSC-SMLM by aherbert.

the class SpotAnalysis method createProfile.

private void createProfile() {
    if (!parametersReady()) {
        return;
    }
    double psfWidth, blur;
    // Read settings
    try {
        psfWidth = Double.parseDouble(widthTextField.getText());
        blur = Double.parseDouble(blurTextField.getText());
        gain = Double.parseDouble(gainTextField.getText());
        msPerFrame = Double.parseDouble(exposureTextField.getText());
    } catch (NumberFormatException e) {
        IJ.error(TITLE, "Invalid numbers in the input parameters");
        return;
    }
    ImagePlus imp = WindowManager.getImage(inputChoice.getSelectedItem());
    // This should not be a problem but leave it in for now
    if (imp == null || (imp.getType() != ImagePlus.GRAY8 && imp.getType() != ImagePlus.GRAY16 && imp.getType() != ImagePlus.GRAY32)) {
        IJ.showMessage(TITLE, "Images must be grayscale.");
        return;
    }
    Roi roi = imp.getRoi();
    if (roi == null || !roi.isArea()) {
        IJ.showMessage(TITLE, "Image must have an area ROI");
        return;
    }
    int recommendedSize = (int) Math.ceil(8 * psfWidth);
    Rectangle bounds = roi.getBounds();
    if (bounds.width < recommendedSize || bounds.height < recommendedSize) {
        IJ.showMessage(TITLE, String.format("Recommend using an ROI of at least %d x %d for the PSF width", recommendedSize, recommendedSize));
        return;
    }
    // Check no existing spots are within the ROI
    if (resultsWithinBounds(bounds)) {
        GenericDialog gd = new GenericDialog(TITLE);
        gd.enableYesNoCancel();
        gd.hideCancelButton();
        gd.addMessage("The results list contains a spot within the selected bounds\n \nDo you want to continue?");
        gd.showDialog();
        if (!gd.wasOKed())
            return;
    }
    createProfile(imp, bounds, psfWidth, blur);
}
Also used : GenericDialog(ij.gui.GenericDialog) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) Roi(ij.gui.Roi) PointRoi(ij.gui.PointRoi) Point(java.awt.Point)

Example 3 with Roi

use of ij.gui.Roi in project GDSC-SMLM by aherbert.

the class SpotFinderPreview method setup.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
	 */
public int setup(String arg, ImagePlus imp) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (imp == null) {
        IJ.noImage();
        return DONE;
    }
    Roi roi = imp.getRoi();
    if (roi != null && roi.getType() != Roi.RECTANGLE) {
        IJ.error("Rectangular ROI required");
        return DONE;
    }
    return flags;
}
Also used : PointRoi(ij.gui.PointRoi) ImageRoi(ij.gui.ImageRoi) Roi(ij.gui.Roi)

Example 4 with Roi

use of ij.gui.Roi in project GDSC-SMLM by aherbert.

the class PSFEstimator method setup.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
	 */
public int setup(String arg, ImagePlus imp) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    extraOptions = Utils.isExtraOptions();
    if (imp == null) {
        IJ.noImage();
        return DONE;
    }
    globalSettings = SettingsManager.loadSettings();
    settings = globalSettings.getPsfEstimatorSettings();
    // Reset
    if (IJ.controlKeyDown()) {
        config = new FitEngineConfiguration(new FitConfiguration());
        globalSettings.setFitEngineConfiguration(config);
    } else {
        config = globalSettings.getFitEngineConfiguration();
    }
    Roi roi = imp.getRoi();
    if (roi != null && roi.getType() != Roi.RECTANGLE) {
        IJ.error("Rectangular ROI required");
        return DONE;
    }
    return showDialog(imp);
}
Also used : FitEngineConfiguration(gdsc.smlm.engine.FitEngineConfiguration) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) Roi(ij.gui.Roi)

Example 5 with Roi

use of ij.gui.Roi in project GDSC-SMLM by aherbert.

the class GaussianFit method setup.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
	 */
public int setup(String arg, ImagePlus imp) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    if (imp == null) {
        IJ.noImage();
        return DONE;
    }
    Roi roi = imp.getRoi();
    if (roi != null && roi.getType() != Roi.RECTANGLE) {
        IJ.error("Rectangular ROI required");
        return DONE;
    }
    this.imp = imp;
    if (arg.equals("final")) {
        runFinal(imp.getProcessor());
        return DONE;
    }
    return flags;
}
Also used : PointRoi(ij.gui.PointRoi) Roi(ij.gui.Roi)

Aggregations

Roi (ij.gui.Roi)14 Rectangle (java.awt.Rectangle)6 PointRoi (ij.gui.PointRoi)4 PolygonRoi (ij.gui.PolygonRoi)4 Point (java.awt.Point)4 ImagePlus (ij.ImagePlus)3 ImageStack (ij.ImageStack)3 GenericDialog (ij.gui.GenericDialog)3 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)2 ImageRoi (ij.gui.ImageRoi)2 Overlay (ij.gui.Overlay)2 BasePoint (gdsc.core.match.BasePoint)1 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)1 FitConfiguration (gdsc.smlm.fitting.FitConfiguration)1 MaskDistribution (gdsc.smlm.model.MaskDistribution)1 PeakResult (gdsc.smlm.results.PeakResult)1 Trace (gdsc.smlm.results.Trace)1 ImageWindow (ij.gui.ImageWindow)1 ByteProcessor (ij.process.ByteProcessor)1 FloatPolygon (ij.process.FloatPolygon)1