Search in sources :

Example 31 with Point2

use of qupath.lib.geom.Point2 in project qupath by qupath.

the class PointIOTest method test2ReadPoints.

@Test
public void test2ReadPoints() {
    List<PathObject> pathObjects = null;
    List<Double[]> pointsList = new ArrayList<>();
    try {
        pathObjects = PointIO.readPoints(file);
        for (var pathObject : pathObjects) {
            Point2 point = pathObject.getROI().getAllPoints().get(0);
            pointsList.add(new Double[] { point.getX(), point.getY() });
        }
    } catch (IOException e) {
        fail();
    }
    // Check that we have the same number of sets as originally
    assertTrue(pathObjects.size() == 5);
    // Check that all coordinates are the same
    assertTrue(map.values().stream().allMatch(p -> {
        return Arrays.stream(p).anyMatch(o -> pointsList.stream().anyMatch(m -> Arrays.equals(m, o)));
    }));
    // Check that classes were correctly assigned (Sets 3 & 5)
    assertTrue(pathObjects.stream().filter(p -> isInside(map.get(3), p.getROI().getAllPoints().get(0))).allMatch(q -> q.getPathClass().getName().equals("Other")));
    assertTrue(pathObjects.stream().filter(p -> isInside(map.get(5), p.getROI().getAllPoints().get(0))).allMatch(q -> q.getPathClass().getName().equals("Tumor")));
    // Check that name were correctly assigned (Sets 4 & 5)
    assertTrue(pathObjects.stream().filter(p -> isInside(map.get(4), p.getROI().getAllPoints().get(0))).allMatch(q -> q.getName().equals("foo")));
    assertTrue(pathObjects.stream().filter(p -> isInside(map.get(5), p.getROI().getAllPoints().get(0))).allMatch(q -> q.getName().equals("bar")));
    // Check C, Z and T
    assertTrue(pathObjects.stream().allMatch(q -> {
        if (isInside(map.get(2), q.getROI().getAllPoints().get(0)))
            return (q.getROI().getC() == 2 && q.getROI().getZ() == 1 && q.getROI().getT() == 7);
        else
            return (q.getROI().getC() == -1 && q.getROI().getZ() == 0 && q.getROI().getT() == 0);
    }));
    file.delete();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Arrays(java.util.Arrays) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) PathObjects(qupath.lib.objects.PathObjects) IOException(java.io.IOException) HashMap(java.util.HashMap) PathClassFactory(qupath.lib.objects.classes.PathClassFactory) File(java.io.File) ArrayList(java.util.ArrayList) MethodOrderer(org.junit.jupiter.api.MethodOrderer) PathObject(qupath.lib.objects.PathObject) Test(org.junit.jupiter.api.Test) ROI(qupath.lib.roi.interfaces.ROI) ROIs(qupath.lib.roi.ROIs) List(java.util.List) Point2(qupath.lib.geom.Point2) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) ImagePlane(qupath.lib.regions.ImagePlane) PathObject(qupath.lib.objects.PathObject) Point2(qupath.lib.geom.Point2) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 32 with Point2

use of qupath.lib.geom.Point2 in project qupath by qupath.

the class PointIOTest method test1WritePoints.

/**
 * Uses the map generated in init() method and writes all its points down to a TSV file.
 */
@Test
public void test1WritePoints() {
    List<PathObject> pathObjects = new ArrayList<>();
    Integer[] colors = new Integer[] { -14336, -13487566, null, -3342337, -1305168 };
    for (var entry : map.entrySet()) {
        ArrayList<Point2> pointsList = new ArrayList<>();
        int c = entry.getKey() == 2 ? 2 : -1;
        int z = entry.getKey() == 2 ? 1 : 0;
        int t = entry.getKey() == 2 ? 7 : 0;
        for (var coord : entry.getValue()) {
            pointsList.add(new Point2(coord[0], coord[1]));
        }
        ROI points = ROIs.createPointsROI(pointsList, ImagePlane.getPlaneWithChannel(c, z, t));
        PathObject pathObject = PathObjects.createAnnotationObject(points);
        if (entry.getKey() == 3)
            pathObject.setPathClass(PathClassFactory.getPathClass("Other"));
        if (entry.getKey() == 4)
            pathObject.setName("foo");
        else if (entry.getKey() == 5) {
            pathObject.setPathClass(PathClassFactory.getPathClass("Tumor"));
            pathObject.setName("bar");
        }
        pathObject.setColorRGB(colors[entry.getKey() - 1]);
        pathObjects.add(pathObject);
    }
    try {
        file = File.createTempFile("tmp", ".tsv");
        PointIO.writePoints(file, pathObjects);
    } catch (IOException e) {
        fail();
    }
}
Also used : PathObject(qupath.lib.objects.PathObject) Point2(qupath.lib.geom.Point2) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ROI(qupath.lib.roi.interfaces.ROI) Test(org.junit.jupiter.api.Test)

