Java Code Examples for mpicbg.imglib.image.Image#createLocalizableByDimCursor()

The following examples show how to use mpicbg.imglib.image.Image#createLocalizableByDimCursor() . 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: Block.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
private static final void copy( final long start, final long loopSize, final Image< FloatType > source, final Image< FloatType > block, final int[] offset, final boolean inside, final OutOfBoundsStrategyFactory< FloatType > strategyFactory )
{
	final int numDimensions = source.getNumDimensions();
	final LocalizableCursor<FloatType> cursor = block.createLocalizableCursor();
	final LocalizableByDimCursor<FloatType> randomAccess;
	
	if ( inside )
		randomAccess = source.createLocalizableByDimCursor();
	else
		randomAccess = source.createLocalizableByDimCursor( strategyFactory );
	
	cursor.fwd( start );
	
	final int[] tmp = new int[ numDimensions ];
	
	for ( long l = 0; l < loopSize; ++l )
	{
		cursor.fwd();
		cursor.getPosition( tmp );
		
		for ( int d = 0; d < numDimensions; ++d )
			tmp[ d ] += offset[ d ];
		
		randomAccess.setPosition( tmp );
		cursor.getType().set( randomAccess.getType() );
	}
}
 
Example 2
Source File: Block.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
private static final void copy3dArray( final int threadIdx, final int numThreads, final Image< FloatType > source, final Image< FloatType > block, final int[] offset, 
		final boolean inside, final OutOfBoundsStrategyFactory< FloatType > strategyFactory )
{
	final int w = block.getDimension( 0 );
	final int h = block.getDimension( 1 );
	final int d = block.getDimension( 2 );
	
	final int offsetX = offset[ 0 ];
	final int offsetY = offset[ 1 ];
	final int offsetZ = offset[ 2 ];
	final float[] blockArray = ((FloatArray)((Array)block.getContainer()).update( null )).getCurrentStorageArray();
	
	final LocalizableByDimCursor3D<FloatType> randomAccess;
	
	if ( inside )
		randomAccess = (LocalizableByDimCursor3D<FloatType>)source.createLocalizableByDimCursor();
	else
		randomAccess = (LocalizableByDimCursor3D<FloatType>)source.createLocalizableByDimCursor( strategyFactory );		
	
	for ( int z = threadIdx; z < d; z += numThreads )
	{
		randomAccess.setPosition( offsetX, offsetY, z + offsetZ );
		
		int i = z * h * w;
		
		for ( int y = 0; y < h; ++y )
		{
			randomAccess.setPosition( offsetX, 0 );
			
			for ( int x = 0; x < w; ++x )
			{
				blockArray[ i++ ] = randomAccess.getType().get();
				randomAccess.fwdX();
			}
			
			randomAccess.move( -w, 0 );
			randomAccess.fwdY();
		}
	}
}
 
Example 3
Source File: Block.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
private static final void paste( final long start, final long loopSize, final Image< FloatType > target, final Image< FloatType > block, 
		final int[] effectiveOffset, final int[] effectiveSize, final int[] effectiveLocalOffset )
{
	final int numDimensions = target.getNumDimensions();
	
	// iterate over effective size
	final LocalizableCursor<?> cursor = ArrayLocalizableCursor.createLinearCursor( effectiveSize );
	
	// read from block
	final LocalizableByDimCursor<FloatType> blockRandomAccess  = block.createLocalizableByDimCursor();
	
	// write to target		
	final LocalizableByDimCursor<FloatType> targetRandomAccess  = target.createLocalizableByDimCursor();
	
	cursor.fwd( start );
	
	final int[] tmp = new int[ numDimensions ];
	
	for ( long l = 0; l < loopSize; ++l )
	{
		cursor.fwd();
		cursor.getPosition( tmp );
		
		// move to the relative local offset where the real data starts
		for ( int d = 0; d < numDimensions; ++d )
			tmp[ d ] += effectiveLocalOffset[ d ];
		
		blockRandomAccess.setPosition( tmp );
		
		// move to the right position in the image
		for ( int d = 0; d < numDimensions; ++d )
			tmp[ d ] += effectiveOffset[ d ] - effectiveLocalOffset[ d ];

		targetRandomAccess.setPosition( tmp );

		// write the pixel
		targetRandomAccess.getType().set( blockRandomAccess.getType() );
	}
}
 
