Java Code Examples for net.imglib2.view.Views#extendValue()

The following examples show how to use net.imglib2.view.Views#extendValue() . 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: LocalThresholdIntegral.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Add 0s before axis minimum.
 * 
 * @param input Input RAI
 * @return An extended and cropped version of input
 */
private <T extends RealType<T>> RandomAccessibleInterval<T> addLeadingZeros(
	RandomAccessibleInterval<T> input)
{
	final long[] min = Intervals.minAsLongArray(input);
	final long[] max = Intervals.maxAsLongArray(input);

	for (int i = 0; i < max.length; i++) {
		min[i]--;
	}

	final T realZero = Util.getTypeFromInterval(input).copy();
	realZero.setZero();

	final ExtendedRandomAccessibleInterval<T, RandomAccessibleInterval<T>> extendedImg = Views.extendValue(input,
		realZero);
	final IntervalView<T> offsetInterval = Views.interval(extendedImg,
		min, max);
	
	return Views.zeroMin(offsetInterval);
}
 
Example 2
Source File: Outline.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ExtendedRandomAccessibleInterval<B, RandomAccessibleInterval<B>>
	extendInterval(RandomAccessibleInterval<B> interval)
{
	final B type = Util.getTypeFromInterval(interval).createVariable();
	type.set(in2());
	return Views.extendValue(interval, type);
}
 
Example 3
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 4
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 5
Source File: IntersectingSourceState.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private static <D extends IntegerType<D>, T extends Type<T>, B extends BooleanType<B>>
DataSource<UnsignedByteType, VolatileUnsignedByteType> makeIntersect(
		final SourceState<B, Volatile<B>> thresholded,
		final ConnectomicsLabelState<D, T> labels,
		final SharedQueue queue,
		final int priority,
		final String name) {
	LOG.debug(
			"Number of mipmap labels: thresholded={} labels={}",
			thresholded.getDataSource().getNumMipmapLevels(),
			labels.getDataSource().getNumMipmapLevels()
	);
	if (thresholded.getDataSource().getNumMipmapLevels() != labels.getDataSource().getNumMipmapLevels()) {
		throw new RuntimeException("Incompatible sources (num mip map levels )");
	}

	final AffineTransform3D[] transforms = new AffineTransform3D[thresholded.getDataSource().getNumMipmapLevels()];
	final RandomAccessibleInterval<UnsignedByteType>[] data = new RandomAccessibleInterval[transforms.length];
	final RandomAccessibleInterval<VolatileUnsignedByteType>[] vdata = new RandomAccessibleInterval[transforms.length];
	final Invalidate<Long>[] invalidate = new Invalidate[transforms.length];
	final Invalidate<Long>[] vinvalidate = new Invalidate[transforms.length];

	final FragmentsInSelectedSegments fragmentsInSelectedSegments = new FragmentsInSelectedSegments(labels.getSelectedSegments());

	for (int level = 0; level < thresholded.getDataSource().getNumMipmapLevels(); ++level) {
		final DataSource<D, T> labelsSource = labels.getDataSource() instanceof MaskedSource<?, ?>
				? ((MaskedSource<D, T>) labels.getDataSource()).underlyingSource()
				: labels.getDataSource();
		final AffineTransform3D tf1 = new AffineTransform3D();
		final AffineTransform3D tf2 = new AffineTransform3D();
		thresholded.getDataSource().getSourceTransform(0, level, tf1);
		labelsSource.getSourceTransform(0, level, tf2);
		if (!Arrays.equals(tf1.getRowPackedCopy(), tf2.getRowPackedCopy()))
			throw new RuntimeException("Incompatible sources ( transforms )");

		final RandomAccessibleInterval<B> thresh = thresholded.getDataSource().getDataSource(0, level);
		final RandomAccessibleInterval<D> label = labelsSource.getDataSource(0, level);

		final CellGrid grid = label instanceof AbstractCellImg<?, ?, ?, ?>
				? ((AbstractCellImg<?, ?, ?, ?>) label).getCellGrid()
				: new CellGrid(
				Intervals.dimensionsAsLongArray(label),
				Arrays.stream(Intervals.dimensionsAsLongArray(label)).mapToInt(l -> (int) l)
						.toArray());

		final B extension = Util.getTypeFromInterval(thresh);
		extension.set(false);
		final LabelIntersectionCellLoader<D, B> loader = new LabelIntersectionCellLoader<>(
				label,
				Views.extendValue(thresh, extension),
				checkForType(labelsSource.getDataType(), fragmentsInSelectedSegments),
				BooleanType::get,
				extension::copy);

		LOG.debug("Making intersect for level={} with grid={}", level, grid);


		final LoadedCellCacheLoader<UnsignedByteType, VolatileByteArray> cacheLoader = LoadedCellCacheLoader.get(grid, loader, new UnsignedByteType(), AccessFlags.setOf(AccessFlags.VOLATILE));
		final Cache<Long, Cell<VolatileByteArray>> cache = new SoftRefLoaderCache<Long, Cell<VolatileByteArray>>().withLoader(cacheLoader);
		final CachedCellImg<UnsignedByteType, VolatileByteArray> img = new CachedCellImg<>(grid, new UnsignedByteType(), cache, new VolatileByteArray(1, true));
		// TODO cannot use VolatileViews because we need access to cache
		final TmpVolatileHelpers.RaiWithInvalidate<VolatileUnsignedByteType> vimg = TmpVolatileHelpers.createVolatileCachedCellImgWithInvalidate(
				img,
				queue,
				new CacheHints(LoadingStrategy.VOLATILE, priority, true));
		data[level] = img;
		vdata[level] = vimg.getRai();
		invalidate[level] = img.getCache();
		vinvalidate[level] = vimg.getInvalidate();
		transforms[level] = tf1;
	}

	return new RandomAccessibleIntervalDataSource<>(
			new ValueTriple<>(data, vdata, transforms),
			new InvalidateDelegates<>(Arrays.asList(new InvalidateDelegates<>(invalidate), new InvalidateDelegates<>(vinvalidate))),
			Interpolations.nearestNeighbor(),
			Interpolations.nearestNeighbor(),
			name);
}
 
