Skip to content, Skip to search

Changes

ImgLib2 Examples

98 bytes removed, 10:36, 27 November 2013
no edit summary
String path = file.getAbsolutePath();
// create the ImgOpener and ImgOptions
ImgOpener imgOpener = new ImgOpener();
ImgOptions imgOptions = new ImgOptions();
// open with ImgOpener as an ArrayImg
imgOptions.setImgModes( ImgMode.ARRAY ); Img< T > image = imgOpener.openImg( path, imgOptions );
// display it via ImgLib using ImageJ
ImageJFunctions.show( image );
// create the ImgOptions. This gives us configuration control over how // the ImgOpener will open with ImgOpener as a CellImg with FloatTypeits datasets. ImgOptions imgOptions = new ImgOptions();  // it ImgOpener will be opened as float independent of the use its own heuristic for determining what Img type of the imageto // to enforce to open it as FloatTypeuse, but we can encourage specific types through our ImgOptions. // CellImgs dynamically load image regions and are useful when an instance of FloatType has to be passed alongimage // won't fit in memory
imgOptions.setImgModes( ImgMode.CELL );
  // open with ImgOpener as a CellImg Img< FloatType > imageFloat imageCell = imgOpener.openImg( path, new FloatType(), imgOptions );
// display it via ImgLib using ImageJ
ImageJFunctions.show( imageFloat imageCell );
}
public Example1d() throws ImgIOException
{
// open file as floatwith ImgOpener
Img< FloatType > img =
new ImgOpener().openImg( "DrosophilaWing.tif", new FloatType() );
ImageJFunctions.show( img );
// use a View to define an interval (min and max coordinate, includinginclusive) to display
RandomAccessibleInterval< FloatType > view =
Views.interval( img, new long[] { 200, 200 }, new long[]{ 500, 350 } );
}
}
 
