Search in sources :

Example 1 with IncompatibleTypeException

use of net.imglib2.exception.IncompatibleTypeException in project imagej-ops by imagej.

the class Convolve method run.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void run() {
    // TODO: get the dimension indices from the image dependent on the
    // selected
    // axes -> OR: just map the kernel dimension labels to the image
    // dimension
    // labels
    final int[] axisIndices = new int[] { 0, 1 };
    // number of indicies must be conform with the dimensionality of axes
    if (axes.length != axisIndices.length) {
        throw new IllegalArgumentException("The number of selected dimension doesn't conforms with the kernel size.");
    }
    if (asFloat) {
        try {
            out = (ImgPlus) in.factory().imgFactory(new FloatType()).create(in, new FloatType());
        } catch (final IncompatibleTypeException e) {
            throw new IllegalArgumentException(e);
        }
    } else {
        out = (ImgPlus<O>) in.factory().create(in, in.firstElement().createVariable());
    }
    final Op op = ops.op(Ops.Filter.Convolve.class, out, in, kernel);
    if (in.numDimensions() > kernel.numDimensions()) {
        if (op instanceof UnaryComputerOp) {
            // if the selected convolve op is a function and the kernel dimensions
            // doesn't match the input image dimensions, than we can still convolve
            // each slice individually
            ops.run(Ops.Slice.class, out, in, op, axisIndices);
        } else {
            throw new IllegalArgumentException("The input image has more dimensions than the kernel!");
        }
    } else if (in.numDimensions() == kernel.numDimensions()) {
        // no 'slicing' necessary
        ops.run(op, out, in, kernel);
    }
}
Also used : Op(net.imagej.ops.Op) UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) Ops(net.imagej.ops.Ops) IncompatibleTypeException(net.imglib2.exception.IncompatibleTypeException) FloatType(net.imglib2.type.numeric.real.FloatType) ItemIO(org.scijava.ItemIO)

Example 2 with IncompatibleTypeException

use of net.imglib2.exception.IncompatibleTypeException in project imagej-ops by imagej.

the class DefaultTubeness method compute.

