(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 10564, 283]*) (*NotebookOutlinePosition[ 11220, 306]*) (* CellTagsIndexPosition[ 11176, 302]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Discrete grayscale image manipulations", "Section"], Cell["\<\ While you can manipulate color images, for purposes of illustration it's much \ easier to work with grayscale images. They can be represented as simple \ rectangular matrices of numbers, which can be manipulated as mathematical \ objects. Color images must be represented as matrices of red, green, blue \ triplets, which are more complicated to deal with. So, we'll start our \ manipulations by extracting the image data from the expression, scaling it so \ the values run between 0 and 1, and adding up a weighted average of the red, \ green, and blue components of each pixel (the weights are based on the \ sensitivity of the eye to each color).\ \>", "Text"], Cell[BoxData[ \(GrayscaleData[ image_]\ := \ \[IndentingNewLine]Map[\(0.3 #[\([1]\)] + 0.59 \ #[\([2]\)] + 0.11 #[\([3]\)]\)\/255. &, \ N[ImageData[image]], {2}]\)], "Input"], Cell[BoxData[ \(\(grayscalepup\ = \ GrayscaleData[pup];\)\)], "Input"], Cell[BoxData[ \(\(Show[Graphics[Raster[grayscalepup]], AspectRatio \[Rule] Automatic];\)\)], "Input"], Cell["\<\ One reason for writing the command with parts as above is that it produces \ packed arrays:\ \>", "Text"], Cell[BoxData[ \(Developer`PackedArrayQ[grayscalepup]\)], "Input"], Cell[TextData[{ "Now that we have the data as a simple 2-dimensional matrix of numbers, \ it's easy to do all sorts of things to it. For example, if we subtract it \ from one, we will get an effect like a photographic negative (remember the \ data is scaled to have values between 0 and 1). Note that we are making use \ of the fact that in ", StyleBox["Mathematica", FontSlant->"Italic"], ", you can perform mathematical operations on nested lists, and the \ operations will automatically be applied to each element. So in this \ example, we are in fact subtracting each element of the matrix from one \ individually." }], "Text"], Cell[BoxData[ \(\(Show[Graphics[Raster[1. - \ grayscalepup]], AspectRatio \[Rule] Automatic];\)\)], "Input"], Cell[BoxData[ \(\(Show[ Graphics[ Raster[grayscalepup . DiagonalMatrix[Table[Random[], {Length[grayscalepup]}]]]], \ AspectRatio \[Rule] Automatic];\)\)], "Input"], Cell["\<\ There are lots of transformations you can do on the data, such as increasing \ constrast, etc. I will show some examples of some involving some \ mathematical sophistication.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Show", "[", RowBox[{"Graphics", "[", RowBox[{ RowBox[{"Raster", "[", "\[IndentingNewLine]", RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{\(1\/9\), RowBox[{"(", GridBox[{ {"1", "1", "1"}, {"1", "1", "1"}, {"1", "1", "1"} }], ")"}]}], ",", "grayscalepup"}], "]"}], "]"}], ",", "\[IndentingNewLine]", \(AspectRatio \[Rule] Automatic\)}], "]"}], "]"}], ";"}]], "Input"], Cell[TextData[{ "As might be suspected from the name, ", ButtonBox["ListConvolve", ButtonData:>{"ListConvolve"}, ButtonStyle->"RefGuideLink"], " does a convolution. In this example, the kernel averages over adjacent \ points, so the result is fuzzier. If you want to look at the average values \ as a block, you can select the middle points only." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Show", "[", RowBox[{"Graphics", "[", RowBox[{ RowBox[{"Raster", "[", "\[IndentingNewLine]", RowBox[{"Take", "[", RowBox[{ RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{\(1\/9\), RowBox[{"(", GridBox[{ {"1", "1", "1"}, {"1", "1", "1"}, {"1", "1", "1"} }], ")"}]}], ",", "grayscalepup"}], "]"}], ",", \({1, \(-1\), 3}\), ",", \({1, \(-1\), 3}\)}], "]"}], "]"}], ",", "\[IndentingNewLine]", \(AspectRatio \[Rule] Automatic\)}], "]"}], "]"}], ";"}]], "Input"], Cell["This kernel does a form of edge detection. ", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Show", "[", RowBox[{"Graphics", "[", RowBox[{ RowBox[{"Raster", "[", "\[IndentingNewLine]", RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{"(", GridBox[{ {"1", "1", "1"}, {"1", \(-8\), "1"}, {"1", "1", "1"} }], ")"}], ",", "grayscalepup"}], "]"}], "]"}], ",", "\[IndentingNewLine]", \(AspectRatio \[Rule] Automatic\)}], "]"}], "]"}], ";"}]], "Input"], Cell["\<\ The above edge detection kernel is not too far from a numerical approximation \ of the Laplacian operator. A second order (unscaled) approximation is\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Show", "[", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"Raster", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{"(", GridBox[{ {"0", "1", "0"}, {"1", \(-4\), "1"}, {"0", "1", "0"} }], ")"}], ",", "grayscalepup"}], "]"}], ",", " ", \({{0, 0}, Dimensions[grayscalepup]}\), ",", \({0, 1}\), ",", " ", \(ColorFunction \[Rule] GrayLevel\)}], "]"}], "]"}], ",", "\[IndentingNewLine]", \(AspectRatio \[Rule] Automatic\)}], "]"}], ";"}]], "Input"], Cell["\<\ Using this idea, we can see the effect of an approximation of the heat \ equation\ \>", "Text"], Cell[BoxData[{ \(\(cfl\ = \ 0.1;\)\), "\[IndentingNewLine]", \(\(nt\ = \ 25;\)\)}], "Input"], Cell[BoxData[ RowBox[{\(diffusedpup\ = \ grayscalepup\), ";", RowBox[{"puptab", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{\(Show[Graphics[Raster[diffusedpup]], \ AspectRatio \[Rule] Automatic]\), ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"diffusedpup", " ", "=", " ", RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{ RowBox[{"(", GridBox[{ {"0", "0", "0"}, {"0", "1", "0"}, {"0", "0", "0"} }], ")"}], "+", RowBox[{"cfl", RowBox[{"(", GridBox[{ {"0", "1", "0"}, {"1", \(-4\), "1"}, {"0", "1", "0"} }], ")"}]}]}], ",", "diffusedpup", ",", \({\(-2\), 2}\), ",", " ", "diffusedpup"}], "]"}]}], ",", \({nt}\)}], "]"}], ";", "diffusedpup"}], ",", \({10}\)}], "]"}]}], ";"}]], "Input"], Cell["\<\ Wouldn't it be nice if we could go backwards and imporve the resolution?\ \>", "Text"], Cell[BoxData[ RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{\(diffusedpup\ = \ puptab[\([i]\)]\), ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"diffusedpup", " ", "=", " ", RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{ RowBox[{"(", GridBox[{ {"0", "0", "0"}, {"0", "1", "0"}, {"0", "0", "0"} }], ")"}], "-", RowBox[{"cfl", RowBox[{"(", GridBox[{ {"0", "1", "0"}, {"1", \(-4\), "1"}, {"0", "1", "0"} }], ")"}]}]}], ",", "diffusedpup", ",", \({\(-2\), 2}\), ",", " ", "diffusedpup"}], "]"}]}], ",", \({nt*i}\)}], "]"}], ";", \(Show[Graphics[Raster[diffusedpup]], \ AspectRatio \[Rule] Automatic]\)}], ",", \({i, 1, 3}\)}], "]"}]], "Input"], Cell[BoxData[ \(Max[diffusedpup]\)], "Input"], Cell[BoxData[ \(Min[diffusedpup]\)], "Input"] }, Open ]] }, FrontEndVersion->"4.0 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 695}}, WindowSize->{496, 599}, WindowMargins->{{110, Automatic}, {31, Automatic}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 57, 0, 53, "Section"], Cell[1799, 53, 676, 10, 166, "Text"], Cell[2478, 65, 200, 4, 85, "Input"], Cell[2681, 71, 75, 1, 30, "Input"], Cell[2759, 74, 114, 2, 50, "Input"], Cell[2876, 78, 115, 3, 33, "Text"], Cell[2994, 83, 69, 1, 30, "Input"], Cell[3066, 86, 644, 12, 147, "Text"], Cell[3713, 100, 122, 2, 50, "Input"], Cell[3838, 104, 209, 5, 130, "Input"], Cell[4050, 111, 199, 4, 52, "Text"], Cell[4252, 117, 639, 15, 105, "Input"], Cell[4894, 134, 375, 8, 71, "Text"], Cell[5272, 144, 823, 19, 126, "Input"], Cell[6098, 165, 60, 0, 33, "Text"], Cell[6161, 167, 591, 14, 105, "Input"], Cell[6755, 183, 175, 3, 52, "Text"], Cell[6933, 188, 776, 17, 146, "Input"], Cell[7712, 207, 105, 3, 33, "Text"], Cell[7820, 212, 103, 2, 50, "Input"], Cell[7926, 216, 1286, 27, 186, "Input"], Cell[9215, 245, 96, 2, 33, "Text"], Cell[9314, 249, 1130, 25, 166, "Input"], Cell[10447, 276, 49, 1, 30, "Input"], Cell[10499, 279, 49, 1, 30, "Input"] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)