Open Source Java Library for Image Creation & Processing
A General-purpose, Multidimensional Image Processing API that Create New Images, Modify Existing Images, Handle Sparse Data, Duplicating Existing Image using Free Java API.
What is ImgLib2?
ImgLib2 is a powerful, open-source Java library for multidimensional image processing, designed to simplify complex development tasks. This general-purpose imaging library enables developers to create and manipulate images within their Java applications through a flexible, interface-driven architecture. It supports essential operations like opening and reading images, duplicating images, and generic image data copying, all while handling both numeric and non-numeric data types with ease. Its dimension-independent design allows code to be applied seamlessly across various data structures.
Built for efficiency, ImgLib2 helps developers focus on core algorithms without unnecessary complexity. The library extends beyond traditional image processing to work with sparse data and even non-image formats like RNA sequences. Key features include robust interpolation support, tools for drawing shapes such as spheres, and comprehensive modifying existing images capabilities. This versatility makes it an invaluable resource for scientific computing and advanced image analysis in Java, streamlining projects that require precise and scalable data manipulation.
Getting Started with ImgLib2
The easiest and recommended way to install ImgLib2 is via GitHub.
Install ImgLib2 via GitHub
go get -u github.com/imglib/imglib2.git Creating New Images via Java
The open source Java library ImgLib2 has included support for creating a new image from the scratch with just a couple of lines of Java code. Using the ImgLib2 library, you can create different types of images such as simple images, 3D images, ImgFactory & more. You can also modify images of the existing images with just a couple of lines of code.
How to Create New Images via Java API?
public Example1c()
{
// create the ImgFactory based on cells (cellsize = 5x5x5...x5) that will
// instantiate the Img
final ImgFactory< FloatType > imgFactory = new CellImgFactory<>( new FloatType(), 5 );
// create an 3d-Img with dimensions 20x30x40 (here cellsize is 5x5x5)Ø
final Img< FloatType > img1 = imgFactory.create( 20, 30, 40 );
// create another image with the same size. Note that the input provides the size for the new image as it implements the Interval interface
final Img< FloatType > img2 = imgFactory.create( img1 );
// display both (but they are empty)
ImageJFunctions.show( img1 );
ImageJFunctions.show( img2 );
}
Image Duplication via Java API
The ImgLib2 library has included functionality for image duplication using Java commands. You can easily make a copy of the existing image. You can employ Cursors to achieve this task. You can also use the copy method which is a generic method and the great thing is that it will work on any kind of Type.
How to Duplicate Image via Java API?
public DuplicateImage() throws ImgIOException
{
// open with SCIFIO as a FloatType
Img< FloatType > img = IO.openImgs( "DrosophilaWing.tif", new FloatType() ).get( 0 );
// copy the image, as it is a generic method it also works with FloatType
Img< FloatType > duplicate = copyImage( img );
// display the copy
ImageJFunctions.show( duplicate );
}
// Generic, type-agnostic method to create an identical copy of an Img
public < T extends Type< T > > Img< T > copyImage( final Img< T > input )
{
// create a new Image with the same properties
Img< T > output = input.factory().create( input );
// create a cursor for both images
Cursor< T > cursorInput = input.cursor();
Cursor< T > cursorOutput = output.cursor();
// iterate over the input
while ( cursorInput.hasNext())
{
// move both cursors forward by one pixel
cursorInput.fwd();
cursorOutput.fwd();
// set the value of this pixel of the output image to the same as the input,
// every Type supports T.set( T type )
cursorOutput.get().set( cursorInput.get() );
}
// return the copy
return output;
}
View Images Partly via Java
The free ImgLib2 library enables software developers to display of only some parts of the image inside their apps via a couple of lines of Java code. Views are very powerful and you can use them to display selected parts of the images, display a rotated view, and few other things. Views can be RandomAccessible, Interval, and can therefore be made Iterable.
Sparse Data Management via Java
The free ImgLib2 library gives software developers the capability to work with sparse data using Java code. The library has provided two interpolation schemes for displaying sparse data. Users can compute a value for every location in space by returning either the value of the closest sample or an interpolated, distance-weighted value of the k nearest neighbors to the sampled location.
How to Work with Sparse Data inside Java Apps?
// Working with sparse data
public SparseExample()
{
// the interval in which to create random points
FinalInterval interval = new FinalInterval( new long[] { 375, 200 } );
// create an IterableRealInterval
IterableRealInterval< FloatType > realInterval = createRandomPoints( interval, 250 );
// using nearest neighbor search we will be able to return a value an any position in space
NearestNeighborSearch< FloatType > search =
new NearestNeighborSearchOnKDTree<>(
new KDTree<>( realInterval ) );
// make it into RealRandomAccessible using nearest neighbor search
RealRandomAccessible< FloatType > realRandomAccessible =
Views.interpolate( search, new NearestNeighborSearchInterpolatorFactory< FloatType >() );
// convert it into a RandomAccessible which can be displayed
RandomAccessible< FloatType > randomAccessible = Views.raster( realRandomAccessible );
// set the initial interval as area to view
RandomAccessibleInterval< FloatType > view = Views.interval( randomAccessible, interval );
// display the view
ImageJFunctions.show( view );
// compute a gauss on it
Img< FloatType > convolved = new ArrayImgFactory<>( new FloatType() ).create( interval );
Gauss.inFloat( new double[] { 3, 3 }, view, interval, convolved,
new Point( view.numDimensions() ), convolved.factory() );
// display the view
ImageJFunctions.show( convolved );
}