Skip to content, Skip to search

Changes

ImgLib2 Examples

322 bytes removed, 14:21, 9 August 2014
Use GitHub template for GitHub links
Internally, we use a compatibility '''Img''' to represent the data which is as fast as ImageJ but in the case of higher dimensionality (>2d) is slower than ImgLib2 can do with the '''ArrayImg'''. Furthermore you are limited in dimensionality (2d-5d), in the type of data ('''UnsignedByteType''', '''UnsignedShortType''', '''FloatType''' and '''ARGBType''') and maximal size of each 2d-plane (max. 46000x46000).
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example1a.java |label=Example1a.java]}}'''
<source lang="java">
import ij.ImageJ;
'''Important''': it does not matter which type of '''Img''' you use to hold the data as we will use '''Iterators''' and '''RandomAccesses''' to access the image content. It might be, however, important if you work on two '''Img''' at the same time using '''Iterators''', see Example2.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example1b.java |label=Example1b.java]}}'''
<source lang="java">
import ij.ImageJ;
Once you have one instance of an '''Img''', it is very easy to create another one using the same '''Type''' and '''ImgFactory''', even if it has a different size. Note that the call '''img.firstElement()''' returns the first pixel of any '''Iterable''', e.g. an '''Img'''.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example1c.java |label=Example1c.java]}}'''
<source lang="java">
import ij.ImageJ;
''Shows the original image, the View of an interval, as well as the by 90 degree rotated version of the view. Note that only the original image in kept in memory, both Views are completely virtual.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example1d.java |label=Example1d.java]}}'''
<source lang="java">
import ij.ImageJ;
'''< T extends Type< T > >''' basically means that '''T''' can be anything that extends '''Type'''. These can be final implementations such as '''FloatType''' or also intermediate interfaces such as '''RealType'''. This, however, also means that in the method body only operations supported by '''Type''' will be available. Note that the method returns a '''T''', which also means that in the constructor from which we call method it will also return an '''Img<FloatType>''' as we provide it with one.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example2a.java |label=Example2a.java]}}'''
<source lang="java">
import ij.ImageJ;
<span style="color:#FF0000">The correct code for the copy-method (in '''copyImageCorrect''') requires the use of a '''RandomAccess'''.</span> We use a '''Cursor''' to iterate over all pixels of the input and a '''RandomAccess''' which we set to the same location the output. Note that the ''setPosition()'' call of the '''RandomAccess''' directly takes the '''Cursor''' as input, which is possible because '''Cursor''' implements '''Localizable'''. Please also note that we use a '''LocalizingCursor''' instead of a normal '''Cursor''' because we need the location of the '''Cursor''' at every pixel.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example2b.java |label=Example2b.java]}}'''
<source lang="java">
import ij.ImageJ;
As the ''target'' needs to be an '''IterableInterval''', it is more confined. This, however does not necessarily mean that it can only be an '''Img''' or a '''View''' that is not infinite. It simply means it has to be something that is iterable and not infinite, which for example also applies to sparse data (e.g. a list of locations and their values).
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example2c.java |label=Example2c.java]}}'''
<source lang="java">
import ij.ImageJ;
==== Example 3a - Variation 1 ====
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example3a1.java |label=Example3a1.java]}}'''
<source lang="java">
Note that this example works just the same way if the input is not an '''Img''', but for example just a standard Java '''ArrayList'''.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example3a2.java |label=Example3a2.java]}}'''
<source lang="java">
import java.util.ArrayList;
If we want to compute the location of the minimal and maximal pixel value, an '''Iterator''' will not be sufficient as we need location information. Instead the location search will demand an '''IterableInterval''' as input data which can create '''Cursors'''. Apart from that, the algorithm looks quite similar. Note that we do not use a '''LocalizingCursor''' but only a '''Cursor''' the location happens only when a new maximal or minimal value has been found while iterating the data.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example3a3.java |label=Example3a3.java]}}'''
<source lang="java">
import net.imglib2.Cursor;
In a very similar way one can compute the average intensity for image data. Note that we restrict the '''Type''' of data to '''RealType'''. In theory, we could use '''NumericType''' as it offers the possibility to ''add'' up values. However, we cannot ensure that '''NumericType''' provided is capable of adding up millions of pixels without overflow. And even if we would ask for a second '''NumericType''' that is capable of adding values up, it might still have numerical instabilities. ''Note that actually every Java native type has those instabilities''. Therefore we use the '''RealSum''' class that offers correct addition of even very large amounts of pixels. As this implementation is only available for double values, we restrict the method here to '''RealType'''.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example3b.java |label=Example3b.java]}}'''
<source lang="java">
import io.scif.img.ImgIOException;
''Shows the result of example 4a for the (a) two-dimensional, (b) three-dimensional and (c) four-dimensional case. The image series in (c) represents a movie of a three-dimensional rendering. The images of (b) and (c) were rendered using the ImageJ 3d Viewer.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example4a.java |label=Example4a.java]}}'''
<source lang="java">
import ij.ImageJ;
''Shows the result of the detection of local minima after the Gaussian blurring. (a) depicts the input image, (b) the blurred version (sigma=1) and (c) all local mimina drawn as circles with radius 1.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example4b.java |label=Example4b.java]}}'''
<source lang="java">
import ij.ImageJ;
''Illustrates the effect of various OutOfBoundsStrategies. (a) shows out of bounds with a constant value, (b) shows a mirroring strategy, (c) shows the periodic strategy, and (d) shows a strategy that uses random values.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example5.java |label=Example5.java]}}'''
<source lang="java">
import ij.ImageJ;
Here, we simply apply a Gaussian convolution with a sigma of 8. Note that it could be applied in-place as well when calling ''Gauss.inFloatInPlace( ... )''. The Gaussian convolution uses by default the '''OutOfBoundsMirrorStrategy'''.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example6a1.java |label=Example6a1.java]}}'''
<source lang="java">
import ij.ImageJ;
Here we use an '''OutOfBoundsStrategyConstantValue''' instead. It results in continuously darker borders as the zero-values from outside of the image are used in the convolution. Note that the computation is done in-place here. However, we still need to provide an '''ImgFactory''' as the Gaussian convolution needs to create temporary image(s) - except for the one-dimensional case.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example6a2.java |label=Example6a2.java]}}'''
<source lang="java">
import ij.ImageJ;
Note: if you wanted, you could force him to use an '''OutOfBoundsStrategy''' directly outside of the '''Interval'''. For that you would have to create an '''RandomAccessibleInterval''' on the '''Img''', extend it by an '''OutOfBoundsStrategy''' and give this as input to the Gaussian convolution.
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example6a3.java |label=Example6a3.java]}}'''
<source lang="java">
import ij.ImageJ;
Specifically, we apply 1-dimensional Gaussian convolution in 30-pixel wide stripes using a sigma of 16. ''Note that whenever you request an ''HyperSlice'' for a certain dimension, you will get back a '''View''' that contains all dimensions <span style="color:#FF0000">but</span> this one.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example6a4.java |label=Example6a4.java]}}'''
<source lang="java">
import ij.ImageJ;
<span style="color:#FF0000">''Important: This source code is only GPLv2!''</span>
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example6b.java |label=Example6b.java]}}'''
<source lang="java">
/**
<span style="color:#FF0000">''Important: This source code is only GPLv2!''</span>
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example6c.java |label=Example6c.java]}}'''
<source lang="java">
/**
''Shows the result for three different interpolators when magnifying a small part of the image by 10x. The nearest neighbor interpolation is computed fastest and is the most versatile as it requires no computation but just a lookout. The result is, however, very pixelated. The linear interpolation produces reasonable results and computes quite fast. The Lanczos interpolation shows visually most pleasing results but also introduces slight artifacts in the background.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example7.java |label=Example7.java]}}'''
<source lang="java">
import ij.ImageJ;
''On the left hand side it shows nearest-neighbor rendered random sparse data as created in example 8a. The right hand side shows the result of a Gaussian convolution, run directly on the virtual RandomAccessibleInterval.''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example8a.java |label=Example8a.java]}}'''
<source lang="java">
import ij.ImageJ;
''Shows the result of sparse sampling of an existing image using a varying number of random samples. The upper panel shows the rendering using nearest neighbor interpolation, the lower panel uses an interpolated, distance-weighted value of the k nearest neighbors relative to each sampled location (i.e. each pixel).''
'''[https://github.com/{{GitHub|org=imglib/|repo=imglib-tutorials/blob/master/src/main/java/|source=Example8b.java |label=Example8b.java]}}'''
<source lang="java">
import ij.ImageJ;
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
11,834
edits