Java Code Examples for net.imglib2.type.numeric.real.FloatType#set()

The following examples show how to use net.imglib2.type.numeric.real.FloatType#set() . 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: MinimalTest.java    From BigStitcher with GNU General Public License v2.0 6 votes vote down vote up
@Override
public RandomAccessibleInterval< FloatType > getFloatImage(
		int timepointId, boolean normalize, ImgLoaderHint... hints )
{
	final Random rnd = new Random( setupId );

	final Img< FloatType > img = ArrayImgs.floats( 512, 512, 86 );

	final float scale;
	
	if ( normalize )
		scale = 1;
	else
		scale = 20000;
	
	for ( final FloatType t : img )
		t.set( rnd.nextFloat() * scale);

	return img;
}
 
Example 2
Source File: PaddingTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void theTest() {

	Img<FloatType> ramp = ArrayImgs.floats(256, 256);
	Img<FloatType> average = ArrayImgs.floats(64, 64);

	int i = 0;
	for (FloatType iv : ramp)
		iv.set(i / 256 + (i++) % 256);
	for (FloatType kv : average)
		kv.set(1f / (64 * 64));

	RandomAccessibleInterval<FloatType> one=ops.filter().convolve(ramp, average);
	RandomAccessibleInterval<FloatType> two=ops.filter().convolve(ramp, average, new OutOfBoundsPeriodicFactory<>());

	assertEquals(ops.stats().mean(Views.iterable(one)).getRealDouble(), 224.5312520918087, 0.0);
	assertEquals(ops.stats().mean(Views.iterable(two)).getRealDouble(), 255.0000066360226, 0.0);
}
 
Example 3
Source File: AdjustInput.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Norms an image so that the sum over all pixels is 1.
 * 
 * @param img - the {@link IterableInterval} to normalize
 */
final public static void normImg( final IterableInterval< FloatType > img )
{
	final double sum = sumImg( img );

	for ( final FloatType t : img )
		t.set( (float) ((double)t.get() / sum) );
}
 
Example 4
Source File: MVDeconFFT.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
public static ArrayImg< FloatType, ? > computeExponentialKernel( final ArrayImg< FloatType, ? > kernel, final int numViews )
{
	final ArrayImg< FloatType, ? > exponentialKernel = kernel.copy();

	for ( final FloatType f : exponentialKernel )
		f.set( pow( f.get(), numViews ) );

	return exponentialKernel;
}
 
Example 5
Source File: TransformInput.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
private static final void loop(
		final Cursor< FloatType > cursor,
		final RealRandomAccess< FloatType > ir,
		final AffineTransform3D transform,
		final float[] s, final float[] t,
		final int offsetX, final int offsetY, final int offsetZ,
		final int imgSizeX, final int imgSizeY, final int imgSizeZ )
{
	// move img cursor forward any get the value (saves one access)
	final FloatType v = cursor.next();
	cursor.localize( s );

	s[ 0 ] += offsetX;
	s[ 1 ] += offsetY;
	s[ 2 ] += offsetZ;

	transform.applyInverse( t, s );

	if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizeX, imgSizeY, imgSizeZ ) )
	{
		ir.setPosition( t );

		// do not accept 0 values in the data where image data is present, 0 means no image data is available
		// (used in MVDeconvolution.computeQuotient)
		v.set( Math.max( MVDeconvolution.minValue, ir.get().get() ) );
	}
}
 
