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

The following examples show how to use net.imglib2.view.Views#interpolate() . 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: RasterViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void defaultRasterTest() {
	Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[]{10,  10}, new DoubleType());
	MersenneTwisterFast r = new MersenneTwisterFast(SEED);
	for (DoubleType d : img) {
		d.set(r.nextDouble());
	}
	RealRandomAccessible<DoubleType> realImg = Views.interpolate(img, new FloorInterpolatorFactory<DoubleType>());
	
	RandomAccessibleOnRealRandomAccessible<DoubleType> il2 = Views.raster(realImg);
	RandomAccessibleOnRealRandomAccessible<DoubleType> opr = ops.transform().rasterView(realImg);
	
	Cursor<DoubleType> il2C = Views.interval(il2, img).localizingCursor();
	RandomAccess<DoubleType> oprRA = Views.interval(opr, img).randomAccess();
	
	while (il2C.hasNext()) {
		il2C.next();
		oprRA.setPosition(il2C);
		assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10);
	}
}
 
Example 2
Source File: AffineWarpField.java    From render with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Logic stolen from
 * <a href='https://github.com/trakem2/TrakEM2/blob/master/TrakEM2_/src/main/java/org/janelia/intensity/LinearIntensityMap.java'>
 *   TrakEM2 LinearIntensityMap
 * </a>.
 *
 * @return an accessor for deriving warped pixel intensities.
 */
public RealRandomAccess<RealComposite<DoubleType>> getAccessor() {

    final ArrayImg<DoubleType, DoubleArray> warpField =
            ArrayImgs.doubles(values, columnCount, rowCount, VALUES_PER_AFFINE);

    final CompositeIntervalView<DoubleType, RealComposite<DoubleType>>
            collapsedSource = Views.collapseReal(warpField);

    final RandomAccessible<RealComposite<DoubleType>> extendedCollapsedSource = Views.extendBorder(collapsedSource);
    final RealRandomAccessible<RealComposite<DoubleType>> coefficients =
            Views.interpolate(extendedCollapsedSource, interpolatorFactory);

    final double xScale = getXScale();
    final double yScale = getYScale();
    final double[] scale = { xScale, yScale };
    final double[] shift = { 0.5 * xScale , 0.5 * yScale };

    final ScaleAndTranslation scaleAndTranslation = new ScaleAndTranslation(scale, shift);

    final RealRandomAccessible<RealComposite<DoubleType>> stretchedCoefficients =
            RealViews.transform(coefficients, scaleAndTranslation);

    return stretchedCoefficients.realRandomAccess();
}
 
Example 3
Source File: ShapeInterpolationMode.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private static RealRandomAccessible<UnsignedLongType> getTransformedMask(final Mask<UnsignedLongType> mask, final AffineTransform3D transform)
{
	final RealRandomAccessible<UnsignedLongType> interpolatedMask = Views.interpolate(
			Views.extendValue(mask.mask, new UnsignedLongType(Label.OUTSIDE)),
			new NearestNeighborInterpolatorFactory<>()
		);
	return RealViews.affine(interpolatedMask, transform);
}
 
Example 4
Source File: LinearIntensityMap.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
public LinearIntensityMap( final RandomAccessibleInterval< T > source, final InterpolatorFactory< RealComposite< T >, RandomAccessible< RealComposite< T > > > interpolatorFactory )
{
	this.interpolatorFactory = interpolatorFactory;
	final CompositeIntervalView< T, RealComposite< T > > collapsedSource = Views.collapseReal( source );
	dimensions = new FinalInterval( collapsedSource );
	final double[] shift = new double[ dimensions.numDimensions() ];
	for ( int d = 0; d < shift.length; ++d )
		shift[ d ] = 0.5;
	translation = new Translation( shift );

	final RandomAccessible< RealComposite< T > > extendedCollapsedSource = Views.extendBorder( collapsedSource );
	coefficients = Views.interpolate( extendedCollapsedSource, interpolatorFactory );
}
 
Example 5
Source File: ImageInterpolation.java    From Stitching with GNU General Public License v2.0 5 votes vote down vote up
public ImageInterpolation( final Img< T > image, final InterpolatorFactory< T, RandomAccessible< T > > interpolatorFactory, final boolean mirror )
{
	this.image = image;
	this.interpolatorFactory = interpolatorFactory;
	if ( mirror )
		this.interpolated = Views.interpolate( Views.extendMirrorSingle( image ), interpolatorFactory );
	else
		this.interpolated = Views.interpolate( Views.extendZero( image ), interpolatorFactory );
}
 
