Java Code Examples for net.imglib2.RandomAccess#get()

The following examples show how to use net.imglib2.RandomAccess#get() . 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: InvertTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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 2
Source File: TestImageAccessor.java    From Colocalisation_Analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts an arbitrary image to a black/white version of it.
 * All image data lower or equal the splitValue will get black,
 * the rest will turn white.
 */
public static <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> makeBinaryImage(
		RandomAccessibleInterval<T> image, T splitValue) {
	Cursor<T> imgCursor = Views.iterable(image).localizingCursor();
	// make a new image of the same type, but binary
	long[] dim = new long[ image.numDimensions() ];
	image.dimensions(dim);
	ArrayImgFactory<T> imgFactory = new ArrayImgFactory<T>();
	RandomAccessibleInterval<T> binImg = imgFactory.create( dim,
			image.randomAccess().get().createVariable() ); // "Binary image of " + image.getName());
	RandomAccess<T> invCursor = binImg.randomAccess();

	while (imgCursor.hasNext()) {
		imgCursor.fwd();
		invCursor.setPosition(imgCursor);
		T currentValue = invCursor.get();
		if (currentValue.compareTo(splitValue) > 0)
			currentValue.setReal(  currentValue.getMaxValue() );
		else
			currentValue.setZero();
	}

	return binImg;
}
 
Example 3
Source File: ShapeInterpolationMode.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private UnsignedLongType getMaskValue(final double x, final double y)
{
	final RealPoint sourcePos = getSourceCoordinates(x, y);
	final RandomAccess<UnsignedLongType> maskAccess = Views.extendValue(mask.mask, new UnsignedLongType(Label.OUTSIDE)).randomAccess();
	for (int d = 0; d < sourcePos.numDimensions(); ++d)
		maskAccess.setPosition(Math.round(sourcePos.getDoublePosition(d)), d);
	return maskAccess.get();
}
 
Example 4
Source File: ShapeInterpolationMode.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private D getDataValue(final double x, final double y)
{
	final RealPoint sourcePos = getSourceCoordinates(x, y);
	final int time = activeViewer.getState().getTimepoint();
	final int level = MASK_SCALE_LEVEL;
	final RandomAccessibleInterval<D> data = source.getDataSource(time, level);
	final RandomAccess<D> dataAccess = data.randomAccess();
	for (int d = 0; d < sourcePos.numDimensions(); ++d)
		dataAccess.setPosition(Math.round(sourcePos.getDoublePosition(d)), d);
	return dataAccess.get();
}
 
Example 5
Source File: AccessedBlocksRandomAccessible.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args)
{
	final long[] dimensions = {10, 7};
	final int[]  blockSize  = {5, 3};

	final ArrayImg<LongType, LongArray> dummy = ArrayImgs.longs(dimensions);

	final AccessedBlocksRandomAccessible<LongType> tracker = new AccessedBlocksRandomAccessible<>(
			dummy,
			new CellGrid(dimensions, blockSize)
	);

	System.out.println(Arrays.toString(tracker.listBlocks()));
	final RandomAccess<LongType> ra = tracker.randomAccess();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.move(4, 0);
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.fwd(0);
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.move(6, 1);
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));

}
 
Example 6
Source File: Coloc_2.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
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 7
Source File: SimpleInterruptibleProjectorPreMultiply.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Render the 2D target image by copying values from the source. Source can have more dimensions than the target.
 * Target coordinate <em>(x,y)</em> is copied from source coordinate <em>(x,y,0,...,0)</em>.
 *
 * @return true if rendering was completed (all target pixels written). false if rendering was interrupted.
 */
@Override
public boolean map()
{
	interrupted.set(false);

	final StopWatch stopWatch = new StopWatch();
	stopWatch.start();

	min[0] = target.min(0);
	min[1] = target.min(1);
	max[0] = target.max(0);
	max[1] = target.max(1);

	final long cr = -target.dimension(0);

	final int width  = (int) target.dimension(0);
	final int height = (int) target.dimension(1);

	final boolean         createExecutor = executorService == null;
	final ExecutorService ex             = createExecutor
	                                       ? Executors.newFixedThreadPool(numThreads)
	                                       : executorService;
	final int             numTasks;
	if (numThreads > 1)
	{
		numTasks = Math.min(numThreads * 10, height);
	}
	else
		numTasks = 1;
	final double                    taskHeight = (double) height / numTasks;
	final ArrayList<Callable<Void>> tasks      = new ArrayList<>(numTasks);
	for (int taskNum = 0; taskNum < numTasks; ++taskNum)
	{
		final long myMinY   = min[1] + (int) (taskNum * taskHeight);
		final long myHeight = (taskNum == numTasks - 1
		                       ? height
		                       : (int) ((taskNum + 1) * taskHeight)) - myMinY - min[1];

		final Callable<Void> r = () -> {
			if (interrupted.get())
				return null;

			System.out.println("WTF!");
			final RandomAccess<A>        sourceRandomAccess = source.randomAccess(
					SimpleInterruptibleProjectorPreMultiply.this);
			final RandomAccess<ARGBType> targetRandomAccess = target.randomAccess(target);

			sourceRandomAccess.setPosition(min);
			sourceRandomAccess.setPosition(myMinY, 1);
			targetRandomAccess.setPosition(min[0], 0);
			targetRandomAccess.setPosition(myMinY, 1);
			for (int y = 0; y < myHeight; ++y)
			{
				if (interrupted.get())
					return null;
				for (int x = 0; x < width; ++x)
				{
					final ARGBType argb = targetRandomAccess.get();
					converter.convert(sourceRandomAccess.get(), argb);
					final int nonpre = argb.get();
					argb.set(PixelUtils.NonPretoPre(nonpre));
					sourceRandomAccess.fwd(0);
					targetRandomAccess.fwd(0);
				}
				sourceRandomAccess.move(cr, 0);
				targetRandomAccess.move(cr, 0);
				sourceRandomAccess.fwd(1);
				targetRandomAccess.fwd(1);
			}
			return null;
		};
		tasks.add(r);
	}
	try
	{
		ex.invokeAll(tasks);
	} catch (final InterruptedException e)
	{
		Thread.currentThread().interrupt();
	}
	if (createExecutor)
		ex.shutdown();

	lastFrameRenderNanoTime = stopWatch.nanoTime();

	return !interrupted.get();
}
 
