Search in sources :

Example 1 with DefaultWritablePolyline

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

the class DefaultDetectRidgesTest method testTooFewDimensions.

@Test(expected = IllegalArgumentException.class)
public void testTooFewDimensions() {
    Img<FloatType> input = generateFloatArrayTestImg(false, 30);
    // run the image through ridge detection
    int width = 1, ridgeLengthMin = 4;
    double lowerThreshold = 2, higherThreshold = 4;
    List<DefaultWritablePolyline> polylines = (List<DefaultWritablePolyline>) ops.run(Ops.Segment.DetectRidges.class, input, width, lowerThreshold, higherThreshold, ridgeLengthMin);
}
Also used : Ops(net.imagej.ops.Ops) DefaultWritablePolyline(net.imglib2.roi.geom.real.DefaultWritablePolyline) DefaultDetectRidges(net.imagej.ops.segment.detectRidges.DefaultDetectRidges) List(java.util.List) RealPoint(net.imglib2.RealPoint) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 2 with DefaultWritablePolyline

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

the class DefaultDetectRidgesTest method testTooManyDimensions.

@Test(expected = IllegalArgumentException.class)
public void testTooManyDimensions() {
    Img<FloatType> input = generateFloatArrayTestImg(false, 30, 30, 30, 30);
    // run the image through ridge detection
    int width = 1, ridgeLengthMin = 4;
    double lowerThreshold = 2, higherThreshold = 4;
    List<DefaultWritablePolyline> polylines = (List<DefaultWritablePolyline>) ops.run(Ops.Segment.DetectRidges.class, input, width, lowerThreshold, higherThreshold, ridgeLengthMin);
}
Also used : Ops(net.imagej.ops.Ops) DefaultWritablePolyline(net.imglib2.roi.geom.real.DefaultWritablePolyline) DefaultDetectRidges(net.imagej.ops.segment.detectRidges.DefaultDetectRidges) List(java.util.List) RealPoint(net.imglib2.RealPoint) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 3 with DefaultWritablePolyline

use of net.imglib2.roi.geom.real.DefaultWritablePolyline 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 4 with DefaultWritablePolyline

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

the class DefaultDetectRidgesTest method RegressionTest.

@Test
public void RegressionTest() {
    Img<FloatType> input = generateFloatArrayTestImg(false, 30, 30);
    RandomAccess<FloatType> linePainter = input.randomAccess();
    // vertical line, then horizontal line
    for (int i = 0; i < 2; i++) {
        linePainter.setPosition(15, i);
        for (int j = 2; j < 12; j++) {
            linePainter.setPosition(j, 1 - i);
            linePainter.get().set(256);
        }
    }
    // diagonal line
    for (int j = 0; j < 10; j++) {
        linePainter.setPosition(j, 0);
        linePainter.setPosition(j, 1);
        linePainter.get().set(256);
    }
    // circle
    int radius = 4, h = 22, k = 22;
    for (double a = 0; a < 2 * Math.PI; a += (Math.PI / (4 * radius))) {
        linePainter.setPosition(h + (int) Math.round(radius * Math.cos(a)), 0);
        linePainter.setPosition(k + (int) Math.round(radius * Math.sin(a)), 1);
        linePainter.get().set(256);
    }
    // run the image through ridge detection
    int width = 1, ridgeLengthMin = 4;
    double lowerThreshold = 2, higherThreshold = 4;
    List<DefaultWritablePolyline> polylines = (List<DefaultWritablePolyline>) ops.run(Ops.Segment.DetectRidges.class, input, width, lowerThreshold, higherThreshold, ridgeLengthMin);
    int vertexCount = 0;
    for (DefaultWritablePolyline pline : polylines) {
        for (int i = 0; i < pline.numVertices(); i++) {
            RealLocalizableRealPositionable p = pline.vertex(i);
            assertEquals(p.getDoublePosition(0), plineVertices[vertexCount++], 1e-5);
            assertEquals(p.getDoublePosition(1), plineVertices[vertexCount++], 1e-5);
        }
    }
}
Also used : Ops(net.imagej.ops.Ops) DefaultWritablePolyline(net.imglib2.roi.geom.real.DefaultWritablePolyline) RealLocalizableRealPositionable(net.imglib2.roi.util.RealLocalizableRealPositionable) DefaultDetectRidges(net.imagej.ops.segment.detectRidges.DefaultDetectRidges) List(java.util.List) RealPoint(net.imglib2.RealPoint) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 5 with DefaultWritablePolyline

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

the class DefaultDetectRidges method calculate.

