net.imglib2.exception.ImgLibException Java Examples

The following examples show how to use net.imglib2.exception.ImgLibException. 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: LRFFT.java    From SPIM_Registration with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public static final Image< FloatType > wrap( final Img< net.imglib2.type.numeric.real.FloatType > i )
{
	if ( i instanceof ImagePlusImg )
	{
		try
		{
			return ImageJFunctions.wrapFloat( ((ImagePlusImg) i).getImagePlus() );
		}
		catch (ImgLibException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}			
	}
	else
	{
		return ImgLib2.wrapFloatToImgLib1( i );
	}
}
 
Example #2
Source File: LRFFT.java    From SPIM_Registration with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public static final Img< net.imglib2.type.numeric.real.FloatType > wrap( final Image< FloatType > i )
{
	final ContainerFactory c = i.getContainerFactory();
	
	if ( c instanceof ImagePlusContainerFactory )
	{
		try
		{
			return net.imglib2.img.display.imagej.ImageJFunctions.wrapFloat( ((ImagePlusContainer)i.getContainer()).getImagePlus() );
		}
		catch (mpicbg.imglib.exception.ImgLibException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}			
	}
	else
	{
		return ImgLib1.wrapFloatToImgLib2( i );
	}
}
 
Example #3
Source File: DisplayImage.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static < T extends RealType< T > & NativeType< T > > ImagePlus getImagePlusInstance(
		final RandomAccessibleInterval< T > img,
		final boolean virtualDisplay,
		final String title,
		final double min,
		final double max )
{
	ImagePlus imp = null;

	if ( img instanceof ImagePlusImg )
		try { imp = ((ImagePlusImg<T, ?>)img).getImagePlus(); } catch (ImgLibException e) {}

	if ( imp == null )
	{
		if ( virtualDisplay )
			imp = ImageJFunctions.wrap( img, title );
		else
			imp = ImageJFunctions.wrap( img, title ).duplicate();
	}

	imp.setTitle( title );
	imp.setDimensions( 1, (int)img.dimension( 2 ), 1 );
	imp.setDisplayRange( min, max );

	return imp;
}
 
Example #4
Source File: OverlayFusion.java    From Stitching with GNU General Public License v2.0 4 votes vote down vote up
public static < T extends RealType< T > & NativeType< T > > ImagePlus createReRegisteredSeries( final T targetType, final ImagePlus imp, final ArrayList<InvertibleBoundable> models, final int dimensionality )
{
	final int numImages = imp.getNFrames();

	// the size of the new image
	final int[] size = new int[ dimensionality ];
	// the offset relative to the output image which starts with its local coordinates (0,0,0)
	final double[] offset = new double[ dimensionality ];

	final int[][] imgSizes = new int[ numImages ][ dimensionality ];
	
	for ( int i = 0; i < numImages; ++i )
	{
		imgSizes[ i ][ 0 ] = imp.getWidth();
		imgSizes[ i ][ 1 ] = imp.getHeight();
		if ( dimensionality == 3 )
			imgSizes[ i ][ 2 ] = imp.getNSlices();
	}
	
	// estimate the boundaries of the output image and the offset for fusion (negative coordinates after transform have to be shifted to 0,0,0)
	Fusion.estimateBounds( offset, size, imgSizes, models, dimensionality );
			
	// for output
	final ImgFactory< T > f = new ImagePlusImgFactory< T >();
	// the composite
	final ImageStack stack = new ImageStack( size[ 0 ], size[ 1 ] );

	for ( int t = 1; t <= numImages; ++t )
	{
		for ( int c = 1; c <= imp.getNChannels(); ++c )
		{
			final Img<T> out = f.create( size, targetType );
			final Img< FloatType > in = ImageJFunctions.convertFloat( Hyperstack_rearranger.getImageChunk( imp, c, t ) );

			fuseChannel( out, Views.interpolate( Views.extendZero( in ), new NLinearInterpolatorFactory< FloatType >() ), offset, models.get( t - 1 ) );

			try
			{
				final ImagePlus outImp = ((ImagePlusImg<?,?>)out).getImagePlus();
				for ( int z = 1; z <= out.dimension( 2 ); ++z )
					stack.addSlice( imp.getTitle(), outImp.getStack().getProcessor( z ) );
			} 
			catch (ImgLibException e) 
			{
				Log.error( "Output image has no ImageJ type: " + e );
			}				
		}
	}
	
	//convertXYZCT ...
	ImagePlus result = new ImagePlus( "registered " + imp.getTitle(), stack );
	
	// numchannels, z-slices, timepoints (but right now the order is still XYZCT)
	if ( dimensionality == 3 )
	{
		result.setDimensions( size[ 2 ], imp.getNChannels(), imp.getNFrames() );
		result = OverlayFusion.switchZCinXYCZT( result );
		return CompositeImageFixer.makeComposite( result, CompositeImage.COMPOSITE );
	}
	//Log.info( "ch: " + imp.getNChannels() );
	//Log.info( "slices: " + imp.getNSlices() );
	//Log.info( "frames: " + imp.getNFrames() );
	result.setDimensions( imp.getNChannels(), 1, imp.getNFrames() );
	
	if ( imp.getNChannels() > 1 )
		return CompositeImageFixer.makeComposite( result, CompositeImage.COMPOSITE );
	return result;
}
 