Example 33 with Point2

use of qupath.lib.geom.Point2 in project qupath by qupath.

the class ShapeSimplifier method simplifyPolygonPoints.

/**
 * Create a simplified polygon (fewer coordinates) using method based on Visvalingam’s Algorithm.
 * The input is a list of points (the vertices) belonging to a closed polygon.
 * This list is modified in place.
 * <p>
 * See references:
 * https://hydra.hull.ac.uk/resources/hull:8338
 * https://www.jasondavies.com/simplify/
 * http://bost.ocks.org/mike/simplify/
 *
 * @param points
 * @param altitudeThreshold
 */
public static void simplifyPolygonPoints(final List<Point2> points, final double altitudeThreshold) {
    if (points.size() <= 1)
        return;
    // Remove duplicates first
    var iter = points.iterator();
    Point2 lastPoint = iter.next();
    while (iter.hasNext()) {
        var nextPoint = iter.next();
        if (nextPoint.equals(lastPoint))
            iter.remove();
        else
            lastPoint = nextPoint;
    }
    if (lastPoint.equals(points.get(0)))
        iter.remove();
    if (points.size() <= 3)
        return;
    int n = points.size();
    // Populate the priority queue
    PriorityQueue<PointWithArea> queue = new PriorityQueue<>();
    Point2 pPrevious = points.get(points.size() - 1);
    Point2 pCurrent = points.get(0);
    PointWithArea pwaPrevious = null;
    PointWithArea pwaFirst = null;
    for (int i = 0; i < n; i++) {
        Point2 pNext = points.get((i + 1) % n);
        PointWithArea pwa = new PointWithArea(pCurrent, calculateArea(pPrevious, pCurrent, pNext));
        pwa.setPrevious(pwaPrevious);
        if (pwaPrevious != null)
            pwaPrevious.setNext(pwa);
        queue.add(pwa);
        pwaPrevious = pwa;
        pPrevious = pCurrent;
        pCurrent = pNext;
        // Handle first and last cases for closed polygon
        if (i == n - 1) {
            pwa.setNext(pwaFirst);
            pwaFirst.setPrevious(pwa);
        } else if (i == 0)
            pwaFirst = pwa;
    }
    double maxArea = 0;
    int minSize = Math.max(n / 100, 3);
    Set<Point2> toRemove = new HashSet<>();
    while (queue.size() > minSize) {
        PointWithArea pwa = queue.poll();
        // logger.info("BEFORE: " + pwa + " (counter " + counter + ")");
        // Altitude check (?)
        double altitude = pwa.getArea() * 2 / pwa.getNext().getPoint().distance(pwa.getPrevious().getPoint());
        if (altitude > altitudeThreshold)
            break;
        if (pwa.getArea() < maxArea)
            pwa.setArea(maxArea);
        else
            maxArea = pwa.getArea();
        // Remove the point & update accordingly
        // points.remove(pwa.getPoint());
        toRemove.add(pwa.getPoint());
        pwaPrevious = pwa.getPrevious();
        PointWithArea pwaNext = pwa.getNext();
        pwaPrevious.setNext(pwaNext);
        pwaPrevious.updateArea();
        pwaNext.setPrevious(pwaPrevious);
        pwaNext.updateArea();
        // Reinsert into priority queue
        queue.remove(pwaPrevious);
        queue.remove(pwaNext);
        queue.add(pwaPrevious);
        queue.add(pwaNext);
    // logger.info(pwa);
    }
    points.removeAll(toRemove);
}
Also used : Point2(qupath.lib.geom.Point2) PriorityQueue(java.util.PriorityQueue) HashSet(java.util.HashSet)

Aggregations

Point2 (qupath.lib.geom.Point2)33 ArrayList (java.util.ArrayList)19 PathObject (qupath.lib.objects.PathObject)7 List (java.util.List)6 PointsROI (qupath.lib.roi.PointsROI)6 ROI (qupath.lib.roi.interfaces.ROI)6 HashMap (java.util.HashMap)5 ImagePlane (qupath.lib.regions.ImagePlane)5 Map (java.util.Map)4 PathClass (qupath.lib.objects.classes.PathClass)4 PathClassFactory (qupath.lib.objects.classes.PathClassFactory)4 ROIs (qupath.lib.roi.ROIs)4 PathIterator (java.awt.geom.PathIterator)3 IOException (java.io.IOException)3 Arrays (java.util.Arrays)3 HashSet (java.util.HashSet)3 Collectors (java.util.stream.Collectors)3 Test (org.junit.jupiter.api.Test)3 PathObjects (qupath.lib.objects.PathObjects)3 AlphaComposite (java.awt.AlphaComposite)2