Example 6
Source File: ConvertedDataSource.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RealRandomAccessible<U> getInterpolatedDataSource(final int t, final int level, final Interpolation method)
{
	return Views.interpolate(
			Views.extend(getDataSource(t, level), new OutOfBoundsConstantValueFactory<>
					(dataTypeExtensionSupplier)),
			dataInterpolation.apply(method)
	                        );
}
 
Example 7
Source File: ConvertedDataSource.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RealRandomAccessible<V> getInterpolatedSource(final int t, final int level, final Interpolation method)
{
	return Views.interpolate(
			Views.extend(getSource(t, level), new OutOfBoundsConstantValueFactory<>(typeExtensionSupplier)),
			interpolation.apply(method)
	                        );
}
 
Example 8
Source File: RandomAccessibleIntervalDataSource.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RealRandomAccessible<D> getInterpolatedDataSource(final int t, final int level, final Interpolation method)
{
	LOG.debug("Requesting data source at t={}, level={} with interpolation {}: ", t, level, method);
	return Views.interpolate(
			Views.extendValue(getDataSource(t, level), dataTypeSupplier.get()),
			dataInterpolation.apply(method)
	                        );
}
 
Example 9
Source File: RandomAccessibleIntervalDataSource.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RealRandomAccessible<T> getInterpolatedSource(final int t, final int level, final Interpolation method)
{
	LOG.debug("Requesting source at t={}, level={} with interpolation {}: ", t, level, method);
	return Views.interpolate(
			Views.extendValue(getSource(t, level), typeSupplier.get()),
			interpolation.apply(method)
	                        );
}
 
Example 10
Source File: ShapeInterpolationMode.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private static <R extends RealType<R>, T extends NativeType<T> & RealType<T>> RealRandomAccessible<T> getInterpolatedDistanceTransformMask(
		final RandomAccessibleInterval<R> dt1,
		final RandomAccessibleInterval<R> dt2,
		final double distance,
		final T targetValue,
		final AffineTransform3D transformToSource)
{
	final RandomAccessibleInterval<R> distanceTransformStack = Views.stack(dt1, dt2);

	final R extendValue = Util.getTypeFromInterval(distanceTransformStack).createVariable();
	extendValue.setReal(extendValue.getMaxValue());
	final RealRandomAccessible<R> interpolatedDistanceTransform = Views.interpolate(
			Views.extendValue(distanceTransformStack, extendValue),
			new NLinearInterpolatorFactory<>()
		);

	final RealRandomAccessible<R> scaledInterpolatedDistanceTransform = RealViews.affineReal(
			interpolatedDistanceTransform,
			new Scale3D(1, 1, -distance)
		);

	final T emptyValue = targetValue.createVariable();
	final RealRandomAccessible<T> interpolatedShape = Converters.convert(
			scaledInterpolatedDistanceTransform,
			(in, out) -> out.set(in.getRealDouble() <= 0 ? targetValue : emptyValue),
			emptyValue.createVariable()
		);

	return RealViews.affineReal(interpolatedShape, transformToSource);
}
 
Example 11
Source File: N5ChannelDataSource.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
@Override
public RealRandomAccessible<RealComposite<D>> getInterpolatedDataSource(int t, int level, Interpolation method) {
	return Views.interpolate(getDataSource(t, level), interpolation.apply(method));
}
 
Example 12
Source File: N5ChannelDataSource.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
@Override
public RealRandomAccessible<VolatileWithSet<RealComposite<T>>> getInterpolatedSource(int t, int level, Interpolation method) {
	final RealRandomAccessible<RealComposite<T>> interpolated = Views.interpolate(viewerData[level], viewerInterpolation.apply(method));
	return Converters.convert(interpolated, viewerConverter, new VolatileWithSet<>(null, true));
}
 
Example 13
Source File: MaskedSource.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private static <T> RealRandomAccessible<T> interpolateNearestNeighbor(final RandomAccessible<T> ra)
{
	return Views.interpolate(ra, new NearestNeighborInterpolatorFactory<>());
}
 
Example 14
Source File: Align.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
public double getCurrentCorrelation(final RandomAccessibleInterval< T > image)
{
	final RealRandomAccessible< T > interpolated = Views.interpolate( Views.extendBorder( image ), new NLinearInterpolatorFactory< T >() );
	final RandomAccessible< T > warped = RealViews.affine( interpolated, currentTransform );
	return PhaseCorrelation2Util.getCorrelation( Views.interval( warped, template ), template );
}
 