@Override
public List<? extends WritablePolyline> calculate(RandomAccessibleInterval<T> input) {
    double sigma = (width / (2 * Math.sqrt(3)));
    // generate the metadata images
    RidgeDetectionMetadata ridgeDetectionMetadata = new RidgeDetectionMetadata(input, sigma, lowerThreshold, higherThreshold);
    // retrieve the metadata images
    Img<DoubleType> p_values = ridgeDetectionMetadata.getPValues();
    Img<DoubleType> n_values = ridgeDetectionMetadata.getNValues();
    Img<DoubleType> gradients = ridgeDetectionMetadata.getGradients();
    // create RandomAccesses for the metadata images
    OutOfBoundsConstantValueFactory<DoubleType, RandomAccessibleInterval<DoubleType>> oscvf = new OutOfBoundsConstantValueFactory<>(new DoubleType(0));
    RandomAccess<DoubleType> pRA = oscvf.create(p_values);
    RandomAccess<DoubleType> nRA = oscvf.create(n_values);
    RandomAccess<DoubleType> gradientRA = oscvf.create(gradients);
    // create the output polyline list.
    List<DefaultWritablePolyline> lines = new ArrayList<>();
    // start at the point of greatest maximum absolute value
    gradientRA.setPosition(RidgeDetectionUtils.getMaxCoords(gradients, true));
    // loop through the maximum values of the image
    while (Math.abs(gradientRA.get().get()) > higherThreshold) {
        // create the List of points that will be used to make the polyline
        List<RealPoint> points = new ArrayList<>();
        // get all of the necessary metadata from the image.
        long[] eigenvectorPos = { gradientRA.getLongPosition(0), gradientRA.getLongPosition(1), 0 };
        // obtain the n-values
        nRA.setPosition(eigenvectorPos);
        double eigenx = nRA.get().getRealDouble();
        nRA.fwd(2);
        double eigeny = nRA.get().getRealDouble();
        // obtain the p-values
        pRA.setPosition(eigenvectorPos);
        double px = pRA.get().getRealDouble();
        pRA.fwd(2);
        double py = pRA.get().getRealDouble();
        // start the list by adding the current point, which is the most line-like
        // point on the polyline
        points.add(RidgeDetectionUtils.get2DRealPoint(gradientRA.getDoublePosition(0) + px, gradientRA.getDoublePosition(1) + py));
        // go in the direction to the left of the perpendicular value
        getNextPoint(gradientRA, pRA, nRA, points, RidgeDetectionUtils.getOctant(eigenx, eigeny), eigenx, eigeny, px, py);
        // flip the array list around so that we get one cohesive line
        gradientRA.setPosition(new long[] { eigenvectorPos[0], eigenvectorPos[1] });
        Collections.reverse(points);
        // go in the opposite direction as before.
        eigenx = -eigenx;
        eigeny = -eigeny;
        getNextPoint(gradientRA, pRA, nRA, points, RidgeDetectionUtils.getOctant(eigenx, eigeny), eigenx, eigeny, px, py);
        // set the value to 0 so that it is not reused.
        gradientRA.get().setReal(0);
        // list has fewer vertices than the parameter, then we do not report it.
        if (points.size() > ridgeLengthMin) {
            DefaultWritablePolyline pline = new DefaultWritablePolyline(points);
            lines.add(pline);
        }
        // find the next max absolute value
        gradientRA.setPosition(RidgeDetectionUtils.getMaxCoords(gradients, true));
    }
    return lines;
}
Also used : DefaultWritablePolyline(net.imglib2.roi.geom.real.DefaultWritablePolyline) ArrayList(java.util.ArrayList) OutOfBoundsConstantValueFactory(net.imglib2.outofbounds.OutOfBoundsConstantValueFactory) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) DoubleType(net.imglib2.type.numeric.real.DoubleType) RealPoint(net.imglib2.RealPoint)

Aggregations

RealPoint (net.imglib2.RealPoint)5 DefaultWritablePolyline (net.imglib2.roi.geom.real.DefaultWritablePolyline)5 AbstractOpTest (net.imagej.ops.AbstractOpTest)4 Test (org.junit.Test)4 List (java.util.List)3 Ops (net.imagej.ops.Ops)3 DefaultDetectRidges (net.imagej.ops.segment.detectRidges.DefaultDetectRidges)3 FloatType (net.imglib2.type.numeric.real.FloatType)3 ArrayList (java.util.ArrayList)2 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)1 OutOfBoundsConstantValueFactory (net.imglib2.outofbounds.OutOfBoundsConstantValueFactory)1 RealLocalizableRealPositionable (net.imglib2.roi.util.RealLocalizableRealPositionable)1 DoubleType (net.imglib2.type.numeric.real.DoubleType)1