Example 4
Source File: ExtractPSF.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make image the same size as defined, center it
 * 
 * @param img
 * @return
 */
public static Image< FloatType > makeSameSize( final Image< FloatType > img, final int[] sizeIn )
{
	final int[] size = sizeIn.clone();

	float min = Float.MAX_VALUE;

	for ( final FloatType f : img )
		min = Math.min( min, f.get() );
	
	final Image< FloatType > square = img.createNewImage( size );
	
	final LocalizableCursor< FloatType > squareCursor = square.createLocalizableCursor();
	final LocalizableByDimCursor< FloatType > inputCursor = img.createLocalizableByDimCursor( new OutOfBoundsStrategyValueFactory<FloatType>( new FloatType( min ) ) );
	
	while ( squareCursor.hasNext() )
	{
		squareCursor.fwd();
		squareCursor.getPosition( size );
		
		for ( int d = 0; d < img.getNumDimensions(); ++d )
			size[ d ] =  size[ d ] - square.getDimension( d )/2 + img.getDimension( d )/2;

		inputCursor.setPosition( size );
		squareCursor.getType().set( inputCursor.getType().get() );
	}
	
	return square;
}
 
Example 5
Source File: BeadSegmentation.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
public Image<FloatType> getFoundBeads( final ViewDataBeads view )
{
	// display found beads
	ImageFactory<FloatType> factory = new ImageFactory<FloatType>( new FloatType(), view.getViewStructure().getSPIMConfiguration().inputImageFactory );
	Image<FloatType> img = factory.createImage( view.getImageSize() );
	
	LocalizableByDimCursor3D<FloatType> cursor = (LocalizableByDimCursor3D<FloatType>) img.createLocalizableByDimCursor();		

   	final float[] tmp = new float[ img.getNumDimensions() ];

	for ( Bead bead : view.getBeadStructure().getBeadList())
	{
       	for ( int d = 0; d < tmp.length; ++d )
       		tmp[ d ] = (float)bead.getL()[ d ];

		final LocalizablePoint p = new LocalizablePoint( tmp );
		
		HyperSphereIterator<FloatType> it = new HyperSphereIterator<FloatType>( img, p, 1, new OutOfBoundsStrategyValueFactory<FloatType>() );
		
		for ( final FloatType f : it )
			f.setOne();			
	}
	
	cursor.close();

	return img;
}
 
Example 6
Source File: BeadSegmentation.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
protected ArrayList<Integer> getNeighboringLabels( final Image<IntType> connectedComponents, final ArrayList<Point3i> neighbors, final int x, final int y, final int z )
{
	final ArrayList<Integer> labels = new ArrayList<Integer>();
	final Iterator<Point3i> iterateNeighbors = neighbors.iterator();
	
	final int w = connectedComponents.getDimension( 0 );
	final int h = connectedComponents.getDimension( 1 );
	final int d = connectedComponents.getDimension( 2 );
	
	final LocalizableByDimCursor3D<IntType> cursor = (LocalizableByDimCursor3D<IntType>) connectedComponents.createLocalizableByDimCursor();

	while (iterateNeighbors.hasNext())
	{
		Point3i neighbor = iterateNeighbors.next();

		int xp = x + neighbor.x;
		int yp = y + neighbor.y;
		int zp = z + neighbor.z;

		if (xp >= 0 && yp >= 0 && zp >= 0 && xp < w && yp < h && zp < d )
		{
			cursor.setPosition( xp, yp, zp );
			int label = cursor.getType().get();

			if (label != 0 && !labels.contains(neighbor)) 
				labels.add(label);
		}
	}
	
	cursor.close();

	return labels;
}
 
