Search in sources :

Example 6 with ValuePair

use of net.imglib2.util.ValuePair in project vcell by virtualcell.

the class ImageStatsForPlotting method computeMean.

/**
 * Computes the mean of each XY slice along the 3rd dimension
 * TODO: Currently assumes only 3 dimensions, must handle time series of z stacks and multiple channels
 * @param data
 * @return Pair containing A) the 3rd dimension index, and B) the mean value of the XY slice
 */
private Pair<double[], double[]> computeMean(RandomAccessibleInterval<T> data, IterableInterval<BitType> mask) {
    double[] indices = new double[(int) data.dimension(2)];
    double[] means = new double[indices.length];
    for (int z = 0; z < indices.length; z++) {
        FinalInterval interval = Intervals.createMinMax(0, 0, z, data.dimension(0) - 1, data.dimension(1) - 1, z);
        double mean = 0.0;
        RandomAccessibleInterval<T> cropped = ops.transform().crop(data, interval);
        if (mask == null) {
            mean = ops.stats().mean(Views.iterable(cropped)).getRealDouble();
        } else {
            Cursor<BitType> maskCursor = mask.localizingCursor();
            RandomAccess<T> dataRA = cropped.randomAccess();
            RealSum sum = new RealSum();
            int size = 0;
            maskCursor.reset();
            while (maskCursor.hasNext()) {
                maskCursor.fwd();
                if (maskCursor.get().get()) {
                    dataRA.setPosition(maskCursor);
                    sum.add(dataRA.get().getRealDouble());
                    size++;
                }
            }
            mean = sum.getSum() / size;
        }
        indices[z] = z;
        means[z] = mean;
    }
    return new ValuePair<double[], double[]>(indices, means);
}
Also used : BitType(net.imglib2.type.logic.BitType) ValuePair(net.imglib2.util.ValuePair) FinalInterval(net.imglib2.FinalInterval) RealSum(net.imglib2.util.RealSum)

Example 7 with ValuePair

use of net.imglib2.util.ValuePair in project imagej-ops by imagej.

the class DefaultMinimumFeret method calculate.

@Override
public Pair<RealLocalizable, RealLocalizable> calculate(Polygon2D input) {
    final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
    double distance = Double.POSITIVE_INFINITY;
    RealLocalizable p0 = points.get(0);
    RealLocalizable p1 = points.get(0);
    double tmpDist = 0;
    RealLocalizable tmpP0 = p0;
    RealLocalizable tmpP1 = p1;
    for (int i = 0; i < points.size() - 2; i++) {
        final RealLocalizable lineStart = points.get(i);
        final RealLocalizable lineEnd = points.get(i + 1);
        tmpDist = 0;
        final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
        for (int j = 0; j < points.size(); j++) {
            if (j != i && j != i + 1) {
                final RealLocalizable ttmpP0 = points.get(j);
                final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
                if (tmp > tmpDist) {
                    tmpDist = tmp;
                    final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
                    tmpP0 = new RealPoint(vp.getX(), vp.getY());
                    tmpP1 = ttmpP0;
                }
            }
        }
        if (tmpDist < distance) {
            distance = tmpDist;
            p0 = tmpP0;
            p1 = tmpP1;
        }
    }
    final RealLocalizable lineStart = points.get(points.size() - 1);
    final RealLocalizable lineEnd = points.get(0);
    final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
    tmpDist = 0;
    for (int j = 0; j < points.size(); j++) {
        if (j != points.size() - 1 && j != 0 + 1) {
            final RealLocalizable ttmpP0 = points.get(j);
            final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
            if (tmp > tmpDist) {
                tmpDist = tmp;
                final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
                tmpP0 = new RealPoint(vp.getX(), vp.getY());
                tmpP1 = ttmpP0;
            }
        }
    }
    if (tmpDist < distance) {
        distance = tmpDist;
        p0 = tmpP0;
        p1 = tmpP1;
    }
    return new ValuePair<>(p0, p1);
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) Line(org.apache.commons.math3.geometry.euclidean.twod.Line) Vector2D(org.apache.commons.math3.geometry.euclidean.twod.Vector2D) RealPoint(net.imglib2.RealPoint) ValuePair(net.imglib2.util.ValuePair) RealPoint(net.imglib2.RealPoint)

Aggregations

ValuePair (net.imglib2.util.ValuePair)7 ArrayList (java.util.ArrayList)3 RealLocalizable (net.imglib2.RealLocalizable)3 DoubleType (net.imglib2.type.numeric.real.DoubleType)2 ImagePlus (ij.ImagePlus)1 ImageStack (ij.ImageStack)1 FloatProcessor (ij.process.FloatProcessor)1 Layer (ini.trakem2.display.Layer)1 Patch (ini.trakem2.display.Patch)1 FSLoader (ini.trakem2.persistence.FSLoader)1 Rectangle (java.awt.Rectangle)1 File (java.io.File)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 LongStream (java.util.stream.LongStream)1 AffineModel1D (mpicbg.models.AffineModel1D)1 IdentityModel (mpicbg.models.IdentityModel)1