Example 8
Source File: RestrictPainting.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private static <T extends RealType<T>> void restrictTo(
		final MaskedSource<T, ?> source,
		final int time,
		final int level,
		final Localizable seed,
		final Runnable requestRepaint) throws MaskInUse
{
	final RandomAccessibleInterval<UnsignedLongType>  canvas        = source.getReadOnlyDataCanvas(time, level);
	final RandomAccessibleInterval<T>                 background    = source.getReadOnlyDataBackground(time,
			level);
	final MaskInfo<UnsignedLongType>                  maskInfo      = new MaskInfo<>(
			time,
			level,
			new UnsignedLongType(Label.TRANSPARENT)
	);
	final Mask<UnsignedLongType> mask = source.generateMask(maskInfo, FOREGROUND_CHECK);
	final AccessBoxRandomAccessible<UnsignedLongType> accessTracker = new AccessBoxRandomAccessible<>(Views
			.extendValue(mask.mask, new UnsignedLongType(1)));

	final RandomAccess<UnsignedLongType> canvasAccess = canvas.randomAccess();
	canvasAccess.setPosition(seed);
	final UnsignedLongType paintedLabel     = canvasAccess.get();
	final RandomAccess<T>  backgroundAccess = background.randomAccess();
	backgroundAccess.setPosition(seed);
	final T backgroundSeed = backgroundAccess.get();

	final RandomAccessible<Pair<T, UnsignedLongType>> paired = Views.pair(
			Views.extendBorder(background),
			Views.extendValue(canvas, new UnsignedLongType(Label.INVALID))
	                                                                     );

	restrictTo(
			paired,
			accessTracker,
			seed,
			new DiamondShape(1),
			bg -> bg.valueEquals(backgroundSeed),
			cv -> cv.valueEquals(paintedLabel)
	          );

	requestRepaint.run();

	source.applyMask(mask, accessTracker.createAccessInterval(), FOREGROUND_CHECK);

}
 
Example 9
Source File: RestrictPainting.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private static void restrictToLabelMultisetType(
		final MaskedSource<LabelMultisetType, ?> source,
		final int time,
		final int level,
		final Localizable seed,
		final Runnable requestRepaint) throws MaskInUse
{
	final RandomAccessibleInterval<UnsignedLongType>  canvas        = source.getReadOnlyDataCanvas(time, level);
	final RandomAccessibleInterval<LabelMultisetType> background    = source.getReadOnlyDataBackground(time,
			level);
	final MaskInfo<UnsignedLongType>                  maskInfo      = new MaskInfo<>(
			time,
			level,
			new UnsignedLongType(Label.TRANSPARENT)
	);
	final Mask<UnsignedLongType> mask = source.generateMask(maskInfo, FOREGROUND_CHECK);
	final AccessBoxRandomAccessible<UnsignedLongType> accessTracker = new AccessBoxRandomAccessible<>(Views
			.extendValue(mask.mask, new UnsignedLongType(1)));

	final RandomAccess<UnsignedLongType> canvasAccess = canvas.randomAccess();
	canvasAccess.setPosition(seed);
	final UnsignedLongType                paintedLabel     = canvasAccess.get();
	final RandomAccess<LabelMultisetType> backgroundAccess = background.randomAccess();
	backgroundAccess.setPosition(seed);
	final LabelMultisetType backgroundSeed      = backgroundAccess.get();
	final long              backgroundSeedLabel = backgroundSeed.entrySet().stream().max((e1, e2) -> Long.compare(
			e1.getCount(),
			e2.getCount()
	                                                                                                             )
	                                                                                    ).map(
			e -> e.getElement().id()).orElse(Label.INVALID);

	final RandomAccessible<Pair<LabelMultisetType, UnsignedLongType>> paired = Views.pair(
			Views.extendValue(
					background,
					new LabelMultisetType()
			                 ),
			Views.extendValue(canvas, new UnsignedLongType(Label.INVALID))
	                                                                                     );

	restrictTo(
			paired,
			accessTracker,
			seed,
			new DiamondShape(1),
			bg -> bg.contains(backgroundSeedLabel),
			cv -> cv.valueEquals(paintedLabel)
	          );

	requestRepaint.run();

	source.applyMask(mask, accessTracker.createAccessInterval(), FOREGROUND_CHECK);

}
 
Example 10
Source File: InvertTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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);

		}
	}
}