Search in sources :

Example 1 with FloatPolygon

use of ij.process.FloatPolygon 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 FloatPolygon

use of ij.process.FloatPolygon in project imagej1 by imagej.

the class EllipseRoi method makePolygonRelative.

void makePolygonRelative() {
    FloatPolygon poly = new FloatPolygon(xpf, ypf, nPoints);
    Rectangle r = poly.getBounds();
    x = r.x;
    y = r.y;
    width = r.width;
    height = r.height;
    for (int i = 0; i < nPoints; i++) {
        xpf[i] = xpf[i] - x;
        ypf[i] = ypf[i] - y;
    }
}
Also used : FloatPolygon(ij.process.FloatPolygon)

Example 3 with FloatPolygon

use of ij.process.FloatPolygon in project imagej1 by imagej.

the class RoiProperties method listCoordinates.

void listCoordinates(Roi roi) {
    if (roi == null)
        return;
    boolean allIntegers = true;
    FloatPolygon fp = roi.getFloatPolygon();
    // FloatPolygon fp  = ((PolygonRoi)roi).getNonSplineFloatCoordinates();
    ImagePlus imp = roi.getImage();
    String title = "Coordinates";
    if (imp != null) {
        Calibration cal = imp.getCalibration();
        if (cal.pixelWidth != 1.0 || cal.pixelHeight != 1.0) {
            for (int i = 0; i < fp.npoints; i++) {
                fp.xpoints[i] *= cal.pixelWidth;
                fp.ypoints[i] *= cal.pixelHeight;
            }
            allIntegers = false;
        }
        title = imp.getTitle();
    }
    if (allIntegers) {
        for (int i = 0; i < fp.npoints; i++) {
            if ((int) fp.xpoints[i] != fp.xpoints[i] || (int) fp.ypoints[i] != fp.ypoints[i]) {
                allIntegers = false;
                break;
            }
        }
    }
    ResultsTable rt = new ResultsTable();
    rt.setPrecision(allIntegers ? 0 : Analyzer.getPrecision());
    for (int i = 0; i < fp.npoints; i++) {
        rt.incrementCounter();
        rt.addValue("X", fp.xpoints[i]);
        rt.addValue("Y", fp.ypoints[i]);
    }
    rt.showRowNumbers(false);
    rt.show("XY_" + title);
}
Also used : FloatPolygon(ij.process.FloatPolygon)

Example 4 with FloatPolygon

use of ij.process.FloatPolygon in project GDSC-SMLM by aherbert.

the class PsfCreator method getSpots.

/**
 * Extract all the ROI points.
 *
 * @return the spots
 */
private BasePoint[] getSpots() {
    final int zpos = imp.getStackSize() / 2;
    final Roi roi = imp.getRoi();
    if (roi != null && roi.getType() == Roi.POINT) {
        final FloatPolygon p = roi.getFloatPolygon();
        final int n = p.npoints;
        float offset = 0.5f;
        // Check if already float coordinates
        if (!SimpleArrayUtils.isInteger(p.xpoints) || !SimpleArrayUtils.isInteger(p.ypoints)) {
            offset = 0;
        }
        final BasePoint[] roiPoints = new BasePoint[n];
        for (int i = 0; i < n; i++) {
            roiPoints[i] = new BasePoint(p.xpoints[i] + offset, p.ypoints[i] + offset, zpos);
        }
        return roiPoints;
    }
    return new BasePoint[0];
}
Also used : BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) Roi(ij.gui.Roi) FloatPolygon(ij.process.FloatPolygon) Point(java.awt.Point) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint)

Example 5 with FloatPolygon

use of ij.process.FloatPolygon in project GDSC-SMLM by aherbert.

the class AstigmatismModelManager method findFitRegion.

private boolean findFitRegion() {
    // Get the centre
    final Roi roi = imp.getRoi();
    if (roi != null && roi.getType() == Roi.POINT) {
        final FloatPolygon p = roi.getFloatPolygon();
        final int n = p.npoints;
        if (n != 1) {
            IJ.error(TITLE, "Require a single point ROI");
            return false;
        }
        cx = (int) p.xpoints[0];
        cy = (int) p.ypoints[0];
        return true;
    }
    IJ.error(TITLE, "Require a single point ROI");
    return false;
}
Also used : Roi(ij.gui.Roi) FloatPolygon(ij.process.FloatPolygon)

Aggregations

FloatPolygon (ij.process.FloatPolygon)11 Roi (ij.gui.Roi)6 ImagePlus (ij.ImagePlus)3 PolygonRoi (ij.gui.PolygonRoi)3 Point (java.awt.Point)3 Rectangle (java.awt.Rectangle)3 OffsetPointRoi (uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi)3 ImageStack (ij.ImageStack)2 ImageWindow (ij.gui.ImageWindow)2 Overlay (ij.gui.Overlay)2 PointRoi (ij.gui.PointRoi)2 ByteProcessor (ij.process.ByteProcessor)2 LUT (ij.process.LUT)2 Color (java.awt.Color)2 BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)2 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)1 PeakResult (gdsc.smlm.results.PeakResult)1 Trace (gdsc.smlm.results.Trace)1 Calibration (ij.measure.Calibration)1 USHORTPaint (ini.trakem2.display.paint.USHORTPaint)1