Search in sources :

Example 6 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class PolygonFeatureTests method smallesEnclosingRectangle.

@Test
public void smallesEnclosingRectangle() {
    // ground truth verified with matlab
    final List<? extends RealLocalizable> received = GeomUtils.vertices(((Polygon2D) ops.run(DefaultSmallestEnclosingRectangle.class, contour)));
    final RealPoint[] expected = new RealPoint[] { new RealPoint(37.229184188393, -0.006307821699), new RealPoint(-14.757779646762, 27.800672834315), new RealPoint(31.725820016821, 114.704793944491), new RealPoint(83.712783851976, 86.897813288478) };
    assertEquals("Number of polygon points differs.", expected.length, received.size());
    for (int i = 0; i < expected.length; i++) {
        assertEquals("Polygon point " + i + " differs in x-coordinate.", expected[i].getDoublePosition(0), received.get(i).getDoublePosition(0), EPSILON);
        assertEquals("Polygon point " + i + " differs in y-coordinate.", expected[i].getDoublePosition(1), received.get(i).getDoublePosition(1), EPSILON);
    }
}
Also used : RealPoint(net.imglib2.RealPoint) Polygon2D(net.imglib2.roi.geom.real.Polygon2D) DefaultSmallestEnclosingRectangle(net.imagej.ops.geom.geom2d.DefaultSmallestEnclosingRectangle) RealPoint(net.imglib2.RealPoint) Test(org.junit.Test) AbstractFeatureTest(net.imagej.ops.features.AbstractFeatureTest)

Example 7 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class PolygonFeatureTests method convexHull2D.

@Test
public void convexHull2D() {
    // ground truth computed with matlab
    final Polygon2D test = (Polygon2D) ops.run(DefaultConvexHull2D.class, contour);
    final List<? extends RealLocalizable> received = GeomUtils.vertices(test);
    final RealPoint[] expected = new RealPoint[] { new RealPoint(1, 30), new RealPoint(2, 29), new RealPoint(26, 6), new RealPoint(31, 6), new RealPoint(42, 9), new RealPoint(49, 22), new RealPoint(72, 65), new RealPoint(78, 77), new RealPoint(48, 106), new RealPoint(42, 109), new RealPoint(34, 109), new RealPoint(28, 106), new RealPoint(26, 104), new RealPoint(23, 98) };
    assertEquals("Number of polygon points differs.", expected.length, received.size());
    for (int i = 0; i < expected.length; i++) {
        assertEquals("Polygon point " + i + " differs in x-coordinate.", expected[i].getDoublePosition(0), received.get(i).getDoublePosition(0), EPSILON);
        assertEquals("Polygon point " + i + " differs in y-coordinate.", expected[i].getDoublePosition(1), received.get(i).getDoublePosition(1), EPSILON);
    }
}
Also used : RealPoint(net.imglib2.RealPoint) DefaultConvexHull2D(net.imagej.ops.geom.geom2d.DefaultConvexHull2D) Polygon2D(net.imglib2.roi.geom.real.Polygon2D) RealPoint(net.imglib2.RealPoint) Test(org.junit.Test) AbstractFeatureTest(net.imagej.ops.features.AbstractFeatureTest)

Example 8 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class DefaultDetectJunctionsTest method TestRegression.

@Test
public void TestRegression() {
    List<DefaultWritablePolyline> lines = new ArrayList<>();
    double threshold = Math.sqrt(2);
    // create first polyline (horizontal)
    List<RealPoint> list1 = new ArrayList<>();
    RealPoint p = new RealPoint(-5, 0);
    for (int i = 0; i < 10; i++) {
        p.move(1, 0);
        list1.add(new RealPoint(p));
    }
    lines.add(new DefaultWritablePolyline(list1));
    // create second polyline (vertical)
    List<RealPoint> list2 = new ArrayList<>();
    p.setPosition(0, 0);
    p.setPosition(-5, 1);
    for (int i = 0; i < 10; i++) {
        p.move(1, 1);
        list2.add(new RealPoint(p));
    }
    lines.add(new DefaultWritablePolyline(list2));
    // create third polyline (diagonal)
    List<RealPoint> list3 = new ArrayList<>();
    p.setPosition(-15, 0);
    p.setPosition(-15, 1);
    for (int i = 0; i < 20; i++) {
        p.move(1, 0);
        p.move(1, 1);
        list3.add(new RealPoint(p));
    }
    lines.add(new DefaultWritablePolyline(list3));
    // create fourth polyline (vertical, different intersection point)
    List<RealPoint> list4 = new ArrayList<>();
    p.setPosition(-11, 0);
    p.setPosition(-18, 1);
    for (int i = 0; i < 7; i++) {
        p.move(1, 1);
        list4.add(new RealPoint(p));
    }
    lines.add(new DefaultWritablePolyline(list4));
    List<RealPoint> results;
    results = (List<RealPoint>) ops.run(net.imagej.ops.segment.detectJunctions.DefaultDetectJunctions.class, lines, threshold);
    List<RealPoint> expected = new ArrayList<>();
    expected.add(new RealPoint(0, 0));
    expected.add(new RealPoint(-11, -11));
    for (int i = 0; i < results.size(); i++) {
        assertEquals(results.get(i).getDoublePosition(0), expected.get(i).getDoublePosition(0), 0);
        assertEquals(results.get(i).getDoublePosition(1), expected.get(i).getDoublePosition(1), 0);
    }
}
Also used : DefaultWritablePolyline(net.imglib2.roi.geom.real.DefaultWritablePolyline) RealPoint(net.imglib2.RealPoint) ArrayList(java.util.ArrayList) RealPoint(net.imglib2.RealPoint) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 9 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class DefaultDetectJunctions method parallelRoutine.