</source>
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.Cursor;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.real.FloatType;
public Example2a() throws ImgIOException
{
// open with ImgOpener using an ArrayImgFactoryas a FloatType
Img< FloatType > img = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// copy the image, as it is a generic method it also works with FloatType
public Example2b() throws ImgIOException
{
// open with ImgOpener . In addition to using ImgOptions, we can directly // pass an ArrayImgFactoryImgFactory to the ImgOpener. This bypasses the Img selection // heuristic and allows custom ImgFactory implementations to be used
Img< FloatType > img = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// copy the image into a CellImg with a cellsize of 20x20
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.Cursor;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.real.FloatType;
public Example2c() throws ImgIOException
{
// open with ImgOpener using an ArrayImgFactoryas a float Img< FloatType > img = new ImgOpener().openImg( "DrosophilaWing.tif", new ArrayImgFactory< FloatType >(), new FloatType() );
// copy & display an image
'''[https://github.com/imagej/imglib/blob/master/examples/src/main/java/Example3b.java Example3b.java]'''
<source lang="java">
import net.imglib2.img.Img;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.img.array.ArrayImgFactoryImg;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
ImgIOException
{
// open with ImgOpener using an ArrayImgFactory final Img< T > img = new ImgOpener().openImg( "DrosophilaWing.tif", new ArrayImgFactory< T >() );
// compute average of the image
}
}
 
</source>
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.Cursor;
import net.imglib2.Interval;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.NativeType;
import net.imglib2.type.logic.BitType;
{
public < T extends RealType< T > & NativeType< T > > Example4b()
throws ImgIOException, IncompatibleTypeException
{
// open with ImgOpener
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.ExtendedRandomAccessibleInterval;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccessible;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.outofbounds.OutOfBoundsConstantValueFactory;
import net.imglib2.type.numeric.real.FloatType;
public Example5() throws ImgIOException
{
// open with ImgOpener using an ArrayImgFactoryas a FloatType
Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWingSmall.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// create an infinite view where all values outside of the Interval are 0
Views.extendValue( image, new FloatType( 128 ) );
// create an infinite view where all outside valuesare values are random in a range of 0-255
RandomAccessible< FloatType> infinite3 = Views.extendRandom( image, 0, 255 );
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.algorithm.gauss.Gauss;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.numeric.real.FloatType;
public Example6a1() throws ImgIOException
{
// open with ImgOpener using an ArrayImgFactoryas a FloatType
Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// perform gaussian convolution with float precision
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.RandomAccessible;
import net.imglib2.algorithm.gauss3.Gauss3;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.view.Views;
public Example6a2() throws ImgIOException, IncompatibleTypeException
{
// open with ImgOpener using an ArrayImgFactoryas a FloatType
Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// perform gaussian convolution with float precision
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccessible;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Intervals;
public Example6a3() throws ImgIOException, IncompatibleTypeException
{
// open with ImgOpener using an ArrayImgFactoryas a FloatType
Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// perform gaussian convolution with float precision
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.gauss3.Gauss3;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.view.Views;
public Example6a4() throws ImgIOException, IncompatibleTypeException
{
// open with ImgOpener using an ArrayImgFactoryas a FloatType
Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// perform all (n-1)-dimensional gaussian (in this case it means 1d) on
*/
import ij.ImageJ;
import mpicbgio.utilscif.RealSumimg.ImgIOException;import io.scif.img.ImgOpener;
import net.imglib2.algorithm.fft.FourierConvolution;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.RealSum;
/**
{
// open with ImgOpener using an ArrayImgFactory
final Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWing.tif", new ArrayImgFactory< FloatType >(), new FloatType() ); final Img< FloatType > kernel = new ImgOpener().openImg( "kernelGauss.tif", new ArrayImgFactory< FloatType >(), new FloatType() );
// normalize the kernel, otherwise we add energy to the image
}
public static void main( final String[] args ) throws ImgIOException, IncompatibleTypeException
{
// open an ImageJ window
}
}
 
</source>
*/
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.algorithm.fft.FourierConvolution;
import net.imglib2.algorithm.fft.FourierTransform;
import net.imglib2.algorithm.fft.InverseFourierTransform;
import net.imglib2.displayconverter.ComplexImaginaryFloatConverter;import net.imglib2.displayconverter.ComplexPhaseFloatConverter;import net.imglib2.displayconverter.ComplexRealFloatConverter;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import ionet.scifimglib2.imgtype.ImgIOException;import ionumeric.scif.img.ImgOpenerRealType;
import net.imglib2.type.numeric.complex.ComplexFloatType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.RealSum;
/**
public Example6c() throws ImgIOException, IncompatibleTypeException
{
// open with ImgOpener using an ArrayImgFactoryas FloatTypes final Img< FloatType > image = new ImgOpener().openImg( "DrosophilaWing.tif", new ArrayImgFactory< FloatType >(), new FloatType() ); final Img< FloatType > template = new ImgOpener().openImg( "WingTemplate.tif", new ArrayImgFactory< FloatType >(), new FloatType() );
// display image and template
// normalize the inverse template
Example6b.norm( templateInverse );
// compute fourier convolution of the inverse template and the image and display it
ImageJFunctions.show( FourierConvolution.convolve( image, templateInverse ) );
}
/**
* Computes the sum of all pixels in an iterable using RealSum
*
* @param iterable - the image data
* @return - the sum of values
*/
public static < T extends RealType< T > > double sumImage( final Iterable< T > iterable )
{
final RealSum sum = new RealSum();
for ( final T type : iterable ) sum.add( type.getRealDouble() );  return sum.getSum(); }  /** * Norms all image values so that their sum is 1 * * @param iterable - the image data */ public static void norm( final Iterable< FloatType > iterable ) { final double sum = sumImage( iterable );  for ( final FloatType type : iterable ) type.setReal( type.get() / sum ); }  public static void main( final String[] args ) throws ImgIOException, IncompatibleTypeException
{
// open an ImageJ window
// open with ImgOpener using an ArrayImgFactory
Img< FloatType > img = new ImgOpener().openImg( "DrosophilaWing.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
ImageJFunctions.show( img );
<source lang="java">
import ij.ImageJ;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import java.util.Random;
import net.imglib2.collection.RealPointSampleList;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.interpolation.neighborsearch.InverseDistanceWeightingInterpolatorFactory;
import net.imglib2.interpolation.neighborsearch.NearestNeighborInterpolatorFactory;
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory;
import io.scif.img.ImgIOException;
import io.scif.img.ImgOpener;
import net.imglib2.neighborsearch.KNearestNeighborSearch;
import net.imglib2.neighborsearch.KNearestNeighborSearchOnKDTree;
public Example8b() throws ImgIOException
{
// open with ImgOpener using an ArrayImgFactorya FloatType
Img< FloatType > img = new ImgOpener().openImg( "DrosophilaWingSmall.tif",
new ArrayImgFactory< FloatType >(), new FloatType() );
// show the image
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
735
edits