How Do I Do Basic Image Processing in Mathematica?
Author
Wolfram Research, Inc.
Title
How Do I Do Basic Image Processing in Mathematica?
Description
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. Also available in Japanese http://library.wolfram.com/howtos/images/index.ja.html
Category
Educational Materials
Keywords
URL
http://www.notebookarchive.org/2018-10-10r8f3h/
DOI
https://notebookarchive.org/2018-10-10r8f3h
Date Added
2018-10-02
Date Last Modified
2018-10-02
File Size
13.89 megabytes
Supplements
Rights
Redistribution rights reserved




Image Processing in Mathematica 4
Image Processing in Mathematica 4
This how-to demonstrates how Mathematica 4 can be used to do image analysis tasks. In addition to many common operations, the ListInterpolation function can be used to do new and exiting 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
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.
In[]:=
dog=Import["C:\\desk\\40 Demos\\schlapp2.jpg"];
In[]:=
Show[dog]
Out[]=
⁃Graphics⁃
In[]:=
Export["C:\\windows\\desktop\\dog.tif",dog,"TIFF"]
Out[]=
"C:\\windows\\desktop\\dog.tif"
In[]:=
tifdog=Import["C:\\windows\\desktop\\dog.tif"];
In[]:=
Show[tifdog]
Out[]=
⁃Graphics⁃
Color conversions
Color conversions
Accessing image data
Accessing image data
Mathematica handles many different kinds of objects: mathematical formulas, lists, and graphics, to name but a few. Although they 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.
In[]:=
Head[dog]
Out[]=
Graphics
In[]:=
Head[tifdog]
Out[]=
Graphics
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.
In[]:=
dog=dog/.GraphicsList;
The list contains an array of pixel values and some supplemental information on image size and color model. Short gives an overview.
In[]:=
Short[dog,12]
Out[]=
{Raster[1,{{0,0},{291,302}},{0,255},ColorFunctionRGBColor],AspectRatioAutomatic}
The pixel values are always the first element of the data structure. They can now be treated like any other list in Mathematica. A RGB image, for example, is represented as an array of triples.
{r,g,b}
f(x,y)=
r(0,0),g(0,0),b(0,0) | ... | r(0,N-1),g(0,N-1),b(0,N-1) |
r(1,0),g(1,0),b(1,0) | ⋯ | r(1,N-1),g(1,N-1),b(1,N-1) |
⋮ | ⋱ | ⋮ |
r(M-1,0),g(M-1,0),b(M-1,0) | ⋯ | r(M-1,N-1),g(M-1,N-1),b(M-1,N-1) |
The next line extracts the array of pixel values. To learn more about matrix manipulation, consult . of The Mathematica Book or the Matrix Manipulation how-to.
Section 1.8.4
In[]:=
pixelvalues=dog[[1,1]];
As you can see, the color values are now stored in a 302×291×3array.
In[]:=
Dimensions[pixelvalues]
Out[]=
{302,291,3}
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.
In[]:=
ListDensityPlot[pixelvalues[[All,All,1]],MeshFalse];
Simple math
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.
In[]:=
ListDensityPlot[-pixelvalues[[All,All,1]],MeshFalse];
In[]:=
ListDensityPlot[256-pixelvalues[[All,All,1]],MeshFalse];
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.
In[]:=
Show[Graphics[RasterArray[Apply[(RGBColor[#1,#2,#3]&),1-pixelvalues/256.,{2}]]],AspectRatio1];
Histograms
Histograms
Histogram operations
Histogram operations
Selecting pieces of images
Selecting pieces of images
Area selection
Area selection
Thresholding
Thresholding
Convolution and correlation
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 3×3 kernels.
In[]:=
ListDensityPlotListConvolve
,ListConvolve
,pixelvalues[[All,All,1]]256.,MeshFalse;
-1 | -2 | -1 |
4 | 0 | 5 |
1 | 2 | 1 |
1 | 0 | 1 |
0 | 0 | 2 |
-1 | 0 | 1 |
List interpolation and data recovery
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
The Doghill mountains
■
Data recovery
Data recovery
Let's go back to the small piece of the image selected in "Selecting pieces of images".
righteye=Take[pixelvalues,{150,180},{110,140}];
ListDensityPlot[righteye[[All,All,1]],MeshFalse]
⁃DensityGraphics⁃
As you can see, the image above consists of only 51×31 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 and 1 in the x and y coordinates.
0
interpol1=ListInterpolation[righteye[[All,All,1]],{{0,1},{0,1}}];
DensityPlot[interpol1[x,y],{x,0,1},{y,0,1},PlotPoints100,MeshFalse];
■
Mathematical operations
Mathematical operations


Cite this as: Wolfram Research, Inc., "How Do I Do Basic Image Processing in Mathematica?" from the Notebook Archive (2002), https://notebookarchive.org/2018-10-10r8f3h

Download

