Image Processing Examples

The Wolfram Language makes it possible to interface with existing image processing libraries efficiently using Wolfram LibraryLink. By interfacing to libraries, you can make use of existing code from within the Wolfram Language. This tutorial walks you through some examples using LibraryLink along with image processing.

LibraryLink provides a way to interface the Wolfram Language to C or C++ code. The interface is low level but efficient, and is targeted to users who wish to either use existing native code or develop C routines for computationally expensive parts of image processing applications. The suggested flow of development is: develop your application using the Wolfram Language, identify bottlenecks, and then implement those routines in C. This tutorial follows this advice: the Wolfram Language implementation will be introduced, then the routine will be implemented in C and compared against the Wolfram Language.

The source for the examples shown in this tutorial is found in the documentation paclet.

Standalone Examples

Two external libraries are used in this code: OpenCV and LibRaw. These libraries are not provided with the Wolfram Language, so in order to compile and run the code, they need to be installed on your machine.

Using Templates to Parametrize on Type

Since Wolfram Language images support different types ("Bit", "Byte", "Bit16", "Real32", and "Real"), templates can be used to both simplify your code and make it more maintainable. In color_negate, you can parametrize on the type and avoid reimplementing the same function multiple times, as shown below.

Restricting Accepted Types

In some cases, the algorithm only works on certain data types or it becomes too verbose to convert to the different data types from within C. In those cases, you need to convert the image types to the ones supported by the C code. This can be done automatically by the LibraryFunction call. Consider the special function defined below.

Sharing Images

Sharing image references between the Wolfram Language kernel and your C code can be an important optimization, since it decreases memory usage. On the other hand, it does place more code complication in your C code and there is a risk of memory leaks.

External Library Examples

Two external libraries are used in this code: OpenCV and LibRaw. These libraries are not provided with the Wolfram Language, so in order to compile and run the code, they need to be installed on your machine.

OpenCV Example

Following is an example that implements a morphological dilation operation using OpenCV library available at http://opencv.org. Function opencv_dilate takes two arguments: a grayscale 2D image and a radius that defines a structuring element in the form of a square . Since OpenCV does not support 64-bit floating point images, IPL_DEPTH_32F is used when the input image is of the type MImage_Type_Real.

Importing Raw Images

LibRaw (http://www.libraw.org) is a library for reading RAW files obtained from digital photo cameras (CRW/CR2, NEF, RAF, DNG, and others). Here is how to use it to import raw images into the Wolfram Language.

The function read_raw_image shown below takes a path to a file and returns an image expression.