Example 7
Source File: BinaryInterpolation2D.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
IDT2D(final Image<BitType> img) {
	this.w = img.getDimension(0);
	this.h = img.getDimension(1);
	ImageFactory<IntType> f = new ImageFactory<IntType>(new IntType(), new ArrayContainerFactory());
	this.result = f.createImage(new int[]{w, h});

	// Set all result pixels to infinity
	final int infinity = (w + h) * 9;
	for (final IntType v : this.result) {
		v.set(infinity);
	}

	// init result pixels with those of the image:
	this.csrc = img.createLocalizableByDimCursor();
	this.cout = result.createLocalizableByDimCursor();

	int count = 0;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			if (isBoundary(x, y)) {
				setOutValueAt(x, y, 0);
				count++;
			} else if (isJustOutside(x, y)) {
				setOutValueAt(x, y, -1);
			}
		}
	}

	if (count > 0) {
		propagate();
	}

	csrc.close();
	cout.close();
}
 
Example 8
Source File: Block.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
private static final void copy3d( final int threadIdx, final int numThreads, final Image< FloatType > source, final Image< FloatType > block, final int[] offset, 
		final boolean inside, final OutOfBoundsStrategyFactory< FloatType > strategyFactory )
{
	final int w = block.getDimension( 0 );
	final int h = block.getDimension( 1 );
	final int d = block.getDimension( 2 );
	
	final int offsetX = offset[ 0 ];
	final int offsetY = offset[ 1 ];
	final int offsetZ = offset[ 2 ];
	final float[] blockArray = ((FloatArray)((Array)block.getContainer()).update( null )).getCurrentStorageArray();
	
	final LocalizableByDimCursor<FloatType> randomAccess;
	
	if ( inside )
		randomAccess = source.createLocalizableByDimCursor();
	else
		randomAccess = source.createLocalizableByDimCursor( strategyFactory );		
	
	final int[] tmp = new int[]{ offsetX, offsetY, 0 };
	
	for ( int z = threadIdx; z < d; z += numThreads )
	{
		tmp[ 2 ] = z + offsetZ;
		randomAccess.setPosition( tmp );
		
		int i = z * h * w;
		
		for ( int y = 0; y < h; ++y )
		{
			randomAccess.setPosition( offsetX, 0 );
			
			for ( int x = 0; x < w; ++x )
			{
				blockArray[ i++ ] = randomAccess.getType().get();
				randomAccess.fwd( 0 );
			}
			
			randomAccess.move( -w, 0 );
			randomAccess.fwd( 1 );
		}
	}
}
 
Example 9
Source File: DOM.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
final public static void mean( final Image< LongType> integralImg, final Image< FloatType > domImg, final int sx, final int sy, final int sz )
{
	final float sumPixels = sx * sy * sz;
	
	final int sxHalf = sx / 2;
	final int syHalf = sy / 2;
	final int szHalf = sz / 2;

	final int w = domImg.getDimension( 0 ) - ( sx / 2 ) * 2; // this makes sense as sx is odd
	final int h = domImg.getDimension( 1 ) - ( sy / 2 ) * 2;
	final int d = domImg.getDimension( 2 ) - ( sz / 2 ) * 2;

	final AtomicInteger ai = new AtomicInteger(0);					
       final Thread[] threads = SimpleMultiThreading.newThreads();
	final int numThreads = threads.length;
       
       for (int ithread = 0; ithread < threads.length; ++ithread)
           threads[ithread] = new Thread(new Runnable()
           {
               public void run()
               {
               	// Thread ID
               	final int myNumber = ai.getAndIncrement();

           		// for each computation we need 8 randomaccesses, so 16 all together
           		final LocalizableByDimCursor< LongType > r1 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r2 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r3 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r4 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r5 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r6 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r7 = integralImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< LongType > r8 = integralImg.createLocalizableByDimCursor();
           		
           		final LocalizableByDimCursor< FloatType > result = domImg.createLocalizableByDimCursor();
           		
           		final int[] p = new int[ 3 ];

           		for ( int z = 0; z < d; ++z )
           		{
           			if ( z % numThreads == myNumber )
           			{
            			for ( int y = 0; y < h; ++y )
            			{
            				// set the result randomaccess
            				p[ 0 ] = sxHalf; p[ 1 ] = y + syHalf; p[ 2 ] = z + szHalf;
            				result.setPosition( p );
            				
            				// set all randomaccess for the first box accordingly
            				p[ 0 ] = 0; p[ 1 ] = y; p[ 2 ] = z;
            				r1.setPosition( p ); // negative

            				p[ 0 ] += sx;
            				r2.setPosition( p ); // positive
            				
            				p[ 1 ] += sy;
            				r3.setPosition( p ); // negative
            				
            				p[ 0 ] -= sx;
            				r4.setPosition( p ); // positive

            				p[ 2 ] += sz;
            				r5.setPosition( p ); // negative

            				p[ 0 ] += sx;
            				r6.setPosition( p ); // positive

            				p[ 1 ] -= sy;
            				r7.setPosition( p ); // negative

            				p[ 0 ] -= sx;
            				r8.setPosition( p ); // positive

            				for ( int x = 0; x < w; ++x )
            				{
            					final long s = -r1.getType().get() + r2.getType().get() - r3.getType().get() + r4.getType().get() - r5.getType().get() + r6.getType().get() - r7.getType().get() + r8.getType().get();

            					result.getType().set( (float)s/sumPixels );
            					
            					r1.fwd( 0 ); r2.fwd( 0 ); r3.fwd( 0 ); r4.fwd( 0 ); r5.fwd( 0 ); r6.fwd( 0 ); r7.fwd( 0 ); r8.fwd( 0 );
            					result.fwd( 0 );
            				}
            			}
           			}
           		}            		
               }
           });

       SimpleMultiThreading.startAndJoin( threads );
	}
 