Example 6
Source File: IntersectingSourceState.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
@Deprecated
private static <D extends IntegerType<D>, T extends Type<T>, B extends BooleanType<B>>
DataSource<UnsignedByteType, VolatileUnsignedByteType> makeIntersect(
		final SourceState<B, Volatile<B>> thresholded,
		final LabelSourceState<D, T> labels,
		final SharedQueue queue,
		final int priority,
		final String name) {
	LOG.debug(
			"Number of mipmap labels: thresholded={} labels={}",
			thresholded.getDataSource().getNumMipmapLevels(),
			labels.getDataSource().getNumMipmapLevels()
	         );
	if (thresholded.getDataSource().getNumMipmapLevels() != labels.getDataSource().getNumMipmapLevels())
	{
		throw new RuntimeException("Incompatible sources (num mip map levels )");
	}

	final AffineTransform3D[]                                  transforms = new AffineTransform3D[thresholded.getDataSource().getNumMipmapLevels()];
	final RandomAccessibleInterval<UnsignedByteType>[]         data       = new RandomAccessibleInterval[transforms.length];
	final RandomAccessibleInterval<VolatileUnsignedByteType>[] vdata      = new RandomAccessibleInterval[transforms.length];
	final Invalidate<Long>[] invalidate                                   = new Invalidate[transforms.length];
	final Invalidate<Long>[] vinvalidate                                  = new Invalidate[transforms.length];

	final SelectedIds                    selectedIds                 = labels.selectedIds();
	final FragmentSegmentAssignmentState assignment                  = labels.assignment();
	final SelectedSegments               selectedSegments            = new SelectedSegments(
			selectedIds,
			assignment);
	final FragmentsInSelectedSegments    fragmentsInSelectedSegments = new FragmentsInSelectedSegments(
			selectedSegments);

	for (int level = 0; level < thresholded.getDataSource().getNumMipmapLevels(); ++level)
	{
		final DataSource<D, T> labelsSource = labels.getDataSource() instanceof MaskedSource<?, ?>
		                                      ? ((MaskedSource<D, T>) labels.getDataSource()).underlyingSource()
		                                      : labels.getDataSource();
		final AffineTransform3D tf1 = new AffineTransform3D();
		final AffineTransform3D tf2 = new AffineTransform3D();
		thresholded.getDataSource().getSourceTransform(0, level, tf1);
		labelsSource.getSourceTransform(0, level, tf2);
		if (!Arrays.equals(tf1.getRowPackedCopy(), tf2.getRowPackedCopy()))
		{
			throw new RuntimeException("Incompatible sources ( transforms )");
		}

		final RandomAccessibleInterval<B> thresh = thresholded.getDataSource().getDataSource(0, level);
		final RandomAccessibleInterval<D> label  = labelsSource.getDataSource(0, level);

		final CellGrid grid = label instanceof AbstractCellImg<?, ?, ?, ?>
		                      ? ((AbstractCellImg<?, ?, ?, ?>) label).getCellGrid()
		                      : new CellGrid(
				                      Intervals.dimensionsAsLongArray(label),
				                      Arrays.stream(Intervals.dimensionsAsLongArray(label)).mapToInt(l -> (int) l)
						                      .toArray());

		final B extension = Util.getTypeFromInterval(thresh);
		extension.set(false);
		final LabelIntersectionCellLoader<D, B> loader = new LabelIntersectionCellLoader<>(
				label,
				Views.extendValue(thresh, extension),
				checkForType(labelsSource.getDataType(), fragmentsInSelectedSegments),
				BooleanType::get,
				extension::copy);

		LOG.debug("Making intersect for level={} with grid={}", level, grid);


		final LoadedCellCacheLoader<UnsignedByteType, VolatileByteArray> cacheLoader = LoadedCellCacheLoader.get(grid, loader, new UnsignedByteType(), AccessFlags.setOf(AccessFlags.VOLATILE));
		final Cache<Long, Cell<VolatileByteArray>> cache = new SoftRefLoaderCache<Long, Cell<VolatileByteArray>>().withLoader(cacheLoader);
		final CachedCellImg<UnsignedByteType, VolatileByteArray> img = new CachedCellImg<>(grid, new UnsignedByteType(), cache, new VolatileByteArray(1, true));
		// TODO cannot use VolatileViews because we need access to cache
		final TmpVolatileHelpers.RaiWithInvalidate<VolatileUnsignedByteType> vimg = TmpVolatileHelpers.createVolatileCachedCellImgWithInvalidate(
				img,
				queue,
				new CacheHints(LoadingStrategy.VOLATILE, priority, true));
		data[level] = img;
		vdata[level] = vimg.getRai();
		invalidate[level] = img.getCache();
		vinvalidate[level] = vimg.getInvalidate();
		transforms[level] = tf1;

	}

	return new RandomAccessibleIntervalDataSource<>(
			new ValueTriple<>(data, vdata, transforms),
			new InvalidateDelegates<>(Arrays.asList(new InvalidateDelegates<>(invalidate), new InvalidateDelegates<>(vinvalidate))),
			Interpolations.nearestNeighbor(),
			Interpolations.nearestNeighbor(),
			name
	);
}
 
Example 7
Source File: DefaultExtendValueView.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public ExtendedRandomAccessibleInterval<T, F> calculate(F input) {
	return Views.extendValue(input, value);
}