Java Code Examples for net.imglib2.RealRandomAccessible#numDimensions()

The following examples show how to use net.imglib2.RealRandomAccessible#numDimensions() . 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: TransformedInterpolatedRealRandomAccess.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
public TransformedInterpolatedRealRandomAccess(
		final RealRandomAccessible< T > realRandomAccessible,
		final T zero,
		final Interval transformedInterval,
		final AffineTransform3D transform,
		final int[] offset )
{
	super( realRandomAccessible.numDimensions() );

	this.transformedInterval = transformedInterval;
	this.zero = zero;
	this.realRandomAccessible = realRandomAccessible;
	this.transform = transform;
	this.offset = new int[ offset.length ];

	for ( int d = 0; d < n; ++d )
		this.offset[ d ] = offset[ d ];

	this.realRandomAccess = realRandomAccessible.realRandomAccess();

	final double[] imatrix = transform.inverse().getRowPackedCopy();

	this.i00 = imatrix[ 0 ];
	this.i01 = imatrix[ 1 ];
	this.i02 = imatrix[ 2 ];
	this.i03 = imatrix[ 3 ];

	this.i10 = imatrix[ 4 ];
	this.i11 = imatrix[ 5 ];
	this.i12 = imatrix[ 6 ];
	this.i13 = imatrix[ 7 ];

	this.i20 = imatrix[ 8 ];
	this.i21 = imatrix[ 9 ];
	this.i22 = imatrix[ 10 ];
	this.i23 = imatrix[ 11 ];

	this.tmp = new float[ n ];
}
 
Example 2
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 3
Source File: OverlayFusion.java    From Stitching with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 fuseChannel( final Img<T> output, final RealRandomAccessible<FloatType> input, final double[] offset, final InvertibleCoordinateTransform transform )
{
	final int dims = output.numDimensions();
	long imageSize = output.dimension( 0 );
	
	for ( int d = 1; d < output.numDimensions(); ++d )
		imageSize *= output.dimension( d );

	// run multithreaded
	final AtomicInteger ai = new AtomicInteger(0);					
       final Thread[] threads = SimpleMultiThreading.newThreads();

       final Vector<Chunk> threadChunks = SimpleMultiThreading.divideIntoChunks( imageSize, threads.length );
       
       for (int ithread = 0; ithread < threads.length; ++ithread)
           threads[ithread] = new Thread(new Runnable()
           {
               @Override
               public void run()
               {
               	// Thread ID
               	final int myNumber = ai.getAndIncrement();
       
               	// get chunk of pixels to process
               	final Chunk myChunk = threadChunks.get( myNumber );
               	final long startPos = myChunk.getStartPosition();
               	final long loopSize = myChunk.getLoopSize();
               	
           		final Cursor<T> out = output.localizingCursor();
           		final RealRandomAccess<FloatType> in = input.realRandomAccess();
           		
           		final double[] tmp = new double[ input.numDimensions() ];
           		
           		try 
           		{
               		// move to the starting position of the current thread
           			out.jumpFwd( startPos );
           			
               		// do as many pixels as wanted by this thread
                       for ( long j = 0; j < loopSize; ++j )
                       {
           				out.fwd();
           				
           				for ( int d = 0; d < dims; ++d )
           					tmp[ d ] = out.getDoublePosition( d ) + offset[ d ];
           				
           				transform.applyInverseInPlace( tmp );
           	
           				in.setPosition( tmp );
           				out.get().setReal( in.get().get() );
           			}
           		} 
           		catch (NoninvertibleModelException e) 
           		{
           			Log.error( "Cannot invert model, qutting." );
           			return;
           		}

               }
           });
       
       SimpleMultiThreading.startAndJoin( threads );
	
       /*
	final LocalizableCursor<T> out = output.createLocalizableCursor();
	final Interpolator<FloatType> in = input.createInterpolator( factory );
	
	final float[] tmp = new float[ input.getNumDimensions() ];
	
	try 
	{
		while ( out.hasNext() )
		{
			out.fwd();
			
			for ( int d = 0; d < dims; ++d )
				tmp[ d ] = out.getPosition( d ) + offset[ d ];
			
			transform.applyInverseInPlace( tmp );

			in.setPosition( tmp );			
			out.getType().setReal( in.getType().get() );
		}
	} 
	catch (NoninvertibleModelException e) 
	{
		Log.error( "Cannot invert model, qutting." );
		return;
	}
	*/
}