Example 6
Source File: TransformInputAndWeights.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
private static final void loop(
		final Cursor< FloatType > cursor,
		final Cursor< FloatType > cursorW,
		final RealRandomAccess< FloatType > ir,
		final RealRandomAccess< FloatType > wr,
		final AffineTransform3D transform,
		final float[] s, final float[] t,
		final int offsetX, final int offsetY, final int offsetZ,
		final int imgSizeX, final int imgSizeY, final int imgSizeZ )
{
	// move img cursor forward any get the value (saves one access)
	final FloatType v = cursor.next();
	cursor.localize( s );

	// move weight cursor forward and get the value 
	final FloatType w = cursorW.next();

	s[ 0 ] += offsetX;
	s[ 1 ] += offsetY;
	s[ 2 ] += offsetZ;
	
	transform.applyInverse( t, s );
	
	if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizeX, imgSizeY, imgSizeZ ) )
	{
		ir.setPosition( t );

		// do not accept 0 values in the data where image data is present, 0 means no image data is available
		// (used in MVDeconvolution.computeQuotient)
		v.set( Math.max( MVDeconvolution.minValue, ir.get().get() ) );
	}

	// compute weights in any case (the border can be negative!)
	wr.setPosition( t );
	w.set( wr.get() );
}
 
Example 7
Source File: Block.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
public static void main( String[] args )
{
	// define the blocksize so that it is one single block
	final RandomAccessibleInterval< FloatType > block = ArrayImgs.floats( 384, 384 );
	final long[] blockSize = new long[ block.numDimensions() ];
	block.dimensions( blockSize );

	final RandomAccessibleInterval< FloatType > image = ArrayImgs.floats( 1024, 1024 );
	final long[] imgSize = new long[ image.numDimensions() ];
	image.dimensions( imgSize );

	// whatever the kernel size is (extra size/2 in general)
	final long[] kernelSize = new long[]{ 16, 32 };

	final BlockGeneratorFixedSizePrecise blockGenerator = new BlockGeneratorFixedSizePrecise( blockSize );
	final Block[] blocks = blockGenerator.divideIntoBlocks( imgSize, kernelSize );

	int i = 0;

	for ( final Block b : blocks )
	{
		// copy data from the image to the block (including extra space for outofbounds/real image data depending on kernel size)
		b.copyBlock( Views.extendMirrorDouble( image ), block );

		// do something with the block (e.g. also multithreaded, cluster, ...)
		for ( final FloatType f : Views.iterable( block ) )
			f.set( i );

		++i;

		// write the block back (use a temporary image if multithreaded or in general not all are copied first)
		b.pasteBlock( image, block );
	}

	ImageJFunctions.show( image );
}
 
Example 8
Source File: TransformWeights.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String call() throws Exception 
{
	// make the blending and get the transformations
	final RealRandomAccess< FloatType > wr = blending.realRandomAccess();

	final Cursor< FloatType > cursorO = Views.iterable( overlapImg ).localizingCursor();
	final Cursor< FloatType > cursorB = Views.iterable( blendingImg ).cursor();

	final float[] s = new float[ 3 ];
	final float[] t = new float[ 3 ];

	cursorO.jumpFwd( portion.getStartPosition() );
	cursorB.jumpFwd( portion.getStartPosition() );

	for ( int j = 0; j < portion.getLoopSize(); ++j )
	{
		// move img cursor forward any get the value (saves one access)
		final FloatType o = cursorO.next();
		cursorO.localize( s );
		
		// move weight cursor forward and get the value 
		final FloatType b = cursorB.next();

		s[ 0 ] += offsetX;
		s[ 1 ] += offsetY;
		s[ 2 ] += offsetZ;

		transform.applyInverse( t, s );

		// compute weights in any part of the image (the border can be negative!)
		wr.setPosition( t );

		o.set( o.get() + 1 );
		b.set( wr.get() );
	}

	return portion + " finished successfully (visualize weights).";
}
 
