Java Code Examples for net.imglib2.img.Img#randomAccess()
The following examples show how to use
net.imglib2.img.Img#randomAccess() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java From sciview with BSD 2-Clause "Simplified" License | 6 votes |
static public Img<ARGBType> convertToARGB(Img<UnsignedByteType> screenshot) { Img<ARGBType> out = ArrayImgs.argbs(screenshot.dimension(0), screenshot.dimension(1)); long[] pos = new long[3]; Cursor<ARGBType> outCur = Views.iterable(out).cursor(); RandomAccess<UnsignedByteType> sRA = screenshot.randomAccess(); while( outCur.hasNext() ) { outCur.fwd(); outCur.localize(pos); pos[2] = 0; sRA.setPosition(pos); int r = sRA.get().get(); pos[2] = 1; sRA.setPosition(pos); int g = sRA.get().get(); pos[2] = 2; sRA.setPosition(pos); int b = sRA.get().get(); int a = 255;// FIXME outCur.get().set(ARGBType.rgba(r, g, b, a)); } return out; }
Example 2
Source File: BoxCountTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testOneVoxel() { // SETUP final PrimitiveIterator.OfDouble sizes = DoubleStream.of(9, 3, 1).map( i -> -Math.log(i)).iterator(); final PrimitiveIterator.OfDouble counts = DoubleStream.of(1, 1, 1).map( Math::log).iterator(); final Img<BitType> img = ArrayImgs.bits(9, 9, 9); final RandomAccess<BitType> access = img.randomAccess(); access.setPosition(new long[] { 4, 4, 4 }); access.get().setOne(); // EXECUTE final List<ValuePair<DoubleType, DoubleType>> points = ops.topology() .boxCount(img, 9L, 3L, 3.0); // VERIFY points.forEach(p -> { assertEquals(p.a.get(), sizes.next(), 1e-12); assertEquals(p.b.get(), counts.next(), 1e-12); }); }
Example 3
Source File: EulerCharacteristic26NFloatingTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
/** * Test with a cube that has a "handle" * <p> * Here χ = β_0 - β_1 + β_2 = 1 - 1 + 0 = 0 * </p> */ @Test public void testHandleCube() throws Exception { final Img<BitType> cube = drawCube(9, 9, 9, 5); final RandomAccess<BitType> access = cube.randomAccess(); // Draw a handle on the front xy-face of the cuboid access.setPosition(9, 0); access.setPosition(6, 1); access.setPosition(4, 2); access.get().setOne(); access.setPosition(3, 2); access.get().setOne(); access.setPosition(7, 1); access.get().setOne(); access.setPosition(8, 1); access.get().setOne(); access.setPosition(4, 2); access.get().setOne(); final double result = ops.topology().eulerCharacteristic26NFloating(cube).get(); assertEquals("Euler characteristic (χ) is incorrect", 0.0, result, 1e-12); }
Example 4
Source File: InvertTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
private <T extends RealType<T>> void defaultCompare( final Img<T> in, final Img<T> out, final T min, final T max) { final Cursor<T> inAccess = in.localizingCursor(); final RandomAccess<T> outAccess = out.randomAccess(); while (inAccess.hasNext()) { final T inVal = inAccess.next(); outAccess.setPosition(inAccess); final T outVal = outAccess.get(); final double bigIn = inVal.getRealDouble(); final double minMax = min.getRealDouble() + max.getRealDouble() - bigIn; final double bigOut = outVal.getRealDouble(); final T minMaxType = outVal.createVariable(); minMaxType.setReal(minMax); if (minMax <= outVal.getMinValue()) assertEquals(outVal.getMinValue(), bigOut, 0.00005); else if (minMax >= outVal.getMaxValue()) assertEquals(outVal .getMaxValue(), bigOut, 0.00005); else assertEquals(minMaxType, outVal); } }
Example 5
Source File: MergeLabelingTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testMask() { final Img<BitType> mask = ops.create().img(in1, new BitType()); final RandomAccess<BitType> maskRA = mask.randomAccess(); maskRA.setPosition(new int[] { 0, 0 }); maskRA.get().set(true); maskRA.setPosition(new int[] { 1, 1 }); maskRA.get().set(true); @SuppressWarnings("unchecked") final MergeLabeling<Integer, ByteType, BitType> op = ops.op( MergeLabeling.class, out, in1, in2, mask); op.compute(in1, in2, out); final RandomAccess<LabelingType<Integer>> outRA = out.randomAccess(); outRA.setPosition(new int[] { 0, 0 }); assertTrue(outRA.get().contains(0)); assertTrue(outRA.get().contains(10)); outRA.setPosition(new int[] { 0, 1 }); assertTrue(outRA.get().isEmpty()); }
Example 6
Source File: ScaleViewTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testScaling() { Img<ByteType> in = generateByteArrayTestImg(true, new long[] { 10, 10 }); double[] scaleFactors = new double[] { 2, 2 }; @SuppressWarnings("unchecked") RandomAccessibleInterval<ByteType> out = (RandomAccessibleInterval<ByteType>) ops.run(DefaultScaleView.class, in, scaleFactors, new NLinearInterpolatorFactory<ByteType>()); assertEquals(out.dimension(0), 20); assertEquals(out.dimension(1), 20); RandomAccess<ByteType> inRA = in.randomAccess(); RandomAccess<ByteType> outRA = out.randomAccess(); inRA.setPosition(new long[] { 5, 5 }); outRA.setPosition(new long[] { 10, 10 }); assertEquals(inRA.get().get(), outRA.get().get()); }
Example 7
Source File: Fusion.java From Stitching with GNU General Public License v2.0 | 6 votes |
public TileProcessor(int threadNumber, List<ArrayList<RealRandomAccess<? extends RealType<?>>>> interpolators, ArrayList<? extends ImageInterpolation<? extends RealType<?>>> input, Vector<Chunk> threadChunks, int numImages, Img<T> output, PixelFusion fusion, ClassifiedRegion[] currentTile, ArrayList<InvertibleBoundable> transform, ImagePlus[] fusionImp, int[] count, double positionsPerThread, double[] offset, int[] loopDim) { this.threadNumber = threadNumber; this.threadChunks = threadChunks; this.currentTile = currentTile; this.transform = transform; this.fusionImp = fusionImp; this.count = count; this.positionsPerThread = positionsPerThread; this.offset = offset; this.loopDim = loopDim; in = getThreadInterpolators(interpolators, threadNumber, input, numImages); inPos = new double[numImages][output.numDimensions()]; myFusion = fusion.copy(); out = output.randomAccess(); }
Example 8
Source File: FrangiVesselnessTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void regressionTest() throws Exception { // load in input image and expected output image. Img<FloatType> inputImg = (Img<FloatType>) ops.run( net.imagej.ops.image.equation.DefaultEquation.class, "Math.tan(0.3*p[0]) + Math.tan(0.1*p[1])"); Img<FloatType> expectedOutput = ((Img<FloatType>) openFloatImg( "Result.tif")); // create ouput image long[] dims = new long[inputImg.numDimensions()]; inputImg.dimensions(dims); Img<FloatType> actualOutput = ArrayImgs.floats(dims); // scale over which the filter operates (sensitivity) int scale = 1; // physical spacing between data points (1,1 since I got it from the // computer) double[] spacing = { 1, 1 }; // run the op ops.run(net.imagej.ops.filter.vesselness.DefaultFrangi.class, actualOutput, inputImg, spacing, scale); // compare the output image data to that stored in the file. Cursor<FloatType> cursor = Views.iterable(actualOutput).localizingCursor(); RandomAccess<FloatType> actualRA = actualOutput.randomAccess(); RandomAccess<FloatType> expectedRA = expectedOutput.randomAccess(); while (cursor.hasNext()) { cursor.fwd(); actualRA.setPosition(cursor); expectedRA.setPosition(cursor); assertEquals(expectedRA.get().get(), actualRA.get().get(), 0); } }
Example 9
Source File: DefaultDerivativeGaussTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void regressionTest() { final int width = 10; final Img<DoubleType> input = ops.convert().float64( generateFloatArrayTestImg(false, width, width)); final Img<DoubleType> output = ops.create().img(input); // Draw a line on the image final RandomAccess<DoubleType> inputRA = input.randomAccess(); inputRA.setPosition(5, 0); for (int i = 0; i < 10; i++) { inputRA.setPosition(i, 1); inputRA.get().set(255); } // filter the image final int[] derivatives = new int[] { 1, 0 }; final double[] sigmas = new double[] { 0.5, 0.5 }; ops.filter().derivativeGauss(output, input, derivatives, sigmas); final Cursor<DoubleType> cursor = output.localizingCursor(); int currentPixel = 0; while (cursor.hasNext()) { cursor.fwd(); assertEquals(cursor.get().getRealDouble(), regressionRowValues[currentPixel % width], 0); currentPixel++; } }
Example 10
Source File: WatershedSeededTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void test() { long[] dims = { 15, 30 }; // create input image Img<FloatType> input = ArrayImgs.floats(dims); MersenneTwisterFast random = new MersenneTwisterFast(SEED); for (FloatType b : input) { b.setReal(random.nextDouble()); } // create 3 seeds Img<BitType> bits = ArrayImgs.bits(dims); RandomAccess<BitType> ra = bits.randomAccess(); ra.setPosition(new int[] { 0, 0 }); ra.get().set(true); ra.setPosition(new int[] { 4, 6 }); ra.get().set(true); ra.setPosition(new int[] { 10, 20 }); ra.get().set(true); // compute labeled seeds final ImgLabeling<Integer, IntType> labeledSeeds = ops.labeling().cca(bits, StructuringElement.EIGHT_CONNECTED); testWithoutMask(input, labeledSeeds); testWithMask(input, labeledSeeds); }
Example 11
Source File: HistogramOfOrientedGradients2DTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void test() { Img<FloatType> hogTestImg = openFloatImg("HoG2DResult.tif"); Img<FloatType> hogInputImg = openFloatImg("HoG2DInput.png"); // use numOrientations = 9 and spanOfNeighborhood = 2 for test @SuppressWarnings("unchecked") RandomAccessibleInterval<FloatType> hogOp = (RandomAccessibleInterval<FloatType>) ops .run(HistogramOfOrientedGradients2D.class, null, hogInputImg, 9, 2); RandomAccess<FloatType> raOp = hogOp.randomAccess(); RandomAccess<FloatType> raTest = hogTestImg.randomAccess(); // check dimensions assertEquals(hogTestImg.numDimensions(), hogOp.numDimensions()); assertEquals(hogTestImg.dimension(0), hogOp.dimension(0)); assertEquals(hogTestImg.dimension(1), hogOp.dimension(1)); assertEquals(hogTestImg.dimension(2), hogOp.dimension(2)); // check pixel values for (int i = 0; i < hogTestImg.dimension(0); i++) { for (int j = 0; j < hogTestImg.dimension(1); j++) { for (int k = 0; k < hogTestImg.dimension(2); k++) { raTest.setPosition(new long[] { i, j, k }); raOp.setPosition(new long[] { i, j, k }); assertEquals("i=" + i + ", j=" + j + ", k=" + k, raTest.get().getRealFloat(), raOp.get().getRealFloat(), EPSILON); } } } }
Example 12
Source File: CoordinateEquationTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 5 votes |
/** * Test the coordinate op version of the equation using 4 dimensions */ @Test public void testEquation4DOp() { final long[] size4D = new long[] { 5, 5, 5, 5 }; final Dimensions dimensions4D = new FinalDimensions(size4D); final Img<ShortType> image = ops.create().img(dimensions4D, new ShortType()); // implement c[0]+10*c[1]+100*c[3]+1000*c[4] final UnaryFunctionOp<long[], Double> op = new AbstractUnaryFunctionOp<long[], Double>() { @Override public Double calculate(final long[] coords) { final double result = coords[0] + 10 * coords[1] + 100 * coords[2] + 1000 * coords[3]; return result; } }; ops.run(DefaultCoordinatesEquation.class, image, op); final RandomAccess<ShortType> ra = image.randomAccess(); ra.setPosition(new long[] { 1, 2, 2, 3 }); assertEquals(1 + 10 * 2 + 100 * 2 + 1000 * 3, ra.get().getRealFloat(), 0.000001); }
Example 13
Source File: EulerCharacteristic26NFloatingTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 5 votes |
/** * Test with a cube that has a cavity inside * <p> * Here χ = β_0 - β_1 + β_2 = 1 - 0 + 1 = 2 * </p> */ @Test public void testHollowCube() throws Exception { final Img<BitType> img = drawCube(3, 3, 3, 1); final RandomAccess<BitType> access = img.randomAccess(); // Add cavity access.setPosition(new long[]{2, 2, 2}); access.get().setZero(); final double result = ops.topology().eulerCharacteristic26NFloating(img).get(); assertEquals("Euler characteristic (χ) is incorrect", 2.0, result, 1e-12); }
Example 14
Source File: Coloc_2.java From Colocalisation_Analysis with GNU General Public License v3.0 | 5 votes |
private RandomAccessibleInterval<T> project( final RandomAccessibleInterval<T> image) { if (image.numDimensions() < 2) { throw new IllegalArgumentException("Dimensionality too small: " + // image.numDimensions()); } final IterableInterval<T> input = Views.iterable(image); final T type = input.firstElement(); // e.g. unsigned 8-bit final long xLen = image.dimension(0); final long yLen = image.dimension(1); // initialize output image with minimum value of the pixel type final long[] outputDims = { xLen, yLen }; final Img<T> output = new ArrayImgFactory<T>().create(outputDims, type); for (final T sample : output) { sample.setReal(type.getMinValue()); } // loop over the input image, performing the max projection final Cursor<T> inPos = input.localizingCursor(); final RandomAccess<T> outPos = output.randomAccess(); while (inPos.hasNext()) { final T inPix = inPos.next(); final long xPos = inPos.getLongPosition(0); final long yPos = inPos.getLongPosition(1); outPos.setPosition(xPos, 0); outPos.setPosition(yPos, 1); final T outPix = outPos.get(); if (outPix.compareTo(inPix) < 0) { outPix.set(inPix); } } return output; }
Example 15
Source File: Fusion.java From Stitching with GNU General Public License v2.0 | 4 votes |
/** * Fuse one slice/volume (one channel) * * @param output - same the type of the ImagePlus input * @param input - FloatType, because of Interpolation that needs to be done * @param transform - the transformation */ protected static <T extends RealType<T>> void fuseBlockNoOverlap( final Img<T> output, final ArrayList< ? extends ImageInterpolation< ? extends RealType< ? > > > input, final double[] offset, final ArrayList< InvertibleBoundable > transform, final boolean displayFusion ) { final int numDimensions = output.numDimensions(); final int numImages = input.size(); // run multithreaded final AtomicInteger ai = new AtomicInteger(0); final Thread[] threads = SimpleMultiThreading.newThreads( numImages ); for (int ithread = 0; ithread < threads.length; ++ithread) threads[ithread] = new Thread( new Runnable() { @Override public void run() { // Thread ID final int myImage = ai.getAndIncrement(); // only the first thread does preview and update the status bar // this requires no synchronized stuff long lastDraw = 0; ImagePlus fusionImp = null; if ( displayFusion && myImage == 0 ) { try { fusionImp = ((ImagePlusImg<?, ?>) output).getImagePlus(); fusionImp.setTitle( "fusing..." ); fusionImp.show(); } catch ( ImgLibException e ) { Log.error( "Output image has no ImageJ type: " + e ); } } final Img< ? extends RealType<?> > image = input.get( myImage ).getImg(); final int[] translation = new int[ numDimensions ]; final InvertibleBoundable t = transform.get( myImage ); final double[] tmp = new double[ numDimensions ]; t.applyInPlace( tmp ); for ( int d = 0; d < numDimensions; ++d ) translation[ d ] = (int) Math.round( tmp[ d ] ); final Cursor< ? extends RealType<?> > cursor = image.localizingCursor(); final RandomAccess< ? extends RealType<?> > randomAccess = output.randomAccess(); final int[] pos = new int[ numDimensions ]; int j = 0; while ( cursor.hasNext() ) { cursor.fwd(); cursor.localize( pos ); // just thread 0 if ( myImage == 0 ) { // just every 10000'th pixel if ( j++ % 10000 == 0 ) { lastDraw = drawFusion( lastDraw, fusionImp ); IJ.showProgress( (double)j / (double)image.size() ); } } for ( int d = 0; d < numDimensions; ++d ) { pos[ d ] += translation[ d ]; pos[ d ] -= offset[ d ]; } randomAccess.setPosition( pos ); randomAccess.get().setReal( cursor.get().getRealFloat() ); } if ( fusionImp != null ) fusionImp.hide(); } }); SimpleMultiThreading.startAndJoin( threads ); }
Example 16
Source File: WatershedTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
@SuppressWarnings("unchecked") private void testWithMask(final RandomAccessibleInterval<FloatType> in) { // create mask which is 1 everywhere long[] dims = new long[in.numDimensions()]; in.dimensions(dims); Img<BitType> mask = ArrayImgs.bits(dims); RandomAccess<BitType> raMask = mask.randomAccess(); for (BitType b : mask) { b.setZero(); } for (int x = 0; x < dims[0] / 2; x++) { for (int y = 0; y < dims[1] / 2; y++) { raMask.setPosition(new int[] { x, y }); raMask.get().setOne(); } } /* * use 8-connected neighborhood */ // compute result without watersheds ImgLabeling<Integer, IntType> out = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, true, false, mask); assertResults(in, out, mask, true, false, true); // compute result with watersheds ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, true, true, mask); assertResults(in, out2, mask, true, true, true); /* * use 4-connected neighborhood */ // compute result without watersheds ImgLabeling<Integer, IntType> out3 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, false, false, mask); assertResults(in, out3, mask, false, false, true); // compute result with watersheds ImgLabeling<Integer, IntType> out4 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, false, true, mask); assertResults(in, out4, mask, false, true, true); }
Example 17
Source File: ReadmeExampleTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
public double getPixel(final String imageName, int x, int y) { final Img<DoubleType> img = images.get(imageName); final RandomAccess<DoubleType> access = img.randomAccess(); access.setPosition(new int[] { x, y }); return access.get().get(); }
Example 18
Source File: WatershedSeededTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
@SuppressWarnings("unchecked") private void testWithMask(final RandomAccessibleInterval<FloatType> in, final ImgLabeling<Integer, IntType> seeds) { // create mask which is 1 everywhere long[] dims = new long[in.numDimensions()]; in.dimensions(dims); Img<BitType> mask = ArrayImgs.bits(dims); RandomAccess<BitType> raMask = mask.randomAccess(); for (BitType b : mask) { b.setZero(); } for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { raMask.setPosition(new int[] { x, y }); raMask.get().setOne(); } } /* * use 8-connected neighborhood */ // compute result without watersheds ImgLabeling<Integer, IntType> out = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, true, false, mask); assertResults(in, out, seeds, mask, false, true); // compute result with watersheds ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, true, true, mask); assertResults(in, out2, seeds, mask, true, true); /* * use 4-connected neighborhood */ // compute result without watersheds ImgLabeling<Integer, IntType> out3 = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, false, false, mask); assertResults(in, out3, seeds, mask, false, true); // compute result with watersheds ImgLabeling<Integer, IntType> out4 = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, false, true, mask); assertResults(in, out4, seeds, mask, true, true); }
Example 19
Source File: InvertTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
private <T extends IntegerType<T>> void integerCompare(final Img<T> in, final Img<T> out, final IntegerType<T> min, final IntegerType<T> max) { // Get min/max for the output type. final BigInteger minOut = InvertIIInteger.minValue(out.firstElement()) .getBigInteger(); final BigInteger maxOut = InvertIIInteger.maxValue(out.firstElement()) .getBigInteger(); BigInteger minMax = BigInteger.ZERO; // min + max if (min == null && max == null) { minMax = InvertIIInteger.minValue(in.firstElement()).getBigInteger().add( InvertIIInteger.maxValue(in.firstElement()).getBigInteger()); } else if (min == null || max == null) { fail("Internal coding error"); } else { minMax = min.getBigInteger().add(max.getBigInteger()); } final Cursor<T> inAccess = in.localizingCursor(); final RandomAccess<T> outAccess = out.randomAccess(); while (inAccess.hasNext()) { final T inVal = inAccess.next(); outAccess.setPosition(inAccess); final T outVal = outAccess.get(); final BigInteger bigIn = inVal.getBigInteger(); final BigInteger bigOut = outVal.getBigInteger(); final BigInteger calcOut = minMax.subtract(bigIn); if (calcOut.compareTo(minOut) <= 0) { assertEquals(minOut, bigOut); } else if (calcOut.compareTo(maxOut) >= 0) { assertEquals(maxOut, bigOut); } else { assertEquals(calcOut, bigOut); } } }
Example 20
Source File: ConvolveTest.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
/** tests fft based convolve */ @Test public void testCreateAndConvolvePoints() { final int xSize = 128; final int ySize = 128; final int zSize = 128; int[] size = new int[] { xSize, ySize, zSize }; Img<DoubleType> phantom = ops.create().img(size); RandomAccess<DoubleType> randomAccess = phantom.randomAccess(); randomAccess.setPosition(new long[] { xSize / 2, ySize / 2, zSize / 2 }); randomAccess.get().setReal(255.0); randomAccess.setPosition(new long[] { xSize / 4, ySize / 4, zSize / 4 }); randomAccess.get().setReal(255.0); Point location = new Point(phantom.numDimensions()); location.setPosition(new long[] { 3 * xSize / 4, 3 * ySize / 4, 3 * zSize / 4 }); HyperSphere<DoubleType> hyperSphere = new HyperSphere<>(phantom, location, 5); for (DoubleType value : hyperSphere) { value.setReal(16); } // create psf using the gaussian kernel op (alternatively PSF could be an // input to the script) RandomAccessibleInterval<DoubleType> psf = ops.create().kernelGauss( new double[] { 5, 5, 5 }, new DoubleType()); RandomAccessibleInterval<DoubleType> convolved = ops.create().img(size); // convolve psf with phantom convolved = ops.filter().convolve(convolved, phantom, psf); DoubleType sum = new DoubleType(); DoubleType max = new DoubleType(); DoubleType min = new DoubleType(); ops.stats().sum(sum, Views.iterable(convolved)); ops.stats().max(max, Views.iterable(convolved)); ops.stats().min(min, Views.iterable(convolved)); assertEquals(sum.getRealDouble(), 8750.000184601617, 0.0); assertEquals(max.getRealDouble(), 3.154534101486206, 0.0); assertEquals(min.getRealDouble(), -2.9776862220387557E-7, 0.0); }