Example 10
Source File: DOM.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
final public static void meanMirror( final Image< LongType> integralImg, final Image< FloatType > domImg, final int sx, final int sy, final int sz )
{
	mean( integralImg, domImg, sx, sy, sz );
			
	final int sxHalf = sx / 2;
	final int syHalf = sy / 2;
	final int szHalf = sz / 2;
	
	final int sxHalf2 = sxHalf * 2;
	final int syHalf2 = syHalf * 2;
	final int szHalf2 = szHalf * 2;
	
	final int w = domImg.getDimension( 0 );
	final int h = domImg.getDimension( 1 );
	final int d = domImg.getDimension( 2 );
	
	final int w1 = w - sxHalf - 1;
	final int h1 = h - syHalf - 1;
	final int d1 = d - szHalf - 1;
	
	final AtomicInteger ai = new AtomicInteger(0);					
       final Thread[] threads = SimpleMultiThreading.newThreads();
	final int numThreads = threads.length;
       
       for (int ithread = 0; ithread < threads.length; ++ithread)
           threads[ithread] = new Thread(new Runnable()
           {
               public void run()
               {
               	// Thread ID
               	final int myNumber = ai.getAndIncrement();

           		final LocalizableByDimCursor< FloatType > c1 = domImg.createLocalizableByDimCursor();
           		final LocalizableByDimCursor< FloatType > c2 = domImg.createLocalizableByDimCursor();

				final int[] p1 = new int[ 3 ];
				final int[] p2 = new int[ 3 ];
		
				// fill the remaining pixels with a mirror strategy (they are mostly blended away anyways)
				for ( int z = 0; z < d; ++z )
				{
					if ( z % numThreads == myNumber )
           			{
						boolean zSmaller = z < szHalf;
						boolean zBigger = z > d1;
			
						if ( zSmaller )
							p1[ 2 ] =  szHalf2 - z;
						else if ( zBigger )
							p1[ 2 ] = 2*d1 - z;
						else
							p1[ 2 ] = z;
			
						p2[ 2 ] = z;
						
						for ( int y = 0; y < h; ++y )
						{
							boolean ySmaller = y < syHalf;
							boolean yBigger = y > h1;
			
							if ( ySmaller )
								p1[ 1 ] =  syHalf2 - y;
							else if ( yBigger )
								p1[ 1 ] = 2*h1 - y;
							else
								p1[ 1 ] = y;
							
							p2[ 1 ] = y;
							
							for ( int x = 0; x < w; ++x )
							{
								boolean xSmaller = x < sxHalf;
								boolean xBigger = x > w1;
								
								if ( xSmaller || ySmaller || zSmaller || xBigger || yBigger || zBigger )
								{
									p2[ 0 ] = x; 
									c1.setPosition( p2 );
									
									if ( xSmaller )
										p1[ 0 ] =  sxHalf2 - x;
									else if ( xBigger )
										p1[ 0 ] = 2*w1 - x;
									else
										p1[ 0 ] = x;
									
									c2.setPosition( p1 );
									c1.getType().set( c2.getType().get() );
								}
							}
						}
           			}
				}
               }
           });

       SimpleMultiThreading.startAndJoin( threads );
	}
 
