Open Source Java Library for Image Processing
A General-purpose, Multidimensional Image Processing API that create new images, modify existing images, working with sparse data, duplicating existing image using Free Java API.
The open source ImgLib2 library gives software developers the capability to create and manipulate images inside their Java apps. The library offers an interface-driven design that enables users to use numeric and non-numeric data types with ease inside their own applications.
The ImgLib2 is a general-purpose, multidimensional image processing library that provides support for several important features related to image processing, such as creating new images, modifying existing images, Opening and reading existing images, working with sparse data, duplicating existing images, Generic copying of image data, Drawing a sphere, Interpolation support and many more.
The library is very user-friendly and avoids unnecessary complexities thus developers can concentrate on the essence of the algorithm while developing their projects. The great thing about the library is that it is dimension-independent and allows users to express their code in a way that can be applied to many-dimensional data. The library working is not limited to images only there are examples working on RNA sequences as well.
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.
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.
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
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.
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 );
}