@Override
public void compute(final RandomAccessibleInterval<T> input, final IterableInterval<DoubleType> tubeness) {
    cancelReason = null;
    final int numDimensions = input.numDimensions();
    // Sigmas in pixel units.
    final double[] sigmas = new double[numDimensions];
    for (int d = 0; d < sigmas.length; d++) {
        final double cal = d < calibration.length ? calibration[d] : 1;
        sigmas[d] = sigma / cal;
    }
    /*
		 * Hessian.
		 */
    // Get a suitable image factory.
    final long[] gradientDims = new long[numDimensions + 1];
    final long[] hessianDims = new long[numDimensions + 1];
    for (int d = 0; d < numDimensions; d++) {
        hessianDims[d] = input.dimension(d);
        gradientDims[d] = input.dimension(d);
    }
    hessianDims[numDimensions] = numDimensions * (numDimensions + 1) / 2;
    gradientDims[numDimensions] = numDimensions;
    final Dimensions hessianDimensions = FinalDimensions.wrap(hessianDims);
    final FinalDimensions gradientDimensions = FinalDimensions.wrap(gradientDims);
    final ImgFactory<DoubleType> factory = ops().create().imgFactory(hessianDimensions);
    final Img<DoubleType> hessian = factory.create(hessianDimensions, new DoubleType());
    final Img<DoubleType> gradient = factory.create(gradientDimensions, new DoubleType());
    final Img<DoubleType> gaussian = factory.create(input, new DoubleType());
    // Handle multithreading.
    final int nThreads = Runtime.getRuntime().availableProcessors();
    final ExecutorService es = threadService.getExecutorService();
    try {
        // Hessian calculation.
        HessianMatrix.calculateMatrix(Views.extendBorder(input), gaussian, gradient, hessian, new OutOfBoundsBorderFactory<>(), nThreads, es, sigma);
        statusService.showProgress(1, 3);
        if (isCanceled())
            return;
        // Hessian eigenvalues.
        final RandomAccessibleInterval<DoubleType> evs = TensorEigenValues.calculateEigenValuesSymmetric(hessian, TensorEigenValues.createAppropriateResultImg(hessian, factory, new DoubleType()), nThreads, es);
        statusService.showProgress(2, 3);
        if (isCanceled())
            return;
        final AbstractUnaryComputerOp<Iterable<DoubleType>, DoubleType> method;
        switch(numDimensions) {
            case 2:
                method = new Tubeness2D(sigma);
                break;
            case 3:
                method = new Tubeness3D(sigma);
                break;
            default:
                System.err.println("Cannot compute tubeness for " + numDimensions + "D images.");
                return;
        }
        ops().transform().project(tubeness, evs, method, numDimensions);
        statusService.showProgress(3, 3);
        return;
    } catch (final IncompatibleTypeException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
        return;
    }
}
Also used : Dimensions(net.imglib2.Dimensions) FinalDimensions(net.imglib2.FinalDimensions) FinalDimensions(net.imglib2.FinalDimensions) DoubleType(net.imglib2.type.numeric.real.DoubleType) ExecutorService(java.util.concurrent.ExecutorService) IncompatibleTypeException(net.imglib2.exception.IncompatibleTypeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with IncompatibleTypeException

use of net.imglib2.exception.IncompatibleTypeException in project imagej-ops by imagej.

the class LocalThresholdTest method testLocalMeanResultsConsistency.

/**
 * @see LocalMeanThresholdIntegral
 * @see LocalMeanThreshold
 */
@Test
public void testLocalMeanResultsConsistency() {
    Img<BitType> out2 = null;
    Img<BitType> out3 = null;
    try {
        out2 = in.factory().imgFactory(new BitType()).create(in, new BitType());
        out3 = in.factory().imgFactory(new BitType()).create(in, new BitType());
    } catch (IncompatibleTypeException exc) {
        exc.printStackTrace();
    }
    // Default implementation
    ops.run(LocalMeanThreshold.class, out2, in, new RectangleShape(2, false), new OutOfBoundsMirrorFactory<ByteType, Img<ByteType>>(Boundary.SINGLE), 0.0);
    // Integral image-based implementation
    ops.run(LocalMeanThresholdIntegral.class, out3, in, new RectangleShape(2, false), new OutOfBoundsMirrorFactory<ByteType, Img<ByteType>>(Boundary.SINGLE), 0.0);
    testIterableIntervalSimilarity(out2, out3);
}
Also used : Img(net.imglib2.img.Img) ArrayImg(net.imglib2.img.array.ArrayImg) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) BitType(net.imglib2.type.logic.BitType) IncompatibleTypeException(net.imglib2.exception.IncompatibleTypeException) ByteType(net.imglib2.type.numeric.integer.ByteType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 4 with IncompatibleTypeException

use of net.imglib2.exception.IncompatibleTypeException in project imagej-ops by imagej.

the class LocalThresholdTest method testLocalSauvolaResultsConsistency.

/**
 * @see LocalSauvolaThresholdIntegral
 * @see LocalSauvolaThreshold
 */
@Test
public void testLocalSauvolaResultsConsistency() {
    Img<BitType> out2 = null;
    Img<BitType> out3 = null;
    try {
        out2 = in.factory().imgFactory(new BitType()).create(in, new BitType());
        out3 = in.factory().imgFactory(new BitType()).create(in, new BitType());
    } catch (IncompatibleTypeException exc) {
        exc.printStackTrace();
    }
    // Default implementation
    ops.run(LocalSauvolaThreshold.class, out2, normalizedIn, new RectangleShape(2, false), new OutOfBoundsMirrorFactory<ByteType, Img<ByteType>>(Boundary.SINGLE), 0.5, 0.5);
    // Integral image-based implementation
    ops.run(LocalSauvolaThresholdIntegral.class, out3, normalizedIn, new RectangleShape(2, false), new OutOfBoundsMirrorFactory<ByteType, Img<ByteType>>(Boundary.SINGLE), 0.5, 0.5);
    testIterableIntervalSimilarity(out2, out3);
}
Also used : Img(net.imglib2.img.Img) ArrayImg(net.imglib2.img.array.ArrayImg) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) BitType(net.imglib2.type.logic.BitType) IncompatibleTypeException(net.imglib2.exception.IncompatibleTypeException) ByteType(net.imglib2.type.numeric.integer.ByteType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 5 with IncompatibleTypeException

use of net.imglib2.exception.IncompatibleTypeException in project imagej-ops by imagej.

the class ApplyManualThresholdTest method testApplyThreshold.

@Test
public void testApplyThreshold() throws IncompatibleTypeException {
    final Img<BitType> out = bitmap();
    final UnsignedShortType threshold = new UnsignedShortType(30000);
    ops.run(ApplyManualThreshold.class, out, in, threshold);
    assertCount(out, 54);
}
Also used : BitType(net.imglib2.type.logic.BitType) UnsignedShortType(net.imglib2.type.numeric.integer.UnsignedShortType) Test(org.junit.Test) AbstractThresholdTest(net.imagej.ops.threshold.AbstractThresholdTest)

Aggregations

IncompatibleTypeException (net.imglib2.exception.IncompatibleTypeException)6 BitType (net.imglib2.type.logic.BitType)5 Test (org.junit.Test)5 AbstractOpTest (net.imagej.ops.AbstractOpTest)4 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)4 Img (net.imglib2.img.Img)4 ArrayImg (net.imglib2.img.array.ArrayImg)4 ByteType (net.imglib2.type.numeric.integer.ByteType)4 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Op (net.imagej.ops.Op)1 Ops (net.imagej.ops.Ops)1 UnaryComputerOp (net.imagej.ops.special.computer.UnaryComputerOp)1 AbstractThresholdTest (net.imagej.ops.threshold.AbstractThresholdTest)1 Dimensions (net.imglib2.Dimensions)1 FinalDimensions (net.imglib2.FinalDimensions)1 UnsignedShortType (net.imglib2.type.numeric.integer.UnsignedShortType)1 DoubleType (net.imglib2.type.numeric.real.DoubleType)1 FloatType (net.imglib2.type.numeric.real.FloatType)1 ItemIO (org.scijava.ItemIO)1