Example 9
Source File: ProcessSequentialPortionWeight.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String call() throws Exception 
{
	final int numViews = imgs.size();
	
	// make the interpolators, weights and get the transformations
	final ArrayList< RealRandomAccess< T > > interpolators = new ArrayList< RealRandomAccess< T > >( numViews );
	final ArrayList< RealRandomAccess< FloatType > > weightAccess = new ArrayList< RealRandomAccess< FloatType > >();
	final int[][] imgSizes = new int[ numViews ][ 3 ];
	
	for ( int i = 0; i < numViews; ++i )
	{
		final RandomAccessibleInterval< T > img = imgs.get( i );
		imgSizes[ i ] = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) };
		
		interpolators.add( Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess() );
					
		weightAccess.add( weights.get( i ).realRandomAccess() );
	}

	final Cursor< T > cursor = fusedImg.localizingCursor();
	final Cursor< FloatType > cursorW = weightImg.cursor();

	final float[] s = new float[ 3 ];
	final float[] t = new float[ 3 ];
	
	cursor.jumpFwd( portion.getStartPosition() );
	cursorW.jumpFwd( portion.getStartPosition() );
	
	for ( int j = 0; j < portion.getLoopSize(); ++j )
	{
		// move img cursor forward any get the value (saves one access)
		final T v = cursor.next();
		cursor.localize( s );

		// move weight cursor forward and get the value 
		final FloatType w = cursorW.next();

		if ( doDownSampling )
		{
			s[ 0 ] *= downSampling;
			s[ 1 ] *= downSampling;
			s[ 2 ] *= downSampling;
		}
		
		s[ 0 ] += bb.min( 0 );
		s[ 1 ] += bb.min( 1 );
		s[ 2 ] += bb.min( 2 );
		
		double sum = 0;
		double sumW = 0;
		
		for ( int i = 0; i < numViews; ++i )
		{				
			transforms[ i ].applyInverse( t, s );
			
			if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizes[ i ][ 0 ], imgSizes[ i ][ 1 ], imgSizes[ i ][ 2 ] ) )
			{
				final RealRandomAccess< T > r = interpolators.get( i );
				r.setPosition( t );
				
				final RealRandomAccess< FloatType > weight = weightAccess.get( i );
				weight.setPosition( t );
				
				final double w1 = weight.get().get();
				
				sum += r.get().getRealDouble() * w1;
				sumW += w1;
			}
		}
		
		if ( sumW > 0 )
		{
			v.setReal( v.getRealFloat() + sum );
			w.set( w.get() + (float)sumW );
		}
	}
	
	return portion + " finished successfully (one weight).";
}
 
Example 10
Source File: ProcessSequentialPortion.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String call() throws Exception 
{
	final int numViews = imgs.size();
	
	// make the interpolators and get the transformations
	final ArrayList< RealRandomAccess< T > > interpolators = new ArrayList< RealRandomAccess< T > >( numViews );
	final int[][] imgSizes = new int[ numViews ][ 3 ];
	
	for ( int i = 0; i < numViews; ++i )
	{
		final RandomAccessibleInterval< T > img = imgs.get( i );
		imgSizes[ i ] = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) };
		
		interpolators.add( Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess() );
	}

	final Cursor< T > cursor = fusedImg.localizingCursor();
	final Cursor< FloatType > cursorW = weightImg.cursor();
	
	final float[] s = new float[ 3 ];
	final float[] t = new float[ 3 ];
	
	cursor.jumpFwd( portion.getStartPosition() );
	cursorW.jumpFwd( portion.getStartPosition() );
	
	for ( int j = 0; j < portion.getLoopSize(); ++j )
	{
		// move img cursor forward any get the value (saves one access)
		final T v = cursor.next();
		cursor.localize( s );
		
		// move weight cursor forward and get the value 
		final FloatType w = cursorW.next();
		
		if ( doDownSampling )
		{
			s[ 0 ] *= downSampling;
			s[ 1 ] *= downSampling;
			s[ 2 ] *= downSampling;
		}
		
		s[ 0 ] += bb.min( 0 );
		s[ 1 ] += bb.min( 1 );
		s[ 2 ] += bb.min( 2 );
		
		double sum = 0;
		int sumW = 0;
		
		for ( int i = 0; i < numViews; ++i )
		{				
			transforms[ i ].applyInverse( t, s );
			
			if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizes[ i ][ 0 ], imgSizes[ i ][ 1 ], imgSizes[ i ][ 2 ] ) )
			{
				final RealRandomAccess< T > r = interpolators.get( i );
				r.setPosition( t );
				sum += r.get().getRealDouble();
				++sumW;
			}
		}
		
		if ( sumW > 0 )
		{
			v.setReal( v.getRealFloat() + sum );
			w.set( w.get() + sumW );
		}
	}
	
	return portion + " finished successfully (no weights).";
}
 