Example 15
Source File: Align.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compute the pixel-wise difference between an affine-transformed source
 * image and a target image.
 *
 * @param source
 *            The source image.
 * @param transform
 *            A coordinate transformation to apply to the source image.
 * @param target
 *            The target image.
 * @param difference
 *            Output image. The pixel-wise difference between the
 *            transformed source image and the target image is stored here.
 * @param service
 *            thread pool for difference calculation
 * @param nTasks
 *            number of image parts that are processed in parallel
 * @param <T> pixel type source
 * @param <S> pixel type target
 */
public static < T extends RealType< T >,  S extends RealType< S > > void computeDifference(
		final RandomAccessible< T > source,
		final AffineTransform transform,
		final RandomAccessible< T > target,
		final RandomAccessibleInterval< S > difference,
		final ExecutorService service,
		final int nTasks)
{
	final RealRandomAccessible< T > interpolated = Views.interpolate( source, new NLinearInterpolatorFactory< T >() );
	final RandomAccessible< T > warped = RealViews.affine( interpolated, transform );

	final long stepSize = Views.iterable( difference ).size() / nTasks;

	final List<Callable< Void >> tasks = new ArrayList<>();
	final AtomicInteger ai = new AtomicInteger( 0 );
	for (int iO = 0; iO<nTasks; iO++)
	{
		tasks.add( new Callable< Void >()
		{
			@Override
			public Void call() throws Exception
			{
				final int i = ai.getAndIncrement();
				final Cursor< T > cw = Views.flatIterable( Views.interval( warped, difference ) ).cursor();
				final Cursor< T > ct = Views.flatIterable( Views.interval( target, difference ) ).cursor();
				final Cursor< S > cd = Views.flatIterable( difference ).cursor();

				cw.jumpFwd( stepSize * i );
				ct.jumpFwd( stepSize * i );
				cd.jumpFwd( stepSize * i );

				final long end = i == nTasks - 1 ? Views.iterable( difference ).size() - stepSize * i : stepSize;
				int count = 0;
				while (count++ < end)
				{
					cd.next().setReal( ( cw.next().getRealDouble() - ct.next().getRealDouble() ));
				}
				return null;
			}
		} );
	}

	try
	{
		List< Future< Void > > futures = service.invokeAll( tasks );
		for (Future< Void > f: futures)
			f.get();
	}
	catch ( InterruptedException | ExecutionException e )
	{
		e.printStackTrace();
	}
}
 
Example 16
Source File: PhaseCorrelationPeak2.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
public <T extends RealType<T>, S extends RealType<S>> void calculateCrossCorr(RandomAccessibleInterval<T> img1, RandomAccessibleInterval<S> img2, 
		long minOverlapPx, boolean interpolateSubpixel)
{
	Pair<Interval, Interval> intervals = PhaseCorrelation2Util.getOverlapIntervals(img1, img2, shift);
	
	// no overlap found
	if (intervals == null) {
		crossCorr = Double.NEGATIVE_INFINITY;
		nPixel = 0;
		return;
	}
	
	nPixel = 1;
	for (int i = 0; i< intervals.getA().numDimensions(); i++){
		nPixel *= intervals.getA().dimension(i);
	}
	
	if (nPixel < minOverlapPx){
		crossCorr = Double.NEGATIVE_INFINITY;
		nPixel = 0;
		return;
	}

	// for subpixel move the underlying Img2 by the subpixel offset
	if ( subpixelShift != null && interpolateSubpixel )
	{
		RealRandomAccessible< S > rra = Views.interpolate( Views.extendMirrorSingle( img2 ), new NLinearInterpolatorFactory< S >() );

		InvertibleRealTransform transform = null;

		// e.g. subpixel = (-0.4, 0.1, -0.145)
		final double tx = subpixelShift.getDoublePosition( 0 ) - shift.getDoublePosition( 0 );
		final double ty = subpixelShift.getDoublePosition( 1 ) - shift.getDoublePosition( 1 );

		if ( rra.numDimensions() == 2 )
			transform = new Translation2D( -tx, -ty ); // -relative subpixel shift only
		else if ( rra.numDimensions() == 3 )
			transform = new Translation3D( -tx, -ty, shift.getDoublePosition( 2 ) - subpixelShift.getDoublePosition( 2 ) ); // -relative subpixel shift only

		img2 = Views.interval( Views.raster( RealViews.transform( rra, transform ) ), img2 );
	}

	// calculate cross correlation.
	// note that the overlap we calculate assumes zero-min input
	crossCorr = PhaseCorrelation2Util.getCorrelation(
			Views.zeroMin( Views.interval(Views.zeroMin(img1), intervals.getA())),
			Views.zeroMin( Views.interval(Views.zeroMin(img2), intervals.getB()))
		);
	
}
 
Example 17
Source File: DefaultInterpolateView.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public RealRandomAccessible<T> calculate(I input) {
	return Views.interpolate(input, factory);
}