Example 11
Source File: DOM.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
final public static void computeDifferencOfMean( final Image< LongType> integralImg, final Image< FloatType > domImg, final int sx1, final int sy1, final int sz1, final int sx2, final int sy2, final int sz2, final float min, final float max  )
{
	final float diff = max - min;
	
	final float sumPixels1 = sx1 * sy1 * sz1;
	final float sumPixels2 = sx2 * sy2 * sz2;
	
	final float d1 = sumPixels1 * diff;
	final float d2 = sumPixels2 * diff;
	
	final int sx1Half = sx1 / 2;
	final int sy1Half = sy1 / 2;
	final int sz1Half = sz1 / 2;

	final int sx2Half = sx2 / 2;
	final int sy2Half = sy2 / 2;
	final int sz2Half = sz2 / 2;
	
	final int sxHalfMax = Math.max( sx1Half, sx2Half );
	final int syHalfMax = Math.max( sy1Half, sy2Half );
	final int szHalfMax = Math.max( sz1Half, sz2Half );

	final int w = domImg.getDimension( 0 ) - ( Math.max( sx1, sx2 ) / 2 ) * 2;
	final int h = domImg.getDimension( 1 ) - ( Math.max( sy1, sy2 ) / 2 ) * 2;
	final int d = domImg.getDimension( 2 ) - ( Math.max( sz1, sz2 ) / 2 ) * 2;
			
	final long imageSize = domImg.getNumPixels();

	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()
           {
               public void run()
               {
               	// Thread ID
               	final int myNumber = ai.getAndIncrement();
       
               	final int[] position = new int[ 3 ];
               	
               	// get chunk of pixels to process
               	final Chunk myChunk = threadChunks.get( myNumber );
               	final long loopSize = myChunk.getLoopSize();
               	
           		final LocalizableCursor< FloatType > cursor = domImg.createLocalizableCursor();
           		final LocalizableByDimCursor< LongType > randomAccess = integralImg.createLocalizableByDimCursor();

           		cursor.fwd( myChunk.getStartPosition() );
           		
           		// do as many pixels as wanted by this thread
                   for ( long j = 0; j < loopSize; ++j )
                   {                    	
           			final FloatType result = cursor.next();
           			
           			final int x = cursor.getPosition( 0 );
           			final int y = cursor.getPosition( 1 );
           			final int z = cursor.getPosition( 2 );
           			
           			final int xt = x - sxHalfMax;
           			final int yt = y - syHalfMax;
           			final int zt = z - szHalfMax;
           			
           			if ( xt >= 0 && yt >= 0 && zt >= 0 && xt < w && yt < h && zt < d )
           			{
           				position[ 0 ] = x - sx1Half;
           				position[ 1 ] = y - sy1Half;
           				position[ 2 ] = z - sz1Half;
           				randomAccess.setPosition( position );
           				final float s1 = computeSum2( sx1, sy1, sz1, randomAccess ) / d1;
           				
           				position[ 0 ] = x - sx2Half;
           				position[ 1 ] = y - sy2Half;
           				position[ 2 ] = z - sz2Half;
           				randomAccess.setPosition( position );
           				final float s2 = computeSum2( sx2, sy2, sz2, randomAccess ) / d2;

           				result.set( ( s2 - s1 ) );
             			}
           			else
           			{
           				result.set( 0 );
           			}
                   }
               }
           });
       
       SimpleMultiThreading.startAndJoin( threads );
       
       //ImageJFunctions.show( tmp1  ).setTitle( "s2" );
       //ImageJFunctions.show( tmp2  ).setTitle( "s1" );
       //ImageJFunctions.show( tmp3  ).setTitle( "1" );
	}
 