Example 11
Source File: ProcessSequentialPortionWeights.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String call() throws Exception 
{
	final int numViews = imgs.size();
	
	// make the interpolators, weights and get the transformations
	final ArrayList< RealRandomAccess< T > > interpolators = new ArrayList< RealRandomAccess< T > >( numViews );
	final ArrayList< ArrayList< RealRandomAccess< FloatType > > > weightAccess = new ArrayList< ArrayList< RealRandomAccess< FloatType > > >();
	final int[][] imgSizes = new int[ numViews ][ 3 ];
	
	for ( int i = 0; i < numViews; ++i )
	{
		final RandomAccessibleInterval< T > img = imgs.get( i );
		imgSizes[ i ] = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) };
		
		interpolators.add( Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess() );
		
		final ArrayList< RealRandomAccess< FloatType > > list = new ArrayList< RealRandomAccess< FloatType > >();

		for ( final RealRandomAccessible< FloatType > rra : weights.get( i ) )
			list.add( rra.realRandomAccess() );
		
		weightAccess.add( list );
	}

	final Cursor< T > cursor = fusedImg.localizingCursor();
	final Cursor< FloatType > cursorW = weightImg.cursor();
	
	final float[] s = new float[ 3 ];
	final float[] t = new float[ 3 ];
	
	cursor.jumpFwd( portion.getStartPosition() );
	cursorW.jumpFwd( portion.getStartPosition() );
	
	for ( int j = 0; j < portion.getLoopSize(); ++j )
	{
		// move img cursor forward any get the value (saves one access)
		final T v = cursor.next();
		cursor.localize( s );

		// move weight cursor forward and get the value 
		final FloatType w = cursorW.next();

		if ( doDownSampling )
		{
			s[ 0 ] *= downSampling;
			s[ 1 ] *= downSampling;
			s[ 2 ] *= downSampling;
		}
		
		s[ 0 ] += bb.min( 0 );
		s[ 1 ] += bb.min( 1 );
		s[ 2 ] += bb.min( 2 );
		
		double sum = 0;
		double sumW = 0;
		
		for ( int i = 0; i < numViews; ++i )
		{				
			transforms[ i ].applyInverse( t, s );
			
			if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizes[ i ][ 0 ], imgSizes[ i ][ 1 ], imgSizes[ i ][ 2 ] ) )
			{
				final RealRandomAccess< T > r = interpolators.get( i );
				r.setPosition( t );
				
				double w1 = 1;
				
				for ( final RealRandomAccess< FloatType > weight : weightAccess.get( i ) )
				{
					weight.setPosition( t );
					w1 *= weight.get().get();
				}
				
				sum += r.get().getRealDouble() * w1;
				sumW += w1;
			}
		}
		
		if ( sumW > 0 )
		{
			v.setReal( v.getRealFloat() + sum );
			w.set( w.get() + (float)sumW );
		}
	}
	
	return portion + " finished successfully (many weights).";
}
 
