Java and the Digital Image Processing Application Package wi

Mariusz Jankowski
University of Southern Maine
mjkcc@usm.maine.edu

Abstract

The Digital Image Processing application package, a relatively recent addition to the Mathematica application library, enhances Mathematica with over 160 functions in the area of digital processing of images (see [1]). The introduction of the package supported by significant performance improvements in versions 4.0 and 4.1 have made Mathematica an effective platform for image processing and analysis. Now, thanks to the introduction of J/Link (see [2]), the Mathematica user gains direct access to an extensive native Java imaging functionality found in the Java 2D and Java Advanced Imaging (JAI) application programming interfaces (APIs) and excellent graphical user interface (GUI) design features thanks to Java Swing.

These developments have the potential to profoundly impact the future of desktop image processing in general and within the Mathematica environment in particular. Issues that will be addressed in the presentation include Java imaging functionality, Mathematica and Java image data formats, performance and trade-offs. Examples will be shown of translation between Mathematica and Java image data objects and scripting of Java image operators found in Java 2D and JAI.

Java 2D API

The Java 2D API introduced in JDK 1.2 provides enhanced two-dimensional graphics, text, and imaging capabilities for Java programs through extensions to the Abstract Windowing Toolkit (AWT) (see [3]). This comprehensive rendering package supports line art, text, and images in a flexible, full-featured framework for developing richer user interfaces, sophisticated drawing programs and image editors.

The Java 2D API implements an imaging model that supports the manipulation of fixed-resolution images stored in memory. A new Image class in the java.awt.image package, BufferedImage, can be used to hold and to manipulate image data retrieved from a file, URL or an appropriate Mathematica expression. The class BufferedImage enables you to perform a variety of image-filtering operations, such as blur and sharpen, affine spatial transformations and image resampling/resizing operations.

<< JLink`

<< ImageProcessing`

InstallJava[];

This reads an image using the Digital Image Processing function ImageRead.

img = ImageRead["cameraman.tif"];

The following example demonstrates an image smoothing operation using Java 2D functionality. It also shows the ease with which Mathematica may be used to script a sequence of Java operations.

src = toBufferedImage[img];
ker = 
        JavaNew["java.awt.image.Kernel", 5, 5, 
          Table[0.04, {25}]];
cop = 
        JavaNew["java.awt.image.ConvolveOp", 
          ker];
dst = 
        JavaNew["java.awt.image.BufferedImage", 256, 256, 
          BufferedImage`TYPEUBYTEUGRAY];

This performs the operation and returns a Mathematica expression containing the image data.

cop@filter[src, dst];
img = fromBufferedImage[dst];

The utility functions toBufferedImage and fromBufferedImage convert between Java and Mathematica image data expressions.

Java Advanced Imaging API

The JavaTM Advanced Imaging API enables developers to easily incorporate high-performance, network-enabled, scalable, platform-independent image processing into Java technology and Mathematica-based applications (see [4]).

The Java Advanced Imaging API provides a set of object-oriented interfaces that support a simple, high-level programming model which allows developers to manipulate images easily. Examples of image processing techniques available in JAI range from simple operations such as contrast enhancement, cropping, and scaling to more complex operations such as advanced geometric warping and frequency domain processing. There are approximately 80 operators, many with native support for optimized performance.

Here is a typical sequence of operations using JAI. First we load the JAI interface

jai = JavaNew["javax.media.jai.JAI"]

Here we define the kernel, the desired operation and return the result.

kernel = 
        JavaNew["javax.media.jai.KernelJAI", 5, 5, 
          Table[0.04, {25}]];
pb = 
        JavaNew["java.awt.image.renderable.ParameterBlock"];
pb@addSource[src];
pb@add[kernel];
dst = jai@create["convolve", pb]

The following converts the result to a Java BufferedImage object for subsequent translation to a Mathematica expression using fromBufferedImage.

bi = dst@getAsBufferedImage[]

Most other JAI image operators may be implemented in a similar manner.

BufferedImage and ImageData Objects

The Digital Image Processing package introduced a convenient new Mathematica object called ImageData which encapsulates information about an image including the actual pixel values, the particular organization of the data and the color format. The BufferedImage Java object introduced in JDK 1.2 plays a similar role in the Java environment. Here we show how to translate grayscale image data between the two environments.

This determines the dimensions of the image and instantiates a BufferedImage object of appropriate size and color.

{h, w} = ImageDimensions[img];
bi = 
        JavaNew["java.awt.image.BufferedImage", w, h, 
          BufferedImage`TYPEUBYTEUGRAY];

This populates the BufferedImage with raw data extracted from the Mathematica ImageData expression.

r = bi@getRaster[];
r@
        setPixels[0, 0, w, h, 
          Flatten[Reverse[RawImageData[img]]]];

Alternatively, given a BufferedImage we wish to convert to an ImageData object.

w = bi@getWidth[];
h = bi@getHeight[];
r = bi@getData[];
c = r@getNumBands[];
pix  =  JavaNew["[I",  w*h*c];

The following returns a grayscale ImageData object.

ToGrayLevel[
      Reverse[Partition[r@getPixels[0, 0, w, h, pix], w]]]

Note that BufferedImage objects are fully compatible with JAI and thus may be used as source image data with any of the JAI operators.

Summary

The imaging functionality of Java 2D and JAI APIs was briefly described. Examples were shown of how to smooth an image using convolution operators available in both of the APIs. A translation mechanism between Java and Mathematica image data objects was also demonstrated. J/Link served as the all-important link between the Java and Mathematica environments with profound potential benefits to the image processing professional.

References

[1] URL: http://www.wolfram.com/products/applications/digitalimage/

[2] URL: http://www.wolfram.com/solutions/mathlink/jlink/

[3] URL: http://java.sun.com/products/java-media/2D/

[4] URL: http://java.sun.com/products/java-media/jai/