Reading an image with imglib2

by 3/16/2012 04:17:00 PM 1 comments


imglib2 appears to be a very powerful image analysis library. It's targeted at folks who do complicated image processing such as scientific analysis and medical-image analysis. I'm starting to use it for some relatively simple analysis. The learning curve for it is pretty steep. As an example, imglib2 has a types heirachy. In this case, 'types' are classes used to retrieve a value from an image. There's a mere 35 classes and interfaces involved in the type hierachy. Since these types appear everywhere in imglib2, you'll have to grok them inorder to even just get started reading an image. imglib2 provides the ImgOpener class for reading image data. I'm currently trying to use it to read both black and white tiffs as well as the more common jpg and png formats. ImgOpener has a few quirks that I'm going to make mention of. NOTE: my examples are written in Scala, apologies to the Java folks

Read and Ignore Types

val tiff = "blackandwhite.tiff"
val jpg = "color.jpg"

val io = new ImgOpener
val tiffImgPlus = io.openImg(tiff)  
val jpgImgPlus = io.openImg(jpg)
both the tiff and jpg are read in correctly but appear in Scala as ImgPlus[Nothing]. The underlying Img implementation for both are PlanarImg

Read Using Correct Type

In order, to enforce the type, you need to pass the correct instance of the type into the openImg method. The tiff should be read as a ShortType.
val tiff = "blackandwhite.tiff"

import net.imglib2.`type`.numeric.integer.ShortType
val io = new ImgOpener
val tiffImgPlus = io.openImg(tiff, new ArrayImgFactory[ShortType], new ShortType) 
The jpg should be read as an UnsignedByteType
val jpg = "color.jpg"

import net.imglib2.`type`.numeric.integer.UnsignedByteType
val io = new ImgOpener
val jpgImgPlus = io.openImg(tiff, new ArrayImgFactory[UnsignedByteType], new UnsignedByteType) 
When an image is read this way, the underlying Img implementation is an ArrayImg.

Read Using an Incorrect Type

If you specify the wrong factory with out passing in a type, ImgOpener will still go ahead and open the image as a PlanarImg
val tiff = "blackandwhite.tiff"

val io = new ImgOpener
// Should be ShortType but using incorrect ARGBType
val tiffImgPlus = io.openImg(tiff, new ArrayImgFactory[ARGBType])  
Now the underlying image is once again a PlanarImg.

Brian Schlining


Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.


herrsaalfeld said...

Hi Brian,

that's nice examples. We're happy that you have discovered ImgLib2 and keep going with it. We have meanwhile added some examples and documentation to the Fiji Wiki:

The examples are partially translations of an older tutorial on the deprecated ImgLib1 but also cover new aspects of the library. The documentation includes simplified UML diagrams for accessors and accessibles, type hierarchy to come.

Stephan Saalfeld