(*********************************************************************** 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[ 9264, 270]*) (*NotebookOutlinePosition[ 9919, 293]*) (* CellTagsIndexPosition[ 9875, 289]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Continuous function image manipulation", "Section"], Cell[TextData[{ "Convolution is a very powerful image manipulation tool. But, it is \ possible to go much further with ", StyleBox["Mathematica", FontSlant->"Italic"], ". We are now going to see how to use the unique combination of symbolic \ and numerical capabilities of ", StyleBox["Mathematica", FontSlant->"Italic"], " to manipulate images in ways impossible in any other system." }], "Text"], Cell["\<\ The key idea is to transform the image from a discrete matrix of values into \ a continuous mathematical function, which can then be manipulated \ mathematically.\ \>", "Text"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " includes a tremendously clever and powerful feature for doing this. ", StyleBox["InterpolatingFunction", "Input"], " objects, introduced originally to represent the solutions to differential \ equations, are objects that act just like ordinary functions, but are based \ on tables of values. The function ", StyleBox["ListInterpolation", "Input"], " takes a list or matrix of numbers, and returns an ", StyleBox["InterpolatingFunction", "Input"], " that can be used to get interpolated values out of the array. " }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(fun = ListInterpolation[ Transpose[GrayscaleData[puppyImage]], {{0, 1}, {0, 1}}]\)], "Input"], Cell[BoxData[ TagBox[\(InterpolatingFunction[{{0.`, 1.`}, {0.`, 1.`}}, "<>"]\), False, Editable->False]], "Output"] }, Open ]], Cell["\<\ This is an approximate function which interpolates the data at the pixels of \ the image. That is to say the value of the function at the pixel position \ is exactly the grayscale value at that position, but in between, it uses a \ polynomial to give a continuous intermediate function value. \ \>", "Text"], Cell[TextData[{ "Now, instead of thinking in terms of arrays of pixels, we can now think in \ terms of a mathematical function (of two variables) that happens to have ", StyleBox["z", FontSlant->"Italic"], "-values that correspond to the brightness of patches of our image. So you \ can plot it like any other mathematical function:" }], "Text"], Cell[BoxData[ \(<< RealTime3D`\)], "Input"], Cell[BoxData[ \(\(Plot3D[fun[x, y], {x, 0, 1}, {y, 0, 1}, PlotPoints \[Rule] 80, \ PlotRange \[Rule] All];\)\)], "Input"], Cell["An alternative way to plot it is with DensityPlot", "Text"], Cell[BoxData[ \(\(DensityPlot[ fun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 200, Mesh \[Rule] False, AspectRatio \[Rule] Automatic, Frame \[Rule] None];\)\)], "Input"], Cell["\<\ So far, we have not seen how the image is smoothed out in a sense. This can \ be well seen by the puppy's left eye:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(eyefun\ = ListInterpolation[ Transpose[GrayscaleData[lefteye]], {{0, 1}, {0, 1}}]\)], "Input"], Cell[BoxData[ TagBox[\(InterpolatingFunction[{{0.`, 1.`}, {0.`, 1.`}}, "<>"]\), False, Editable->False]], "Output"] }, Open ]], Cell["This shows the original (grayscaled) pixels", "Text"], Cell[BoxData[ \(\(DensityPlot[ eyefun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] Dimensions[GrayscaleData[lefteye]], Mesh \[Rule] False, AspectRatio \[Rule] Automatic, Frame \[Rule] None];\)\)], "Input"], Cell["\<\ But, we're not limited to the original pixels: Let's plot the same region \ with 10 times as many sample points in each direction:\ \>", "Text"], Cell[BoxData[ \(\(DensityPlot[ eyefun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 10\ Dimensions[GrayscaleData[lefteye]], Mesh \[Rule] False, AspectRatio \[Rule] Automatic, Frame \[Rule] None];\)\)], "Input"], Cell["Or", "Text"], Cell[BoxData[ \(\(Plot3D[ eyefun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 5\ Dimensions[GrayscaleData[lefteye]], Mesh \[Rule] False];\)\)], "Input"], Cell["\<\ Another approach to images is to plot contours. This is in effect a type of \ interpolation as well. However, instead of discrete pixels, we now have \ discrete grayscale thresholds, by default 10 levels of gray:\ \>", "Text"], Cell[BoxData[ \(\(ContourPlot[ fun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 50, AspectRatio \[Rule] Automatic, Frame \[Rule] None];\)\)], "Input"], Cell[BoxData[ \(\(ContourPlot[ fun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 50, AspectRatio \[Rule] Automatic, Frame \[Rule] None, ContourShading \[Rule] False];\)\)], "Input"], Cell[BoxData[ \(\(ContourPlot[ fun[x, y], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 200, ContourLines \[Rule] False, AspectRatio \[Rule] Automatic, Frame \[Rule] None];\)\)], "Input"], Cell[TextData[{ "The InterpolatingFunction object can be treated like any other function in \ ", StyleBox["Mathematica.", FontSlant->"Italic"], " This means you can apply operators to it, such as differentiation..." }], "Text"], Cell[BoxData[ \(\(Show[ GraphicsArray[ Block[{$DisplayFunction\ = \ Identity}, {\[IndentingNewLine]DensityPlot[ Evaluate[D[fun[x, y], x]], {x, 0, 1}, {y, 0, 1}, PlotPoints \[Rule] 200, Mesh \[Rule] False, AspectRatio \[Rule] Automatic], DensityPlot[Evaluate[D[fun[x, y], y]], {x, 0, 1}, {y, 0, 1}, PlotPoints \[Rule] 200, Mesh \[Rule] False, AspectRatio \[Rule] Automatic]}]]];\)\)], "Input"], Cell[BoxData[ \(\(Show[ GraphicsArray[ Block[{$DisplayFunction\ = \ Identity}, {\[IndentingNewLine]DensityPlot[ Evaluate[Sqrt[D[fun[x, y], x]^2\ + D[fun[x, y], y]^2]], {x, 0, 1}, {y, 0, 1}, PlotPoints \[Rule] 200, Mesh \[Rule] False, AspectRatio \[Rule] Automatic], DensityPlot[ Evaluate[ Abs[D[fun[x, y], x, x]\ + \ D[fun[x, y], y, y]]], {x, 0, 1}, {y, 0, 1}, PlotPoints \[Rule] 200, Mesh \[Rule] False, AspectRatio \[Rule] Automatic]}]]];\)\)], "Input"], Cell[TextData[{ "What happens if we apply a function to the ", StyleBox["x", FontSlant->"Italic"], " and ", StyleBox["y", FontSlant->"Italic"], " variables before passing them to the function? This has the effect of \ changing the spacing at which the original image is sampled, as a function of \ the ", StyleBox["x", FontSlant->"Italic"], " and ", StyleBox["y", FontSlant->"Italic"], " coordinates. Perhaps an example will make this clear:" }], "Text"], Cell[BoxData[ \(\(DensityPlot[ fun[4\ x*\((1\ - \ x)\), 4\ y*\((1 - y)\)], {x, 0, 1}, {y, 0, 1}, \[IndentingNewLine]PlotPoints \[Rule] 200, Mesh \[Rule] False, AspectRatio \[Rule] Automatic, Frame \[Rule] None];\)\)], "Input"], Cell["\<\ Looking at a plot of the transformation, you can get a better idea of how the \ reflection happened:\ \>", "Text"], Cell[BoxData[ \(\(Plot[4\ x\ \((1\ - \ x)\), {x, 0, 1}];\)\)], "Input"] }, Open ]] }, FrontEndVersion->"4.0 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 695}}, WindowSize->{710, 665}, WindowMargins->{{152, Automatic}, {0, 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, 417, 10, 71, "Text"], Cell[2219, 65, 186, 4, 52, "Text"], Cell[2408, 71, 611, 12, 92, "Text"], Cell[CellGroupData[{ Cell[3044, 87, 128, 3, 30, "Input"], Cell[3175, 92, 131, 3, 29, "Output"] }, Open ]], Cell[3321, 98, 319, 5, 71, "Text"], Cell[3643, 105, 356, 7, 71, "Text"], Cell[4002, 114, 47, 1, 30, "Input"], Cell[4052, 117, 134, 2, 30, "Input"], Cell[4189, 121, 65, 0, 33, "Text"], Cell[4257, 123, 225, 4, 50, "Input"], Cell[4485, 129, 140, 3, 33, "Text"], Cell[CellGroupData[{ Cell[4650, 136, 130, 3, 30, "Input"], Cell[4783, 141, 131, 3, 29, "Output"] }, Open ]], Cell[4929, 147, 59, 0, 33, "Text"], Cell[4991, 149, 270, 5, 70, "Input"], Cell[5264, 156, 154, 3, 33, "Text"], Cell[5421, 161, 274, 5, 70, "Input"], Cell[5698, 168, 18, 0, 33, "Text"], Cell[5719, 170, 217, 5, 50, "Input"], Cell[5939, 177, 238, 4, 52, "Text"], Cell[6180, 183, 204, 4, 50, "Input"], Cell[6387, 189, 243, 5, 50, "Input"], Cell[6633, 196, 242, 5, 50, "Input"], Cell[6878, 203, 238, 6, 52, "Text"], Cell[7119, 211, 531, 10, 110, "Input"], Cell[7653, 223, 634, 12, 110, "Input"], Cell[8290, 237, 490, 16, 52, "Text"], Cell[8783, 255, 259, 4, 50, "Input"], Cell[9045, 261, 124, 3, 33, "Text"], Cell[9172, 266, 76, 1, 30, "Input"] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)