Example 12
Source File: InteractiveIntegral.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
final public static void computeDifferencOfMeanSlice( final Image< LongType> integralImg, final Image< FloatType > sliceImg, final int z, final int sx1, final int sy1, final int sz1, final int sx2, final int sy2, final int sz2, final float min, final float max  )
{
	final float sumPixels1 = sx1 * sy1 * sz1;
	final float sumPixels2 = sx2 * sy2 * sz2;
	
	final int sx1Half = sx1 / 2;
	final int sy1Half = sy1 / 2;
	final int sz1Half = sz1 / 2;

	final int sx2Half = sx2 / 2;
	final int sy2Half = sy2 / 2;
	final int sz2Half = sz2 / 2;
	
	final int sxHalfMax = Math.max( sx1Half, sx2Half );
	final int syHalfMax = Math.max( sy1Half, sy2Half );
	final int szHalfMax = Math.max( sz1Half, sz2Half );

	final int w = sliceImg.getDimension( 0 ) - ( Math.max( sx1, sx2 ) / 2 ) * 2;
	final int h = sliceImg.getDimension( 1 ) - ( Math.max( sy1, sy2 ) / 2 ) * 2;
	final int d = (integralImg.getDimension( 2 ) - 1) - ( Math.max( sz1, sz2 ) / 2 ) * 2;
			
	final long imageSize = w * h;

	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()
           {
               public void run()
               {
               	// Thread ID
               	final int myNumber = ai.getAndIncrement();
       
               	// get chunk of pixels to process
               	final Chunk myChunk = threadChunks.get( myNumber );
               	final long loopSize = myChunk.getLoopSize();
               	
           		final LocalizableCursor< FloatType > cursor = sliceImg.createLocalizableCursor();
           		final LocalizableByDimCursor< LongType > randomAccess = integralImg.createLocalizableByDimCursor();

           		cursor.fwd( myChunk.getStartPosition() );
           		
           		// do as many pixels as wanted by this thread
                   for ( long j = 0; j < loopSize; ++j )
                   {                    	
           			final FloatType result = cursor.next();
           			
           			final int x = cursor.getPosition( 0 );
           			final int y = cursor.getPosition( 1 );
           			//final int z = cursor.getPosition( 2 );
           			
           			final int xt = x - sxHalfMax;
           			final int yt = y - syHalfMax;
           			final int zt = z - szHalfMax;
           			
           			if ( xt >= 0 && yt >= 0 && zt >= 0 && xt < w && yt < h && zt < d )
           			{
           				final float s1 = DOM.computeSum( x - sx1Half, y - sy1Half, z - sz1Half, sx1, sy1, sz1, randomAccess ) / sumPixels1;
           				final float s2 = DOM.computeSum( x - sx2Half, y - sy2Half, z - sz2Half, sx2, sy2, sz2, randomAccess ) / sumPixels2;
           			
           				result.set( ( s2 - s1 ) / ( max - min ) );
           			}
                   }
               }
           });
       
       SimpleMultiThreading.startAndJoin( threads );
	}
 