Example 12
Source File: MVDeconvolution.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
protected static Img< FloatType > loadInitialImage(
		final String fileName,
		final boolean checkNumbers,
		final float minValue,
		final Dimensions dimensions,
		final ImgFactory< FloatType > imageFactory )
{
	IOFunctions.println( "Loading image '" + fileName + "' as start for iteration." );

	final ImagePlus impPSI = LegacyStackImgLoaderIJ.open( new File( fileName ) );

	if ( impPSI == null )
	{
		IOFunctions.println( "Could not load image '" + fileName + "'." );
		return null;
	}

	final long[] dimPsi = impPSI.getStack().getSize() == 1 ? 
			new long[]{ impPSI.getWidth(), impPSI.getHeight() } : new long[]{ impPSI.getWidth(), impPSI.getHeight(), impPSI.getStack().getSize() };
	final Img< FloatType > psi = imageFactory.create( dimPsi, new FloatType() );
	LegacyStackImgLoaderIJ.imagePlus2ImgLib2Img( impPSI, psi, false );

	if ( psi == null )
	{
		IOFunctions.println( "Could not load image '" + fileName + "'." );
		return null;
	}
	else
	{
		boolean dimensionsMatch = true;

		final long dim[] = new long[ dimensions.numDimensions() ];

		for ( int d = 0; d < psi.numDimensions(); ++d )
		{
			if ( psi.dimension( d ) != dimensions.dimension( d ) )
				dimensionsMatch = false;

			dim[ d ] = dimensions.dimension( d );
		}

		if ( !dimensionsMatch )
		{
			IOFunctions.println(
					"Dimensions of '" + fileName + "' do not match: " +
					Util.printCoordinates( dimPsi ) + " != " + Util.printCoordinates( dim ) );
			return null;
		}

		if ( checkNumbers )
		{
			IOFunctions.println(
					"Checking values of '" + fileName + "' you can disable this check by setting " +
					"spim.process.fusion.deconvolution.MVDeconvolution.checkNumbers = false;" );

			boolean smaller = false;
			boolean hasZerosOrNeg = false;
			
			for ( final FloatType v : psi )
			{
				if ( v.get() < minValue )
					smaller = true;

				if ( v.get() <= 0 )
				{
					hasZerosOrNeg = true;
					v.set( minValue );
				}
			}

			if ( smaller )
				IOFunctions.println(
						"Some values '" + fileName + "' are smaller than the minimal value of " +
						minValue + ", this can lead to instabilities." );

			if ( hasZerosOrNeg )
				IOFunctions.println(
						"Some values '" + fileName + "' were smaller or equal to zero," +
						"they have been replaced with the min value of " + minValue );
		}
	}

	return psi;
}
 
Example 13
Source File: PartialDerivativeFilterTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void test() {
	Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20 });

	Cursor<FloatType> cursorImg = img.cursor();
	int counterX = 0;
	int counterY = 0;
	while (cursorImg.hasNext()) {
		if (counterX > 8 && counterX < 12 || counterY > 8 && counterY < 12) {
			cursorImg.next().setOne();
		} else {
			cursorImg.next().setZero();
		}
		counterX++;
		if (counterX % 20 == 0) {
			counterY++;
		}
		if (counterX == 20) {
			counterX = 0;
		}
		if (counterY == 20) {
			counterY = 0;
		}
	}

	RandomAccessibleInterval<FloatType> out = ops.filter().partialDerivative(img, 0);

	FloatType type = Util.getTypeFromInterval(out).createVariable();
	type.set(4.0f);
	RandomAccess<FloatType> outRA = out.randomAccess();
	for (int i = 0; i < 8; i++) {
		outRA.setPosition(new int[] { 9, i });
		assertEquals(type, outRA.get());

	}
	outRA.setPosition(new int[] { 9, 8 });
	type.set(3.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 9, 10 });
	type.set(0.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 9, 11 });
	type.set(1.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 9, 12 });
	type.set(3.0f);
	assertEquals(type, outRA.get());
	type.set(4.0f);
	for (int i = 13; i < 20; i++) {
		outRA.setPosition(new int[] { 9, i });
		assertEquals(type, outRA.get());

	}

	type.set(-4.0f);
	for (int i = 0; i < 8; i++) {
		outRA.setPosition(new int[] { 12, i });
		assertEquals(type, outRA.get());

	}
	outRA.setPosition(new int[] { 12, 8 });
	type.set(-3.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 12, 10 });
	type.set(0.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 12, 11 });
	type.set(-1.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 12, 12 });
	type.set(-3.0f);
	assertEquals(type, outRA.get());
	type.set(-4.0f);
	for (int i = 13; i < 20; i++) {
		outRA.setPosition(new int[] { 12, i });
		assertEquals(type, outRA.get());

	}
}
 