private <L extends RealLocalizable & RealPositionable> void parallelRoutine(RealLocalizableRealPositionable p1, RealLocalizableRealPositionable p2, RealLocalizableRealPositionable q1, RealLocalizableRealPositionable q2, List<RealPoint> junctions, boolean areVertical) {
    // find out whether or not they are on the same line
    boolean sameLine = false;
    if (areVertical && Math.round(p1.getDoublePosition(0)) == Math.round(q1.getDoublePosition(0)))
        sameLine = true;
    else {
        double m = (q2.getDoublePosition(1) - q1.getDoublePosition(1)) / (q2.getDoublePosition(0) - q1.getDoublePosition(0));
        double bp = (p2.getDoublePosition(1) - m * p2.getDoublePosition(0));
        double bq = (q2.getDoublePosition(1) - m * q2.getDoublePosition(0));
        if (bp == bq)
            sameLine = true;
    }
    // there is no junction
    if (!sameLine && Math.min(Math.min(getDistance(p1, q1), getDistance(p2, q1)), Math.min(getDistance(p1, q2), getDistance(p2, q2))) > threshold)
        return;
    int foundJunctions = 0;
    double lengthp = getDistance(p1, p2);
    double lengthq = getDistance(q1, q2);
    // check p1 to be a junction
    if ((getDistance(p1, q1) < lengthq && getDistance(p1, q2) < lengthq && sameLine) || Math.min(getDistance(p1, q1), getDistance(p1, q2)) < threshold) {
        junctions.add(makeRealPoint(p1));
        foundJunctions++;
    }
    // check p2 to be a junction
    if ((getDistance(p2, q1) < lengthq && getDistance(p2, q2) < lengthq && sameLine) || Math.min(getDistance(p2, q1), getDistance(p2, q2)) < threshold) {
        junctions.add(makeRealPoint(p2));
        foundJunctions++;
    }
    // check q1 to be a junction
    if (((getDistance(q1, p1) < lengthp && getDistance(q1, p2) < lengthp && sameLine) || (Math.min(getDistance(q1, p1), getDistance(q1, p2)) < threshold)) && foundJunctions < 2) {
        junctions.add(makeRealPoint(q1));
        foundJunctions++;
    }
    // check q2 to be a junction
    if (((getDistance(q2, p1) < lengthp && getDistance(q2, p2) < lengthp && sameLine) || (Math.min(getDistance(q2, p1), getDistance(q2, p2)) < threshold)) && foundJunctions < 2) {
        junctions.add(makeRealPoint(q2));
        foundJunctions++;
    }
}
Also used : RealPoint(net.imglib2.RealPoint)

Example 10 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class DefaultDetectJunctions method calculate.