Example 13
Source File: InteractiveIntegral.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
public static ArrayList<SimplePeak> findPeaks( final Image<FloatType> laPlace, final float minValue )
	{
	    final AtomicInteger ai = new AtomicInteger( 0 );
	    final Thread[] threads = SimpleMultiThreading.newThreads( Threads.numThreads() );
	    final int nThreads = threads.length;
	    final int numDimensions = laPlace.getNumDimensions();
	    
	    final Vector< ArrayList<SimplePeak> > threadPeaksList = new Vector< ArrayList<SimplePeak> >();
	    
	    for ( int i = 0; i < nThreads; ++i )
	    	threadPeaksList.add( new ArrayList<SimplePeak>() );

		for (int ithread = 0; ithread < threads.length; ++ithread)
	        threads[ithread] = new Thread(new Runnable()
	        {
	            public void run()
	            {
	            	final int myNumber = ai.getAndIncrement();
            	
	            	final ArrayList<SimplePeak> myPeaks = threadPeaksList.get( myNumber );	
	            	final LocalizableByDimCursor<FloatType> cursor = laPlace.createLocalizableByDimCursor();	            	
	            	final LocalNeighborhoodCursor<FloatType> neighborhoodCursor = LocalNeighborhoodCursorFactory.createLocalNeighborhoodCursor( cursor );
	            	
	            	final int[] position = new int[ numDimensions ];
	            	final int[] dimensionsMinus2 = laPlace.getDimensions();

            		for ( int d = 0; d < numDimensions; ++d )
            			dimensionsMinus2[ d ] -= 2;
	            	
MainLoop:           while ( cursor.hasNext() )
	                {
	                	cursor.fwd();
	                	cursor.getPosition( position );
	                	
	                	if ( position[ 0 ] % nThreads == myNumber )
	                	{
	                		for ( int d = 0; d < numDimensions; ++d )
	                		{
	                			final int pos = position[ d ];
	                			
	                			if ( pos < 1 || pos > dimensionsMinus2[ d ] )
	                				continue MainLoop;
	                		}

	                		// if we do not clone it here, it might be moved along with the cursor
	                		// depending on the container type used
	                		final float currentValue = cursor.getType().get();
	                		
	                		// it can never be a desired peak as it is too low
	                		if ( Math.abs( currentValue ) < minValue )
                				continue;

                			// update to the current position
                			neighborhoodCursor.update();

                			// we have to compare for example 26 neighbors in the 3d case (3^3 - 1) relative to the current position
                			final SpecialPoint specialPoint = isSpecialPoint( neighborhoodCursor, currentValue ); 
                			
                			if ( specialPoint == SpecialPoint.MIN )
                				myPeaks.add( new SimplePeak( position, Math.abs( currentValue ), true, false ) ); //( position, currentValue, specialPoint ) );
                			else if ( specialPoint == SpecialPoint.MAX )
                				myPeaks.add( new SimplePeak( position, Math.abs( currentValue ), false, true ) ); //( position, currentValue, specialPoint ) );
                			
                			// reset the position of the parent cursor
                			neighborhoodCursor.reset();	                				                		
	                	}
	                }
                
	                cursor.close();
            }
        });
	
		SimpleMultiThreading.startAndJoin( threads );		

		// put together the list from the various threads	
		final ArrayList<SimplePeak> dogPeaks = new ArrayList<SimplePeak>();
		
		for ( final ArrayList<SimplePeak> peakList : threadPeaksList )
			dogPeaks.addAll( peakList );		
		
		return dogPeaks;
	}
 
Example 14
Source File: EntropyFloatArray3D.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
public static Image<FloatType> computeEntropy(final Image<FloatType> image, final ContainerFactory entropyType, final int histogramBins, final int windowSizeX, final int windowSizeY, final int windowSizeZ)
{
	final float maxEntropy = getMaxEntropy(histogramBins);
	
	final ImageFactory<FloatType> factory = new ImageFactory<FloatType>( new FloatType(), entropyType );
	final Image<FloatType> entropy = factory.createImage( image.getDimensions(), "Entropy of " + image.getName() );

	final LocalizableByDimCursor3D<FloatType> it = (LocalizableByDimCursor3D<FloatType>)entropy.createLocalizableByDimCursor();
	
	final EntropyFloatArray3D entropyObject = EntropyFloatArray3D.initEntropy( image, histogramBins, windowSizeX, windowSizeY, windowSizeZ, 0, 0, 0 );
	
	final int directionZ = +1; 
	int directionY = +1;
	int directionX = +1;
	
	for (int z = 0; z < image.getDimension( 2 ); z++)
	{
		for (int y = 0; y < image.getDimension( 1 ); y++)
   		{
   			for (int x = 0; x < image.getDimension( 0 ); x++)
   			{
   				if (x != 0)
   					entropyObject.updateEntropyX(directionX);
   				    				
   				it.setPosition( entropyObject.getX(), entropyObject.getY(), entropyObject.getZ() );
   				it.getType().set( entropyObject.getEntropy() / maxEntropy );
			}    			
   			directionX *= -1;
   			
   			if (y != image.getDimension( 1 ) - 1)
   				entropyObject.updateEntropyY(directionY);
   		}                            		
   		directionY *= -1;
   		
		if (z != image.getDimension( 2 ) - 1)
			entropyObject.updateEntropyZ(directionZ);                            		
	}
	
	entropyObject.cIn.close();
	entropyObject.cOut.close();
	
	return entropy;
}
 