Example 14
Source File: HessianFilterTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void test() {
	Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 50, 50 });

	Cursor<FloatType> cursorImg = img.cursor();
	int counterX = 0;
	int counterY = 0;
	while (cursorImg.hasNext()) {
		if (counterX > 20 && counterX < 30 || counterY > 20 && counterY < 30) {
			cursorImg.next().setOne();
		} else {
			cursorImg.next().setZero();
		}
		counterX++;
		if (counterX % 50 == 0) {
			counterY++;
		}
		if (counterX == 50) {
			counterX = 0;
		}
		if (counterY == 50) {
			counterY = 0;
		}
	}

	CompositeIntervalView<FloatType, RealComposite<FloatType>> out = ops.filter().hessian(img);
	
	
	Cursor<RealComposite<FloatType>> outCursor = Views.iterable(out).cursor();
	
	while (outCursor.hasNext()) {
		RealComposite<FloatType> values = outCursor.next();
		assertEquals(values.get(1), values.get(2));
	}

	CompositeView<FloatType, RealComposite<FloatType>>.CompositeRandomAccess outRA = out.randomAccess();
	
	// two numbers represent a coordinate: 20|0 ; 21|0 ...
	int[] positions = new int[] { 20, 0, 21, 0, 19, 31, 19, 30 };
	float[] valuesXX = new float[] { 16.0f, -16.0f, 15.0f, 11.0f };
	float[] valuesXY = new float[] { 0.0f, 0.0f, 1.0f, 3.0f };
	float[] valuesYY = new float[] { 0.0f, 0.0f, 15.0f, 15.0f };

	FloatType type = Util.getTypeFromInterval(img).createVariable();
	int i = 0;
	int j = 0;
	while (i < positions.length - 1) {
		int[] pos = new int[2];
		pos[0] = positions[i];
		pos[1] = positions[i + 1];

		outRA.setPosition(pos);
		type.set(valuesXX[j]);
		assertEquals(type, outRA.get().get(0));

		outRA.setPosition(pos);
		type.set(valuesXY[j]);
		assertEquals(type, outRA.get().get(1));

		outRA.setPosition(pos);
		type.set(valuesYY[j]);
		assertEquals(type, outRA.get().get(3));

		i += 2;
		j++;
	}
}
 
Example 15
Source File: SobelFilterTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void test() {

	Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20 });

	Cursor<FloatType> cursorImg = img.cursor();
	int counterX = 0;
	int counterY = 0;
	while (cursorImg.hasNext()) {
		if (counterX > 8 && counterX < 12 || counterY > 8 && counterY < 12) {
			cursorImg.next().setOne();
		} else {
			cursorImg.next().setZero();
		}
		counterX++;
		if (counterX % 20 == 0) {
			counterY++;
		}
		if (counterX == 20) {
			counterX = 0;
		}
		if (counterY == 20) {
			counterY = 0;
		}
	}
	RandomAccessibleInterval<FloatType> out = ops.filter().sobel(img);

	RandomAccess<FloatType> outRA = out.randomAccess();
	outRA.setPosition(new int[] { 0, 8 });
	FloatType type = Util.getTypeFromInterval(out).createVariable();
	type.set(4.0f);
	assertEquals(type, outRA.get());
	type.setZero();
	outRA.setPosition(new int[] { 0, 10 });
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 10, 8 });
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 10, 10 });
	assertEquals(type, outRA.get());

	outRA.setPosition(new int[] { 10, 12 });
	type.set(0.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 12, 10 });
	assertEquals(type, outRA.get());
}
 
