net.imglib2.multithreading.SimpleMultiThreading Java Examples
The following examples show how to use
net.imglib2.multithreading.SimpleMultiThreading.
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: OverlayFusion.java From Stitching with GNU General Public License v2.0 | 4 votes |
/** * 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; } */ }
Example #2
Source File: Fusion.java From Stitching with GNU General Public License v2.0 | 4 votes |
/** * 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 fuseBlockNoOverlap( final Img<T> output, final ArrayList< ? extends ImageInterpolation< ? extends RealType< ? > > > input, final double[] offset, final ArrayList< InvertibleBoundable > transform, final boolean displayFusion ) { final int numDimensions = output.numDimensions(); final int numImages = input.size(); // run multithreaded final AtomicInteger ai = new AtomicInteger(0); final Thread[] threads = SimpleMultiThreading.newThreads( numImages ); for (int ithread = 0; ithread < threads.length; ++ithread) threads[ithread] = new Thread( new Runnable() { @Override public void run() { // Thread ID final int myImage = ai.getAndIncrement(); // only the first thread does preview and update the status bar // this requires no synchronized stuff long lastDraw = 0; ImagePlus fusionImp = null; if ( displayFusion && myImage == 0 ) { try { fusionImp = ((ImagePlusImg<?, ?>) output).getImagePlus(); fusionImp.setTitle( "fusing..." ); fusionImp.show(); } catch ( ImgLibException e ) { Log.error( "Output image has no ImageJ type: " + e ); } } final Img< ? extends RealType<?> > image = input.get( myImage ).getImg(); final int[] translation = new int[ numDimensions ]; final InvertibleBoundable t = transform.get( myImage ); final double[] tmp = new double[ numDimensions ]; t.applyInPlace( tmp ); for ( int d = 0; d < numDimensions; ++d ) translation[ d ] = (int) Math.round( tmp[ d ] ); final Cursor< ? extends RealType<?> > cursor = image.localizingCursor(); final RandomAccess< ? extends RealType<?> > randomAccess = output.randomAccess(); final int[] pos = new int[ numDimensions ]; int j = 0; while ( cursor.hasNext() ) { cursor.fwd(); cursor.localize( pos ); // just thread 0 if ( myImage == 0 ) { // just every 10000'th pixel if ( j++ % 10000 == 0 ) { lastDraw = drawFusion( lastDraw, fusionImp ); IJ.showProgress( (double)j / (double)image.size() ); } } for ( int d = 0; d < numDimensions; ++d ) { pos[ d ] += translation[ d ]; pos[ d ] -= offset[ d ]; } randomAccess.setPosition( pos ); randomAccess.get().setReal( cursor.get().getRealFloat() ); } if ( fusionImp != null ) fusionImp.hide(); } }); SimpleMultiThreading.startAndJoin( threads ); }