How Do I Do Basic Image Processing in
Mathematica
This how-to demonstrates how Mathematica can be used to do
image analysis tasks. In addition to many common operations, the
ListInterpolation
function can be used to do new and exciting manipulations and analyses.
Note: To save bandwidth, the original imported picture is
not
shown in every subsection. Click here to see it.
Importing, Displaying, and Exporting Graphics
Files
The Import
command provides Mathematica users with an efficient and intuitive way to read
files in most standard graphics formats, including bitmap, JPEG, GIF, TIFF, and numerous
platform-specific formats. In many cases, Mathematica even automatically detects
the correct graphics format; otherwise, users can specify it themselves. Note that one has
to use double backslashes in the file path. To interactively select the file, use the
GetFilePath command in the Input menu.
This command opens a standard file dialog
and returns a correctly formatted path to the file at the current text cursor position.
![[Graphics:Images/index_gr_1.gif]](Images/index_gr_1.gif)
The graphic can now be displayed using
Show.
![[Graphics:Images/index_gr_2.gif]](Images/index_gr_2.gif)
![[Graphics:Images/index_gr_3.gif]](Images/index_gr_3.gif)
![[Graphics:Images/index_gr_4.gif]](Images/index_gr_4.gif)
It can also be exported in a different format. For a complete list of import and export
formats, consult the documentation for Import
and Export.
![[Graphics:Images/index_gr_5.gif]](Images/index_gr_5.gif)
![[Graphics:Images/index_gr_6.gif]](Images/index_gr_6.gif)
![[Graphics:Images/index_gr_7.gif]](Images/index_gr_7.gif)
![[Graphics:Images/index_gr_8.gif]](Images/index_gr_8.gif)
![[Graphics:Images/index_gr_9.gif]](Images/index_gr_9.gif)
![[Graphics:Images/index_gr_10.gif]](Images/index_gr_10.gif)
Return to Top
Color Conversions
ColorOutput
converts Mathematica
graphics between different color models. The three standard models are RGBColor, CMYKColor, and GrayLevel. Note that ColorOutput changes the image representation, not just the rendering.
![[Graphics:Images/index_gr_11.gif]](Images/index_gr_11.gif)
![[Graphics:Images/index_gr_12.gif]](Images/index_gr_12.gif)
Return to Top
Accessing Image Data
Mathematica handles many different kinds of objects: mathematical formulas,
lists, and graphics, to name but a few. Although objects often look very different,
Mathematica represents all in one uniform way; they are all expressions.
The imported graphics files are, for example, stored as a
Graphics object.
The object f in an expression f[x, y, ... ]
is known as the head of
the expression. You can extract it using Head.
Particularly when you write programs in Mathematica, you will often
want to test the head of an expression to find out what kind of thing the expression is.
Both the imported JPEG and TIFF files are, of course, Graphics objects.
![[Graphics:Images/index_gr_13.gif]](Images/index_gr_13.gif)
![[Graphics:Images/index_gr_14.gif]](Images/index_gr_14.gif)
![[Graphics:Images/index_gr_15.gif]](Images/index_gr_15.gif)
![[Graphics:Images/index_gr_16.gif]](Images/index_gr_16.gif)
The easiest way to access the pixel data is to change the head of the
expression to a Mathematica list and extract the array of pixel
values. The following command replaces the Graphics head with a List head. Explaining the mechanism is beyond the
scope of this document. If you are interested, consult the documentation
for Replace.
![[Graphics:Images/index_gr_17.gif]](Images/index_gr_17.gif)
The list contains an array of pixel values and some supplemental information on image size and color model. Short gives an overview.
![[Graphics:Images/index_gr_18.gif]](Images/index_gr_18.gif)
![[Graphics:Images/index_gr_19.gif]](Images/index_gr_19.gif)
The pixel values are always the first element of the data structure. They can now be
treated like any other list in Mathematica. An RGB image, for example, is represented
as an array of {r, g, b} triples.
The next line extracts the array of pixel values. To learn more about
matrix manipulation, consult Section
1.8.4 of The Mathematica Book or How Do I Create and
Manipulate Matrices?
![[Graphics:Images/index_gr_22.gif]](Images/index_gr_22.gif)
As you can see, the color values are now stored in a 302x291x3 array.
![[Graphics:Images/index_gr_23.gif]](Images/index_gr_23.gif)
![[Graphics:Images/index_gr_24.gif]](Images/index_gr_24.gif)
For example, here is a ListDensityPlot of the red channel of the
image. All is a new function in Mathematica 4 that simply instructs Mathematica to take all elements in the respective dimension of the array.
![[Graphics:Images/index_gr_25.gif]](Images/index_gr_25.gif)
![[Graphics:Images/index_gr_26.gif]](Images/index_gr_26.gif)
Return to Top
Simple Math
All Mathematica functions that can be applied to lists can also work on the
image data. For example, the following two lines are two different ways of inverting
a grayscale image.
![[Graphics:Images/index_gr_27.gif]](Images/index_gr_27.gif)
![[Graphics:Images/index_gr_29.gif]](Images/index_gr_29.gif)
![[Graphics:Images/index_gr_30.gif]](Images/index_gr_30.gif)
The same procedure works for color images, too. Using
Apply
is a shortcut to convert all three color values in one step. Also,
RGBColor expects its values to be between 0 and 1,
so pixelvalues gets divided by 256.
![[Graphics:Images/index_gr_31.gif]](Images/index_gr_31.gif)
![[Graphics:Images/index_gr_32.gif]](Images/index_gr_32.gif)
Return to Top
Histograms
Although Mathematica's histogram functions in
Graphics`Graphics` work only for small data sets, histograms
can be created easily by using BinCounts and
ListPlot.
BinCounts is part of the Statistics`DataManipulation` package that ships with Mathematica.
Read in the package.
![[Graphics: Images/index_gr_33.gif]]( Images/index_gr_33.gif)
Count the occurrences of pixel values in the red channel of the image.
![[Graphics:Images/index_gr_34.gif]](Images/index_gr_34.gif)
![[Graphics:Images/index_gr_35.gif]](Images/index_gr_35.gif)
![[Graphics:Images/index_gr_36.gif]](Images/index_gr_36.gif)
![[Graphics:Images/index_gr_37.gif]](Images/index_gr_37.gif)
Return to Top
Histogram Operations
Because the image has been converted into a Mathematica list,
any Mathematica function can be applied to parts of the list. This
makes histogram operations trivial. For example, add a reddish tint by
taking the square root of the red channel. Note that the color values for
RasterArray have to be scaled between 0 and 1.
The #'s in the code are pure functions. To learn more about them,
see The Mathematica Book.
In the following picture, the red channel is decimated by 70 percent.
![[Graphics: Images/index_gr_38.gif]]( Images/index_gr_38.gif)
![[Graphics: Images/index_gr_39.gif]]( Images/index_gr_39.gif)
The next picture shows the effect of taking the square root of the color values
in the red channel. Because the values are scaled between 0 and 1, the image has a reddish tint.
![[Graphics:Images/index_gr_40.gif]](Images/index_gr_40.gif)
![[Graphics:Images/index_gr_41.gif]](Images/index_gr_41.gif)
Return to Top
Selecting Pieces of Images
Slices
Once the pixel information is converted into a Mathematica list, it can be
operated on like any other Mathematica list of lists. In the following picture,
the three color channels are extracted by slicing the 266 x 276 x 3 data set along the
z axis.
Click here to learn more about selecting pieces of lists.
![[Graphics:Images/index_gr_42.gif]](Images/index_gr_42.gif)
![[Graphics:Images/index_gr_43.gif]](Images/index_gr_43.gif)
Or, take a slice through row 200 of the red channel.
![[Graphics:Images/index_gr_44.gif]](Images/index_gr_44.gif)
![[Graphics:Images/index_gr_45.gif]](Images/index_gr_45.gif)
To visualize this slice, set all the pixels along this line to white. Copying the array into a new variable first preserves the original image.
![[Graphics:Images/index_gr_46.gif]](Images/index_gr_46.gif)
![[Graphics:Images/index_gr_47.gif]](Images/index_gr_47.gif)
![[Graphics:Images/index_gr_48.gif]](Images/index_gr_48.gif)
![[Graphics:Images/index_gr_49.gif]](Images/index_gr_49.gif)
The easiest way to plot multiple slices at once is to use the
MultipleListPlot
standard package.
![[Graphics:Images/index_gr_50.gif]](Images/index_gr_50.gif)
![[Graphics:Images/index_gr_51.gif]](Images/index_gr_51.gif)
![[Graphics:Images/index_gr_52.gif]](Images/index_gr_52.gif)
Because ListDensityPlot and Raster work only with two-dimensional arrays, you
must use
RasterArray to put markers on the full-color
image.
![[Graphics:Images/index_gr_53.gif]](Images/index_gr_53.gif)
![[Graphics:Images/index_gr_54.gif]](Images/index_gr_54.gif)
![[Graphics:Images/index_gr_55.gif]](Images/index_gr_55.gif)
![[Graphics:Images/index_gr_56.gif]](Images/index_gr_56.gif)
Return to Top
Area Selection
Selecting areas of a graphic is also straightforward. To find the
values for Take,
click the graphic and move the mouse while holding down the
CONTROL
key. The pointer location will be displayed in the lower-left corner of the window.
To select multiple points, click all that interest you; then copy, unselect the
graphic, and paste. Mathematica will return a list of all the locations you
selected. For more information on how to select coordinates in Mathematica
graphics, see How Do I Extract
Coordinates from a Plot?
This line of code selects a 51-by-31-pixel rectangle of the picture.
![[Graphics:Images/index_gr_57.gif]](Images/index_gr_57.gif)
![[Graphics:Images/index_gr_58.gif]](Images/index_gr_58.gif)
![[Graphics:Images/index_gr_59.gif]](Images/index_gr_59.gif)
![[Graphics:Images/index_gr_60.gif]](Images/index_gr_60.gif)
![[Graphics:Images/index_gr_61.gif]](Images/index_gr_61.gif)
The next line sets the red channel to .
![[Graphics:Images/index_gr_63.gif]](Images/index_gr_63.gif)
![[Graphics:Images/index_gr_64.gif]](Images/index_gr_64.gif)
![[Graphics:Images/index_gr_65.gif]](Images/index_gr_65.gif)
![[Graphics:Images/index_gr_66.gif]](Images/index_gr_66.gif)
Putting the results back into the original image is also pretty easy
and straightforward. First copy the image into a new variable to preserve the original image.
![[Graphics:Images/index_gr_67.gif]](Images/index_gr_67.gif)
Then replace the corresponding values with the transformed ones.
![[Graphics:Images/index_gr_68.gif]](Images/index_gr_68.gif)
![[Graphics:Images/index_gr_69.gif]](Images/index_gr_69.gif)
![[Graphics:Images/index_gr_70.gif]](Images/index_gr_70.gif)
Return to Top
Thresholding
To select pixels based on some property, simply use If with the appropriate
cutoff value. For example, the following line selects all pixels with an R value greater
than 0.5x256.
![[Graphics:Images/index_gr_71.gif]](Images/index_gr_71.gif)
![[Graphics:Images/index_gr_72.gif]](Images/index_gr_72.gif)
For more information on selecting parts of lists, see How Do I Create and Manipulate Matrices?
Return to Top
Convolution and Correlation
The new Mathematica 4 functions
ListConvolve
and ListCorrelate
make a number of advanced image-processing tasks easy to do. The following graphic
shows the effect of applying a Sobel edge filter to the image by convolving the image
with two 3x3 kernels.
![[Graphics:Images/index_gr_73.gif]](Images/index_gr_73.gif)
![[Graphics:Images/index_gr_74.gif]](Images/index_gr_74.gif)
Return to Top
List Interpolation and Data Recovery
An interesting feature of Mathematica is its ability to rapidly calculate list
interpolations of any degree in arbitrary dimensions. This feature opens up new ways
to manipulate images and recover information lost by the discretizing of data.
The Doghill Mountains
Before we do anything useful with list interpolation, let's try
something fun. Perform a first-order list interpolation over the data set, and
plot the color values in 3D. Using the new real-time 3D graphics, then rotate
the graphic and see the image pop in and out of view.
![[Graphics:Images/index_gr_75.gif]](Images/index_gr_75.gif)
![[Graphics:Images/index_gr_76.gif]](Images/index_gr_76.gif)
![[Graphics:Images/index_gr_77.gif]](Images/index_gr_77.gif)
![[Graphics:Images/index_gr_78.gif]](Images/index_gr_78.gif)
![[Graphics:Images/interpolation.gif]](Images/interpolation.gif)
Data Recovery
Let's go back to the small piece of the image selected in "Selecting Pieces of Images."
![[Graphics:Images/index_gr_80.gif]](Images/index_gr_80.gif)
![[Graphics:Images/index_gr_81.gif]](Images/index_gr_81.gif)
![[Graphics:Images/index_gr_82.gif]](Images/index_gr_82.gif)
![[Graphics:Images/index_gr_83.gif]](Images/index_gr_83.gif)
As you can see, the image above consists of only 51x31 data points, so the
resolution is not terribly high. List interpolation provides a convenient way to recover
more features.
The following command creates a list interpolation over the red channel of the
image and scales the resulting function between
0 and 1 in the x and y coordinates.
![[Graphics:Images/index_gr_85.gif]](Images/index_gr_85.gif)
![[Graphics:Images/index_gr_86.gif]](Images/index_gr_86.gif)
![[Graphics:Images/index_gr_87.gif]](Images/index_gr_87.gif)
Mathematical Operations
One can do all kinds of fancy image manipulations and transformations using
ListInterpolation. For example, differentiation finds
changes in color values. Two-dimensional differentiation also creates a nice embossing.
![[Graphics:Images/index_gr_88.gif]](Images/index_gr_88.gif)
![[Graphics:Images/index_gr_89.gif]](Images/index_gr_89.gif)
Return to Top
|