Example 15
Source File: Entropy.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
public static Image<FloatType> computeEntropy(final Image<FloatType> img, final ContainerFactory entropyType, final int histogramBins, final int windowSizeX, final int windowSizeY, final int windowSizeZ)
{
	// check if we can use fast forward algorithm		
	if ( Array3D.class.isInstance( img.getContainer() ) )
	{
		IOFunctions.println("Input is instance of Image<Float> using an Array3D, fast forward algorithm --- Fast Forward Algorithm available.");
		return EntropyFloatArray3D.computeEntropy( img, entropyType, histogramBins, windowSizeX, windowSizeY, windowSizeZ); 
	}	
	
	final float maxEntropy = getMaxEntropy(histogramBins);
	final ImageFactory<FloatType> factory = new ImageFactory<FloatType>( new FloatType(), entropyType );
	final Image<FloatType> entropy = factory.createImage( img.getDimensions(), "Entropy of " + img.getName() );
	
	final LocalizableByDimCursor<FloatType> entropyIterator = entropy.createLocalizableByDimCursor( new OutOfBoundsStrategyMirrorFactory<FloatType>() );
	
	final Entropy ei = Entropy.initEntropy( img, histogramBins, windowSizeX, windowSizeY, windowSizeZ, 0, 0, 0);
	
	final int directionZ = +1; 
	int directionY = +1;
	int directionX = +1;	
	
	final int width = img.getDimension( 0 );
	final int height = img.getDimension( 1 );
	final int depth = img.getDimension( 2 );
	
	for (int z = 0; z < depth; z++)
	{    			
		for (int y = 0; y < height; y++)
   		{
   			for (int x = 0; x < width; x++)
   			{
   				if (x != 0)
   					ei.updateEntropyX(directionX);
   				    	
   				entropyIterator.move( ei.getX() - entropyIterator.getPosition(0), 0 );
   				entropyIterator.move( ei.getY() - entropyIterator.getPosition(1), 1 );
   				entropyIterator.move( ei.getZ() - entropyIterator.getPosition(2), 2 );
   				
   				entropyIterator.getType().set( ei.getEntropy() / maxEntropy );
			}    			
   			directionX *= -1;
   			
   			if (y != height - 1)
   				ei.updateEntropyY(directionY);
   		}                            		
   		directionY *= -1;
   		
		if (z != depth - 1)
			ei.updateEntropyZ(directionZ);                            		
	}
			
	entropyIterator.close();
	ei.close();
	
	return entropy;
}
 
Example 16
Source File: Entropy.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
public static Entropy initEntropy( final Image<FloatType> img, final int histogramBins, final int windowSizeX, final int windowSizeY, final int windowSizeZ, final int x, final int y, final int z)
{
	final LocalizableByDimCursor<FloatType> cursor = img.createLocalizableByDimCursor( new OutOfBoundsStrategyMirrorFactory<FloatType>() );
	
	// arrays for guessing the probabilities
	final Entropy ei = new Entropy(0.001f, img, cursor, histogramBins, windowSizeX, windowSizeY, windowSizeZ, x, y, z);
	
	//
	// fill arrays
	//
	for (int zs = z - ei.windowSizeZHalf; zs <= z + ei.windowSizeZHalf; zs++)
		for (int ys = y - ei.windowSizeYHalf; ys <= y + ei.windowSizeYHalf; ys++)
			for (int xs = x - ei.windowSizeXHalf; xs <= x + ei.windowSizeXHalf; xs++)
			{
				// compute bin
				cursor.move( xs - cursor.getPosition( 0 ), 0 );
				cursor.move( ys - cursor.getPosition( 1 ), 1 );
				cursor.move( zs - cursor.getPosition( 2 ), 2 );
				
				int bin = (int) (cursor.getType().get() * histogramBins);

				// for the case of value being exactly 1
				if (bin >= histogramBins) bin = histogramBins - 1;
				if (bin < 0) bin = 0;

				ei.absFreq[bin]++;					
			}
			
	//
	// make probablities and compute the entropy
	//		
	ei.entropy = 0;
	for (int bin = 0; bin < histogramBins; bin++)
	{
		if (ei.absFreq[bin] > 0)
		{
			final float prob = ei.absFreq[bin] / ei.size;				
			ei.entropy -= prob * (Math.log(prob) / Math.log(2));
		}
	}
	
	IOFunctions.println(ei.entropy);
	
	return ei;
}