Skip to content, Skip to search

Changes

Gauss Package ImgLib2

3,870 bytes added, 24 January
Use HTTPS links where feasible
{{Infobox Plugin
| software = Fiji/ImgLib2
| name = Gaussian Convolution
| author = Stephan Preibisch
| maintainer = Stephan Preibisch
| source = [httphttps://fiji.sc/cgi-bingit/gitweb.cgi?p=imglib.git;a=tree;f=imglib2algorithms/algorithmscore/src/main/java/net/imglib2/algorithm/gauss;hb=refs/heads/master]
| released = 20 December 2011
| latest version = 20 December 2011
<source lang="java">
import net.imglib2.algorithm.gauss.Gauss;
 
// the source
final Img< T > img = ...
<source lang="java">
// compute with float precision, but on T using an OutOfBoundsMirrorFactory periodic (FFT-like) strategy where at the "mirror"end of// lies behind each dimension the last pixel, i.e. all border pixels are there twiceimage simply starts again
final Img< T > convolved = Gauss.inFloat( sigma, img,
new OutOfBoundsMirrorFactoryOutOfBoundsPeriodicFactory<FloatType, RandomAccessibleInterval<FloatType>>( Boundary) ) );</source> === Computing gaussian convolutions on RandomAccessibles with Intervals === For more advanced use of the Gaussian convolution, it accepts RandomAccessibles as input and output, too. In this way the developer has complete freedom which area to convolve and where to write the result. The input and output can be any RandomAccessible, i.e. also any kind of transformed View or Type. To perform a Gaussian convolution on RandomAccessible you need to specify more input variables:* the '''sigma''' in each dimension* the '''input RandomAccessible''' (has to cover all the area required for the convolution, you might need to use Views.extend( ...DOUBLE ) if it is too small* the '''Interval''' in which the Randomaccessible should be convolved. Note that more pixels around this area are required to perform the convolution, see implementation* the '''output RandomAccessible''' specifies the target, i.e. where the result will be written to (can be the same as the input) * the '''Location in the output RandomAccessible''' defines where the content will be inserted* an '''ImageFactory''' for creating the temporary images, it has to be of the type in which the computation is performed. If it is inFloat, it will require an ImgFactory<FloatType>, inDouble will require ImgFactory<DoubleType> and inNumericType needs an ImageFactory<T> An example on how to call the most generic version of the Gaussian convolution can be found below in the examples section. === Some examples ===<source lang="java">// create a new, empty 2-d imagefinal ArrayImgFactory<FloatType> factory = new ArrayImgFactory<FloatType>();final Img<FloatType> img = factory.create( new int[]{ 512, 256 }, new FloatType() ); // fill it with some funny content int i = 0;for ( final FloatType f : img ) f.set( i++ ); for ( final FloatType f : img ) if ( i++ % 7 == 0 || i % 8 == 0 ) f.set( f.get() * 1.5f ); // show the input ImageJFunctions.show( img ); // define the 2-d sigmasfinal double[] sigma = new double[] { 2, 0.75 }; // compute a gauss convolution with double precisionImageJFunctions.show( Gauss.inDouble( sigma, img ) );  // compute a Gaussian convolution in-place in a small Interval Gauss.inFloat( sigma, Views.extendMirrorSingle( img ), new FinalInterval( new long[]{300,150}, new long[]{ 400,200 } ), img, new Location( new long[] {300,150} ), img.factory() ); // show the resultImageJFunctions.show( img );
</source>
For more advanced use Another nice example of the generality of the gaussian convolution is the '''Game of Death 2''' which uses Gaussian convolution, it accepts RandomAccessibles as input and output, tooto simulate diffusion of different species of viruses. It redefines the add() operator of our new NumericType called '''LifeForm''' which can then be used to start the simulation. The '''Game of Death 2''' is included in the imglib repository. In this way
== Implementation ==
 
The first (obvious) core idea of the implementation is to break down the convolution into one-dimensional convolutions along each dimension. In order to properly convolve all input data, a continuously decreasing area has to be convolved when going along each dimension (see figure). This is necessary as the input for dimension d+1 has to be convolved in dimension d in order to produce the correct result.
 
The second core idea is to not iterate over the output but over the input to save operations and access the input as little as possible as it might be an expensive operation (page cell container, renderer, ...). Furthermore, the symmetry of the gauss kernel allows to skip almost half of all operations as each input pixel contributes left and right of itself with with the same weight. This, however, includes a more complicated logic, special cases at the beginning and end of each line, as well as different operations if the kernel is larger than the convolved image.
 
For more details please refer to the source code for now, it is linked on top of this page.
 
[[Image:Gauss.png|780px|Visualizes the offsets and sizes required to perform an n-dimensional gaussian convolution]]
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
12,248
edits