Example #5
Source File: Fusion.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 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 );        
}
 
Example #6
Source File: Save3dTIFF.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends RealType<T> & NativeType<T>> boolean exportImage( final RandomAccessibleInterval<T> img, final BoundingBoxGUI bb, final TimePoint tp, final ViewSetup vs, final double min, final double max )
{
	// do nothing in case the image is null
	if ( img == null )
		return false;
	
	// determine min and max
	final float[] minmax;
	
	if ( Double.isNaN( min ) || Double.isNaN( max ) )
		minmax = FusionHelper.minMax( img );
	else
		minmax = new float[]{ (float)min, (float)max };

	ImagePlus imp = null;
	
	if ( img instanceof ImagePlusImg )
		try { imp = ((ImagePlusImg<T, ?>)img).getImagePlus(); } catch (ImgLibException e) {}

	if ( imp == null )
		imp = ImageJFunctions.wrap( img, getImgTitler().getImageTitle( tp, vs ) ).duplicate();

	imp.setTitle( getImgTitler().getImageTitle( tp, vs ) );

	if ( bb != null )
	{
		imp.getCalibration().xOrigin = -(bb.min( 0 ) / bb.getDownSampling());
		imp.getCalibration().yOrigin = -(bb.min( 1 ) / bb.getDownSampling());
		imp.getCalibration().zOrigin = -(bb.min( 2 ) / bb.getDownSampling());
		imp.getCalibration().pixelWidth = imp.getCalibration().pixelHeight = imp.getCalibration().pixelDepth = bb.getDownSampling();
	}
	
	imp.setDimensions( 1, (int)img.dimension( 2 ), 1 );
	
	imp.setDisplayRange( minmax[ 0 ], minmax[ 1 ] );
	
	imp.updateAndDraw();

	final String fileName;
	
	if ( !getImgTitler().getImageTitle( tp, vs ).endsWith( ".tif" ) )
		fileName = new File( path, getImgTitler().getImageTitle( tp, vs ) + ".tif" ).getAbsolutePath();
	else
		fileName = new File( path, getImgTitler().getImageTitle( tp, vs ) ).getAbsolutePath();
	
	if ( compress )
	{
		IOFunctions.println( new Date( System.currentTimeMillis() ) + ": Saving file " + fileName + ".zip" );
		return new FileSaver( imp ).saveAsZip( fileName );
	}
	else
	{
		IOFunctions.println( new Date( System.currentTimeMillis() ) + ": Saving file " + fileName );
		return new FileSaver( imp ).saveAsTiffStack( fileName );
	}
}