use of net.imglib2.img.array.ArrayImgFactory 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.img.array.ArrayImgFactory in project imagej-ops by imagej.
the class ColocalisationTest method gaussianSmooth.
/**
* Gaussian Smooth of the input image using intermediate float format.
*
* @param <T>
* @param img
* @param sigma
* @return
*/
public static <T extends RealType<T> & NativeType<T>> Img<T> gaussianSmooth(RandomAccessibleInterval<T> img, double[] sigma) {
Interval interval = Views.iterable(img);
ImgFactory<T> outputFactory = new ArrayImgFactory<T>();
final long[] dim = new long[img.numDimensions()];
img.dimensions(dim);
Img<T> output = outputFactory.create(dim, img.randomAccess().get().createVariable());
final long[] pos = new long[img.numDimensions()];
Arrays.fill(pos, 0);
Localizable origin = new Point(pos);
ImgFactory<FloatType> tempFactory = new ArrayImgFactory<FloatType>();
RandomAccessible<T> input = Views.extendMirrorSingle(img);
Gauss.inFloat(sigma, input, interval, output, origin, tempFactory);
return output;
}
use of net.imglib2.img.array.ArrayImgFactory in project imagej-ops by imagej.
the class ColocalisationTest method produceMeanBasedNoiseImage.
/**
* This method creates a noise image that has a specified mean. Every pixel
* has a value uniformly distributed around mean with the maximum spread
* specified.
*
* @return IllegalArgumentException if specified means and spreads are not
* valid
*/
public static <T extends RealType<T> & NativeType<T>> Img<T> produceMeanBasedNoiseImage(T type, int width, int height, double mean, double spread, double[] smoothingSigma, long seed) throws IllegalArgumentException {
if (mean < spread || (mean + spread) > type.getMaxValue()) {
throw new IllegalArgumentException("Mean must be larger than spread, and mean plus spread must be smaller than max of the type");
}
// create the new image
ImgFactory<T> imgFactory = new ArrayImgFactory<T>();
Img<T> noiseImage = imgFactory.create(new int[] { width, height }, // "Noise image");
type);
Random r = new Random(seed);
for (T value : Views.iterable(noiseImage)) {
value.setReal(mean + ((r.nextDouble() - 0.5) * spread));
}
// TO DO: call Ops
return gaussianSmooth(noiseImage, smoothingSigma);
// filter.gaus instead
}
use of net.imglib2.img.array.ArrayImgFactory 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.img.array.ArrayImgFactory in project imagej-ops by imagej.
the class CopyArrayImgTest method createData.
@Before
public void createData() {
input = new ArrayImgFactory<UnsignedByteType>().create(new int[] { 120, 100 }, new UnsignedByteType());
final Random r = new Random(System.currentTimeMillis());
final Cursor<UnsignedByteType> inc = input.cursor();
while (inc.hasNext()) {
inc.next().setReal(r.nextDouble() * 255);
}
}
Aggregations