use of net.imglib2.Dimensions in project imagej-ops by imagej.
the class MapTest method testIIAndIIInplaceParallel.
@Test
public void testIIAndIIInplaceParallel() {
final Img<ByteType> first = generateByteArrayTestImg(true, 10, 10);
final Img<ByteType> firstCopy = first.copy();
final Img<ByteType> second = generateByteArrayTestImg(false, 10, 10);
for (final ByteType px : second) px.set((byte) 1);
final Img<ByteType> secondCopy = second.copy();
final Img<ByteType> secondDiffDims = generateByteArrayTestImg(false, 10, 10, 2);
sub = Inplaces.binary(ops, Ops.Math.Subtract.class, ByteType.class);
final BinaryInplaceOp<? super Img<ByteType>, Img<ByteType>> map = Inplaces.binary(ops, MapIIAndIIInplaceParallel.class, firstCopy, second, sub);
map.run(firstCopy, second, firstCopy);
map.run(first, secondCopy, secondCopy);
assertImgSubEquals(first, second, firstCopy);
assertImgSubEquals(first, second, secondCopy);
// Expect exception when in2 has different dimensions
thrown.expect(IllegalArgumentException.class);
ops.op(MapIIAndIIInplaceParallel.class, first, secondDiffDims, sub);
}
use of net.imglib2.Dimensions in project imagej-ops by imagej.
the class ConvolveTest method testConvolve.
/**
* tests fft based convolve
*/
@Test
public void testConvolve() {
float delta = 0.0001f;
int[] size = new int[] { 225, 167 };
int[] kernelSize = new int[] { 27, 39 };
long[] borderSize = new long[] { 10, 10 };
// create an input with a small sphere at the center
Img<FloatType> in = new ArrayImgFactory<FloatType>().create(size, new FloatType());
placeSphereInCenter(in);
// create a kernel with a small sphere in the center
Img<FloatType> kernel = new ArrayImgFactory<FloatType>().create(kernelSize, new FloatType());
placeSphereInCenter(kernel);
// create variables to hold the image sums
FloatType inSum = new FloatType();
FloatType kernelSum = new FloatType();
FloatType outSum = new FloatType();
FloatType outSum2 = new FloatType();
FloatType outSum3 = new FloatType();
// calculate sum of input and kernel
ops.stats().sum(inSum, in);
ops.stats().sum(kernelSum, kernel);
// convolve and calculate the sum of output
@SuppressWarnings("unchecked") final Img<FloatType> out = (Img<FloatType>) ops.run(ConvolveFFTF.class, in, kernel, borderSize);
// create an output for the next test
Img<FloatType> out2 = new ArrayImgFactory<FloatType>().create(size, new FloatType());
// create an output for the next test
Img<FloatType> out3 = new ArrayImgFactory<FloatType>().create(size, new FloatType());
// Op used to pad the input
final BinaryFunctionOp<RandomAccessibleInterval<FloatType>, Dimensions, RandomAccessibleInterval<FloatType>> padOp = (BinaryFunctionOp) Functions.binary(ops, PadInputFFTMethods.class, RandomAccessibleInterval.class, RandomAccessibleInterval.class, Dimensions.class, true);
// Op used to pad the kernel
final BinaryFunctionOp<RandomAccessibleInterval<FloatType>, Dimensions, RandomAccessibleInterval<FloatType>> padKernelOp = (BinaryFunctionOp) Functions.binary(ops, PadShiftKernelFFTMethods.class, RandomAccessibleInterval.class, RandomAccessibleInterval.class, Dimensions.class, true);
// Op used to create the complex FFTs
UnaryFunctionOp<Dimensions, RandomAccessibleInterval<ComplexFloatType>> createOp = (UnaryFunctionOp) Functions.unary(ops, CreateOutputFFTMethods.class, RandomAccessibleInterval.class, Dimensions.class, new ComplexFloatType(), true);
final int numDimensions = in.numDimensions();
// 1. Calculate desired extended size of the image
final long[] paddedSize = new long[numDimensions];
// if no getBorderSize() was passed in, then extend based on kernel size
for (int d = 0; d < numDimensions; ++d) {
paddedSize[d] = (int) in.dimension(d) + (int) kernel.dimension(d) - 1;
}
RandomAccessibleInterval<FloatType> paddedInput = padOp.calculate(in, new FinalDimensions(paddedSize));
RandomAccessibleInterval<FloatType> paddedKernel = padKernelOp.calculate(kernel, new FinalDimensions(paddedSize));
RandomAccessibleInterval<ComplexFloatType> fftImage = createOp.calculate(new FinalDimensions(paddedSize));
RandomAccessibleInterval<ComplexFloatType> fftKernel = createOp.calculate(new FinalDimensions(paddedSize));
// run convolve using the rai version with the memory created above
ops.run(ConvolveFFTC.class, out2, paddedInput, paddedKernel, fftImage, fftKernel);
ops.run(ConvolveFFTC.class, out3, paddedInput, paddedKernel, fftImage, fftKernel, true, false);
ops.stats().sum(outSum, Views.iterable(out));
ops.stats().sum(outSum2, out2);
ops.stats().sum(outSum3, out3);
// multiply input sum by kernelSum and assert it is the same as outSum
inSum.mul(kernelSum);
assertEquals(inSum.get(), outSum.get(), delta);
assertEquals(inSum.get(), outSum2.get(), delta);
assertEquals(inSum.get(), outSum3.get(), delta);
assertEquals(size[0], out.dimension(0));
assertEquals(size[0], out2.dimension(0));
}
use of net.imglib2.Dimensions in project imagej-ops by imagej.
the class PartialDerivativeFilterTest method testAllDerivatives.
@Test
public void testAllDerivatives() {
Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20, 3 });
Cursor<FloatType> cursorImg = img.cursor();
int counterX = 0;
int counterY = 0;
while (cursorImg.hasNext()) {
if (counterX > 8 && counterX < 12 || counterY > 8 && counterY < 12) {
cursorImg.next().setOne();
} else {
cursorImg.next().setZero();
}
counterX++;
if (counterX % 20 == 0) {
counterY++;
}
if (counterX == 20) {
counterX = 0;
}
if (counterY == 20) {
counterY = 0;
}
}
CompositeIntervalView<FloatType, RealComposite<FloatType>> out = ops.filter().allPartialDerivatives(img);
CompositeView<FloatType, RealComposite<FloatType>>.CompositeRandomAccess outRA = out.randomAccess();
FloatType type = Util.getTypeFromInterval(img).createVariable();
// position 9,8 in all dimensions
outRA.setPosition(new int[] { 9, 8, 0 });
RealComposite<FloatType> outvalue = outRA.get();
Float[] correctValues = new Float[] { 12.0f, 4.0f, 0.0f };
int i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 8, 1 });
outvalue = outRA.get();
correctValues = new Float[] { 12.0f, 4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 8, 2 });
outvalue = outRA.get();
correctValues = new Float[] { 12.0f, 4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
// position 9,9 in all dimensions
outRA.setPosition(new int[] { 9, 9, 0 });
outvalue = outRA.get();
correctValues = new Float[] { 4.0f, 4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 9, 1 });
outvalue = outRA.get();
correctValues = new Float[] { 4.0f, 4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 9, 2 });
outvalue = outRA.get();
correctValues = new Float[] { 4.0f, 4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
// position 9,10 in all dimensions
outRA.setPosition(new int[] { 9, 10, 0 });
outvalue = outRA.get();
correctValues = new Float[] { 0.0f, 0.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 10, 1 });
outvalue = outRA.get();
correctValues = new Float[] { 0.0f, 0.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 10, 2 });
outvalue = outRA.get();
correctValues = new Float[] { 0.0f, 0.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
// position 9,11 in all dimensions
outRA.setPosition(new int[] { 9, 11, 0 });
outvalue = outRA.get();
correctValues = new Float[] { 4.0f, -4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 11, 1 });
outvalue = outRA.get();
correctValues = new Float[] { 4.0f, -4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
outRA.setPosition(new int[] { 9, 11, 2 });
outvalue = outRA.get();
correctValues = new Float[] { 4.0f, -4.0f, 0.0f };
i = 0;
for (FloatType value : outvalue) {
type.set(correctValues[i]);
assertEquals(type, value);
i++;
}
}
use of net.imglib2.Dimensions in project imagej-ops by imagej.
the class CreateImgTest method testImageFactory.
@Test
public void testImageFactory() {
final Dimensions dim = new FinalDimensions(10, 10, 10);
@SuppressWarnings("unchecked") final Img<DoubleType> arrayImg = (Img<DoubleType>) ops.run(CreateImgFromDimsAndType.class, dim, new DoubleType(), new ArrayImgFactory<DoubleType>());
final Class<?> arrayFactoryClass = arrayImg.factory().getClass();
assertEquals("Image Factory: ", ArrayImgFactory.class, arrayFactoryClass);
@SuppressWarnings("unchecked") final Img<DoubleType> cellImg = (Img<DoubleType>) ops.run(CreateImgFromDimsAndType.class, dim, new DoubleType(), new CellImgFactory<DoubleType>());
final Class<?> cellFactoryClass = cellImg.factory().getClass();
assertEquals("Image Factory: ", CellImgFactory.class, cellFactoryClass);
}
use of net.imglib2.Dimensions in project imagej-ops by imagej.
the class CreateImgTest method testImageDimensions.
@Test
public void testImageDimensions() {
final Random randomGenerator = new Random();
for (int i = 0; i < TEST_SIZE; i++) {
// between 2 and 5 dimensions
final long[] dim = new long[randomGenerator.nextInt(4) + 2];
// between 2 and 10 pixels per dimensions
for (int j = 0; j < dim.length; j++) {
dim[j] = randomGenerator.nextInt(9) + 2;
}
// create img
@SuppressWarnings("unchecked") final Img<DoubleType> img = (Img<DoubleType>) ops.run(CreateImgFromDimsAndType.class, dim, new DoubleType());
assertArrayEquals("Image Dimensions:", dim, Intervals.dimensionsAsLongArray(img));
}
}
Aggregations