Search in sources :

Example 16 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class CentroidPolygon method calculate.

@Override
public RealLocalizable calculate(final Polygon2D input) {
    double area = sizeFunc.calculate(input).get();
    double cx = 0;
    double cy = 0;
    for (int i = 0; i < input.numVertices(); i++) {
        RealLocalizable p0 = input.vertex(i);
        RealLocalizable p1 = input.vertex((i + 1) % input.numVertices());
        double p0_x = p0.getDoublePosition(0);
        double p0_y = p0.getDoublePosition(1);
        double p1_x = p1.getDoublePosition(0);
        double p1_y = p1.getDoublePosition(1);
        cx += (p0_x + p1_x) * (p0_x * p1_y - p1_x * p0_y);
        cy += (p0_y + p1_y) * (p0_x * p1_y - p1_x * p0_y);
    }
    return new RealPoint(cx / (area * 6), cy / (area * 6));
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) RealPoint(net.imglib2.RealPoint) RealPoint(net.imglib2.RealPoint)

Example 17 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class PolygonFeatureTests method boundingBox.

@Test
public void boundingBox() {
    // ground truth verified with matlab
    final List<? extends RealLocalizable> received = GeomUtils.vertices(((Polygon2D) ops.run(DefaultBoundingBox.class, contour)));
    final RealPoint[] expected = new RealPoint[] { new RealPoint(1, 6), new RealPoint(1, 109), new RealPoint(78, 109), new RealPoint(78, 6) };
    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) RealPoint(net.imglib2.RealPoint) DefaultBoundingBox(net.imagej.ops.geom.geom2d.DefaultBoundingBox) Test(org.junit.Test) AbstractFeatureTest(net.imagej.ops.features.AbstractFeatureTest)

Example 18 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class AbstractFeatureTest method getPolygon.

protected static Polygon2D getPolygon() {
    final List<RealPoint> vertices = new ArrayList<>();
    try {
        Files.lines(Paths.get(AbstractFeatureTest.class.getResource("2d_geometric_features_polygon.txt").toURI())).forEach(l -> {
            String[] coord = l.split(" ");
            RealPoint v = new RealPoint(new double[] { Double.parseDouble(coord[0]), Double.parseDouble(coord[1]) });
            vertices.add(v);
        });
    } catch (IOException | URISyntaxException exc) {
        exc.printStackTrace();
    }
    return new DefaultWritablePolygon2D(vertices);
}
Also used : RealPoint(net.imglib2.RealPoint) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) DefaultWritablePolygon2D(net.imglib2.roi.geom.real.DefaultWritablePolygon2D)

Aggregations

RealPoint (net.imglib2.RealPoint)12 RealLocalizable (net.imglib2.RealLocalizable)11 ArrayList (java.util.ArrayList)6 DefaultWritablePolygon2D (net.imglib2.roi.geom.real.DefaultWritablePolygon2D)6 Polygon2D (net.imglib2.roi.geom.real.Polygon2D)6 AbstractFeatureTest (net.imagej.ops.features.AbstractFeatureTest)5 Test (org.junit.Test)5 ValuePair (net.imglib2.util.ValuePair)3 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 DefaultBoundingBox (net.imagej.ops.geom.geom2d.DefaultBoundingBox)1 DefaultContour (net.imagej.ops.geom.geom2d.DefaultContour)1 DefaultConvexHull2D (net.imagej.ops.geom.geom2d.DefaultConvexHull2D)1 DefaultSmallestEnclosingRectangle (net.imagej.ops.geom.geom2d.DefaultSmallestEnclosingRectangle)1 LabelRegionToPolygonConverter (net.imagej.ops.geom.geom2d.LabelRegionToPolygonConverter)1 DoubleType (net.imglib2.type.numeric.real.DoubleType)1 Line (org.apache.commons.math3.geometry.euclidean.twod.Line)1 Vector2D (org.apache.commons.math3.geometry.euclidean.twod.Vector2D)1