@Override
public List<RealPoint> calculate(List<? extends WritablePolyline> input) {
    // output that allows for both split polyline inputs and a
    // realPointCollection for our junctions.
    List<RealPoint> output = new ArrayList<>();
    for (int first = 0; first < input.size() - 1; first++) {
        WritablePolyline firstLine = input.get(first);
        for (int second = first + 1; second < input.size(); second++) {
            WritablePolyline secondLine = input.get(second);
            // interval containing both plines
            Interval intersect = Intervals.intersect(slightlyEnlarge(firstLine, 2), slightlyEnlarge(secondLine, 2));
            // each other.
            if (Intervals.isEmpty(intersect))
                continue;
            // create an arraylist to contain all of the junctions for these two
            // lines (so that we can filter the junctions before putting them in
            // output).
            ArrayList<RealPoint> currentPairJunctions = new ArrayList<>();
            for (int p = 0; p < firstLine.numVertices() - 1; p++) {
                for (int q = 0; q < secondLine.numVertices() - 1; q++) {
                    RealLocalizableRealPositionable p1 = firstLine.vertex(p);
                    RealLocalizableRealPositionable p2 = firstLine.vertex(p + 1);
                    RealLocalizableRealPositionable q1 = secondLine.vertex(q);
                    RealLocalizableRealPositionable q2 = secondLine.vertex(q + 1);
                    // special cases if both lines are vertical
                    boolean pVertical = Math.round(p1.getDoublePosition(0)) == Math.round(p2.getDoublePosition(0));
                    boolean qVertical = Math.round(q1.getDoublePosition(0)) == Math.round(q2.getDoublePosition(0));
                    // intersection point between the lines created by line segments p
                    // and q.
                    double[] intersectionPoint = new double[2];
                    // since they are parallel and cannot be the same.
                    if (pVertical && qVertical) {
                        parallelRoutine(p1, p2, q1, q2, currentPairJunctions, true);
                        continue;
                    } else if (pVertical) {
                        double mq = (q2.getDoublePosition(1) - q1.getDoublePosition(1)) / (q2.getDoublePosition(0) - q1.getDoublePosition(0));
                        double bq = (q1.getDoublePosition(1) - mq * q1.getDoublePosition(0));
                        double x = p1.getDoublePosition(0);
                        double y = mq * x + bq;
                        intersectionPoint[0] = x;
                        intersectionPoint[1] = y;
                    } else if (qVertical) {
                        double mp = (p2.getDoublePosition(1) - p1.getDoublePosition(1)) / (p2.getDoublePosition(0) - p1.getDoublePosition(0));
                        double bp = (p1.getDoublePosition(1) - mp * p1.getDoublePosition(0));
                        double x = q1.getDoublePosition(0);
                        double y = mp * x + bp;
                        intersectionPoint[0] = x;
                        intersectionPoint[1] = y;
                    } else {
                        double mp = (p2.getDoublePosition(1) - p1.getDoublePosition(1)) / (p2.getDoublePosition(0) - p1.getDoublePosition(0));
                        double mq = (q2.getDoublePosition(1) - q1.getDoublePosition(1)) / (q2.getDoublePosition(0) - q1.getDoublePosition(0));
                        if (mp == mq) {
                            parallelRoutine(p1, p2, q1, q2, currentPairJunctions, false);
                            continue;
                        }
                        double bp = (p2.getDoublePosition(1) - mp * p2.getDoublePosition(0));
                        double bq = (q2.getDoublePosition(1) - mq * q2.getDoublePosition(0));
                        // point of intersection of lines created by line segments p and
                        // q.
                        double x = (bq - bp) / (mp - mq);
                        double y = mp * x + bp;
                        intersectionPoint[0] = x;
                        intersectionPoint[1] = y;
                    }
                    // find the distance from the intersection point to both line
                    // segments, and the length of the line segments.
                    double distp1 = getDistance(intersectionPoint, p1);
                    double distp2 = getDistance(intersectionPoint, p2);
                    double distq1 = getDistance(intersectionPoint, q1);
                    double distq2 = getDistance(intersectionPoint, q2);
                    // max distance from line segment to intersection point
                    double maxDist = Math.max(Math.min(distp1, distp2), Math.min(distq1, distq2));
                    // these lines are close enough to form a junction
                    if (maxDist <= threshold)
                        currentPairJunctions.add(new RealPoint(intersectionPoint));
                }
            }
            // filter out the current pair's junctions by removing duplicates and
            // then averaging all remaining nearby junctions
            filterJunctions(currentPairJunctions);
            // add the filtered junctions to the output list.
            for (RealPoint point : currentPairJunctions) output.add(point);
        }
    }
    // filter the junctions -- for each set of junctions that seem vaguely
    // similar, pick out the best one-
    filterJunctions(output);
    return output;
}
Also used : RealLocalizableRealPositionable(net.imglib2.roi.util.RealLocalizableRealPositionable) RealPoint(net.imglib2.RealPoint) ArrayList(java.util.ArrayList) RealPoint(net.imglib2.RealPoint) WritablePolyline(net.imglib2.roi.geom.real.WritablePolyline) RealInterval(net.imglib2.RealInterval) Interval(net.imglib2.Interval)

Aggregations

RealPoint (net.imglib2.RealPoint)21 ArrayList (java.util.ArrayList)9 RealLocalizable (net.imglib2.RealLocalizable)6 DefaultWritablePolygon2D (net.imglib2.roi.geom.real.DefaultWritablePolygon2D)5 Test (org.junit.Test)5 AbstractFeatureTest (net.imagej.ops.features.AbstractFeatureTest)4 Polygon2D (net.imglib2.roi.geom.real.Polygon2D)3 TriangularFacet (net.imagej.ops.geom.geom3d.mesh.TriangularFacet)2 DefaultWritablePolyline (net.imglib2.roi.geom.real.DefaultWritablePolyline)2 Vector3D (org.apache.commons.math3.geometry.euclidean.threed.Vector3D)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 AbstractOpTest (net.imagej.ops.AbstractOpTest)1 DefaultBoundingBox (net.imagej.ops.geom.geom2d.DefaultBoundingBox)1 DefaultConvexHull2D (net.imagej.ops.geom.geom2d.DefaultConvexHull2D)1 DefaultSmallestEnclosingRectangle (net.imagej.ops.geom.geom2d.DefaultSmallestEnclosingRectangle)1 DefaultMesh (net.imagej.ops.geom.geom3d.mesh.DefaultMesh)1 Facet (net.imagej.ops.geom.geom3d.mesh.Facet)1 FinalInterval (net.imglib2.FinalInterval)1 Interval (net.imglib2.Interval)1