Example 16
Source File: ConvertTypes.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void compute(final C input, final FloatType output) {
	output.set(input.getRealFloat());
}
 
Example 17
Source File: FourNeighborhoodExtremaTest.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testRandomPeaksMT()
{
	Img< FloatType > img = ArrayImgs.floats( 50, 50 );
	Random rnd = new Random( seed );
	
	for( FloatType t : img )
		t.set( rnd.nextFloat() );
	
	ArrayList< Pair< Localizable, Double > > correct = new ArrayList< Pair<Localizable,Double> >(); 
	
	RandomAccess<FloatType> ra = img.randomAccess();
	
	for ( int i = 0; i < 10; ++i ){
		for (int d = 0; d< img.numDimensions(); d++){
			ra.setPosition((int) (rnd.nextDouble() * 50), d);
		}
		ra.get().set((float) (rnd.nextDouble() + 1.0));
		
		correct.add(new ValuePair<Localizable, Double>(new Point(ra), new Double(ra.get().get())));
	}
	
	// sort the peaks in descending order
	Collections.sort(correct, new Comparator<Pair< Localizable, Double >>() {
		@Override
		public int compare(Pair<Localizable, Double> o1, Pair<Localizable, Double> o2) {
			return Double.compare(o2.getB(), o1.getB());
		}
	});
	
	int nMax = 5;
	ArrayList< Pair< Localizable, Double > > found = FourNeighborhoodExtrema.findMaxMT(Views.extendPeriodic(img), img, nMax, Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	assertEquals(nMax, found.size());
	
	
	long[] posCorrect = new long[img.numDimensions()];
	long[] posFound = new long[img.numDimensions()];
	
	for (int i = 0; i<found.size(); i++){			
		assertEquals(correct.get(i).getB(), found.get(i).getB());
		
		correct.get(i).getA().localize(posCorrect);
		found.get(i).getA().localize(posFound);			
		assertArrayEquals(posCorrect, posFound);
	}
}
 
Example 18
Source File: PhaseCorrelationTest.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testPCNegativeShift() {
	
	// TODO: very large shifts (nearly no overlap) lead to incorrect shift determination (as expected)
	// maybe we can optimize behaviour in this situation
	Img< FloatType > img = ArrayImgs.floats( 200, 200 );
	Random rnd = new Random( seed );
	
	for( FloatType t : img )
		t.set( rnd.nextFloat());
	
	long shiftX = -2;
	long shiftY = -2;
	
	FinalInterval interval1 = new FinalInterval(new long[] {50, 50});
	FinalInterval interval2 = Intervals.translate(interval1, shiftX, 0);
	interval2 = Intervals.translate(interval2, shiftY, 1);

	int [] extension = new int[img.numDimensions()];
	Arrays.fill(extension, 10);
	
	RandomAccessibleInterval<FloatType> pcm = PhaseCorrelation2.calculatePCM(
			Views.zeroMin(Views.interval(Views.extendZero( img ), interval1)),
			Views.zeroMin(Views.interval(Views.extendZero( img ), interval2)),
			extension, new ArrayImgFactory<FloatType>(), 
			new FloatType(), new ArrayImgFactory<ComplexFloatType>(), new ComplexFloatType(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	
	PhaseCorrelationPeak2 shiftPeak = PhaseCorrelation2.getShift(pcm,
			Views.zeroMin(Views.interval(Views.extendZero( img ), interval1)),
			Views.zeroMin(Views.interval(Views.extendZero( img ), interval2)),
			20, 0, false, Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	
	long[] expected = new long[]{shiftX, shiftY};
	long[] found = new long[img.numDimensions()];
	
	
	
	shiftPeak.getShift().localize(found);
	System.out.println( Util.printCoordinates( found ) );
	
	assertArrayEquals(expected, found);
	
}
 
Example 19
Source File: PhaseCorrelationTest.java    From BigStitcher with GNU General Public License v2.0 3 votes vote down vote up
@Test
public void testPC() {
	
	// TODO: very large shifts (nearly no overlap) lead to incorrect shift determination (as expected)
	// maybe we can optimize behaviour in this situation
	Img< FloatType > img = ArrayImgs.floats( 200, 200 );
	Random rnd = new Random( seed );
	
	for( FloatType t : img )
		t.set( rnd.nextFloat());
	
	long shiftX = 28;
	long shiftY = 0;
	
	FinalInterval interval1 = new FinalInterval(new long[] {50, 50});
	FinalInterval interval2 = Intervals.translate(interval1, shiftX, 0);
	interval2 = Intervals.translate(interval2, shiftY, 1);

	
	int [] extension = new int[img.numDimensions()];
	Arrays.fill(extension, 10);
	
	RandomAccessibleInterval<FloatType> pcm = PhaseCorrelation2.calculatePCM(Views.interval(img, interval1), Views.zeroMin(Views.interval(img, interval2)), extension, new ArrayImgFactory<FloatType>(), 
			new FloatType(), new ArrayImgFactory<ComplexFloatType>(), new ComplexFloatType(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	
	PhaseCorrelationPeak2 shiftPeak = PhaseCorrelation2.getShift(pcm, Views.interval(img, interval1), Views.zeroMin(Views.interval(img, interval2)), 20, 0, false, Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	
	long[] expected = new long[]{shiftX, shiftY};
	long[] found = new long[img.numDimensions()];
	
	
	
	shiftPeak.getShift().localize(found);
	
	assertArrayEquals(expected, found);
	
}
 
Example 20
Source File: PhaseCorrelationTest.java    From BigStitcher with GNU General Public License v2.0 2 votes vote down vote up
@Test
public void testPCRealShift() {
	
	// TODO: very large shifts (nearly no overlap) lead to incorrect shift determination (as expected)
	// maybe we can optimize behaviour in this situation
	Img< FloatType > img = ArrayImgs.floats( 200, 200 );
	Random rnd = new Random( seed );
	
	for( FloatType t : img )
		t.set( rnd.nextFloat());
	
	double shiftX = -20.9;
	double shiftY = 1.9;
	
	// to test < 0.5 px off
	final double eps = 0.5;
	
	FinalInterval interval2 = new FinalInterval(new long[] {50, 50});
	
	

	AffineRandomAccessible< FloatType, AffineGet > imgTr = RealViews.affine( Views.interpolate( Views.extendZero( img ), new NLinearInterpolatorFactory<>() ), new Translation2D( shiftX, shiftY ));
	IntervalView< FloatType > img2 = Views.interval( Views.raster( imgTr ), interval2);
	
	int [] extension = new int[img.numDimensions()];
	Arrays.fill(extension, 10);
	
	RandomAccessibleInterval<FloatType> pcm = PhaseCorrelation2.calculatePCM(Views.zeroMin(img2), Views.zeroMin(Views.interval(img, interval2)), extension, new ArrayImgFactory<FloatType>(), 
			new FloatType(), new ArrayImgFactory<ComplexFloatType>(), new ComplexFloatType(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	
	PhaseCorrelationPeak2 shiftPeak = PhaseCorrelation2.getShift(pcm, Views.zeroMin(img2), Views.zeroMin(Views.interval(img, interval2)), 20, 0, true, Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	
	
	double[] expected = new double[]{shiftX, shiftY};
	double[] found = new double[img.numDimensions()];
	
	
	
	
	shiftPeak.getSubpixelShift().localize(found);
	
	System.out.println( Util.printCoordinates( found ) );
	
	
	for (int d = 0; d < expected.length; d++)
		assertTrue( Math.abs( expected[d] - found[d] ) < eps );
	
}