(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. 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[ 14431, 421]*) (*NotebookOutlinePosition[ 15075, 443]*) (* CellTagsIndexPosition[ 15031, 439]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Haar Edge Detection", "Subtitle"], Cell["\<\ Patrick J. Van Fleet Minicourse #5, Joint Mathematics Meetings New Orleans, LA\ \>", "Subsubtitle"], Cell["Objective", "Section"], Cell[CellGroupData[{ Cell["\<\ In this notebook, we will learn how to perform naive image edge detection \ using the 2D Haar Wavelet Transform.\ \>", "Subsubtitle"], Cell["Conventions", "Section"] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "This notebook uses the package ", StyleBox["WaveletFunctions", FontColor->RGBColor[0.501961, 0, 0]], " (written by Patrick Van Fleet). All commands from the WaveletFunctions \ library will be denoted in ", StyleBox["maroon", FontColor->RGBColor[0.501961, 0, 0]], ". Help is available for almost every command in ", StyleBox["WaveletFunctions", FontColor->RGBColor[0.501961, 0, 0]], ". Click on Help and then Help Browser (or Shift F1). Next click the \ Add-ons & Links tab. Scroll down until you find ", StyleBox["WaveletFunctions", FontColor->RGBColor[0.501961, 0, 0]], ". Like all ", StyleBox["Mathematica", FontSlant->"Italic"], " help screens, the help is \"live\" - you can either execute the commands \ in the help to see the effects of the command or cut and paste them into your \ own notebook.\n\nValues that can be changed by the user are defined using ", StyleBox["blue", FontColor->RGBColor[0, 0, 1]], ".\n\nComments are useful within cells of code. Any code enclosed by (* \ *) is a comment and ignored by the ", StyleBox["Mathematica", FontSlant->"Italic"], " kernel.\n" }], "Subsubtitle"], Cell["Load WaveletFunctions and a Linear Algebra Package", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ The linear algebra package is needed only to easily create a zero matrix. \ (You can do this without the package.)\ \>", "Subsubtitle"], Cell[BoxData[{ \(<< WaveletFunctions`WaveletFunctions`\), "\[IndentingNewLine]", \(<< LinearAlgebra`MatrixManipulation`\), "\[IndentingNewLine]", \(\)}], "Input"], Cell["Spelling Off", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ This turns off the annoying error messages you get when the spelling of two \ variable names is close.\ \>", "Subsubtitle"], Cell[BoxData[{ \(\(Off[General::spell];\)\), "\[IndentingNewLine]", \(\(Off[General::spell1];\)\)}], "Input"], Cell["Locating Images", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ If you have access to the internet, then use the imgdir variable that is \ listed for the internet. Else, use the imgdir for images already stored on \ your computer. If you have your own image, create a url/folder location as \ the imgdir variable.\ \>", "Subsubtitle"], Cell[BoxData[ RowBox[{\( (*\ This\ variable\ is\ the\ location\ of\ images\ on\ the\ \ \(\(web\)\(.\)\)\ *) \), "\[IndentingNewLine]", RowBox[{ StyleBox[\(imgdir = \ "\";\), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", "\[IndentingNewLine]", \( (*\ This\ variable\ is\ the\ location\ of\ images\ on\ the\ local\ \ \(\(computer\)\(.\)\)\ *) \), "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(imgdir\ = \ $BaseDirectory <> "\<\\Applications\\Images\ \\\>";\), FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", \( (*\ You\ can\ create\ your\ own\ version\ of\ imgdir\ \ \(\(below\)\(.\)\)\ *) \), "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(\(\(imgdir\)\(\ \)\(=\)\)\ ;\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}], "\[IndentingNewLine]"}]}]], "Input"], Cell["Available Images", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Here is a list of images for use with this notebook. Surprisingly cartoons \ work really well! Some images work better than others. Uncomment one of the \ images. You can also add your own image!\ \>", "Subsubtitle"], Cell[BoxData[{ RowBox[{ StyleBox[\(image\ = \ "\";\), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image = \ "\";\), FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], "*)"}], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image\ = \ "\";\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image\ = \ "\";\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image\ = \ "\";\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image = "\";\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image\ = \ "\";\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"(*", " ", StyleBox[\(image\ = \ "\";\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"(*", " ", StyleBox[\(\(\(image\)\(\ \)\(=\)\)\ ;\), FontColor->RGBColor[0, 0, 1]], " ", "*)"}]}]}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Here are some maximum iteration numbers for our image list:\n\n", StyleBox["File", FontWeight->"Bold"], "\t\t\t\t", StyleBox["Max Iterations", FontWeight->"Bold"], "\nbeatles.png\t\t\t\t2\ncalvin_hobbes_gray.jpg\t\t5\nfingerprint.gif\t\t\t\ \t7\ngarfield_gray.jpg\t\t\t3\nmoonsurface.png\t\t\t3\t\nsnoopy_gray.jpg\t\t\t\ 1\nspockkirkmccoy.png\t\t\t3\nstooges.gif\t\t\t\t5" }], "Subsubtitle"], Cell["Load an Image", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["We first load an image.", "Subsubtitle"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"A", "=", StyleBox[\(ReadImage[imgdir <> image, PrintInfo \[Rule] True]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\n", \( (*\ If\ your\ image\ was\ processed\ as\ a\ color\ image, \ uncomment\ the\ line\ below\ to\ convert\ \(\(it\)\(.\)\)\ *) \), "\[IndentingNewLine]", \( (*\ \(A\ = \ Total[A]/3;\)\ *) \), "\[IndentingNewLine]"}], "\n", StyleBox[\(ImagePlot[A, Frame -> True, FrameTicks -> False];\), FontColor->RGBColor[0.501961, 0, 0]]}], "Input"], Cell["Edge Detection", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ To perform edge detection on the image, we first compute the 2D HWT.\ \>", "Subsubtitle"], Cell[BoxData[{ RowBox[{ RowBox[{"B", " ", "=", " ", StyleBox[\(WT2D[A, { .5, .5}, NumIterations \[Rule] 1]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", StyleBox[\(WaveletDensityPlot[B, NumIterations \[Rule] 1, DivideLines \[Rule] True];\), FontColor->RGBColor[0.501961, 0, 0]]}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "The next step is to replace the upper left hand corner with a zero matrix \ of the same dimensions. The ", StyleBox["WaveletFunctions", FontColor->RGBColor[0.501961, 0, 0]], " module ", StyleBox["PutCorner", FontColor->RGBColor[0.501961, 0, 0]], " will help here. ", StyleBox["PutCorner", FontColor->RGBColor[0.501961, 0, 0]], " takes two arguments and replaces the upper left hand corner of the first \ argument with the second argument." }], "Subsubtitle"], Cell[BoxData[{\({rows, cols}\ = \ Dimensions[A];\), "\[IndentingNewLine]", \(Z\ = \ ZeroMatrix[rows/2, cols/2];\), "\[IndentingNewLine]", RowBox[{ RowBox[{"newB", " ", "=", StyleBox[\(PutCorner[B, Z]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", StyleBox[\(WaveletDensityPlot[newB, NumIterations \[Rule] 1, DivideLines \[Rule] True];\), FontColor->RGBColor[0.501961, 0, 0]]}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ We are left with the vertical, horizontal, and diagonal differences. We \ compute the inverse of the modified transform to obtain the edges.\ \>", "Subsubtitle"], Cell[BoxData[{ RowBox[{ RowBox[{"edges", " ", "=", " ", StyleBox[\(IWT2D[newB, {1. , 1. }, NumIterations \[Rule] 1]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", StyleBox[\(ImagePlot[edges, LinearScaling \[Rule] True];\), FontColor->RGBColor[0.501961, 0, 0]]}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Try the procedure again with different images.", "Subsubtitle"], Cell["Iterating the Process", "Section"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ You can use the iterated transform as part of the algorithm. \ \>", "Subsubtitle"], Cell[BoxData[{ StyleBox[\(image = "\";\), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", StyleBox[\(ReadImage[imgdir <> image]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", StyleBox[\(ImagePlot[A, LinearScaling \[Rule] True];\), FontColor->RGBColor[0.501961, 0, 0]], "\[IndentingNewLine]", RowBox[{\({rows, cols} = Dimensions[A];\), "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ StyleBox[\(its = 2\), FontColor->RGBColor[0, 0, 1]], StyleBox[";", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], \( (*\ Do\ not\ exceed\ the\ maximum\ number\ of\ iterations\ \(allowed!!\)\ \ *) \), "\[IndentingNewLine]", RowBox[{"B", " ", "=", StyleBox[" ", FontColor->RGBColor[0.501961, 0, 0]], StyleBox[\(WT2D[A, { .5, .5}, NumIterations \[Rule] its]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", RowBox[{ StyleBox[\(WaveletDensityPlot[B, NumIterations \[Rule] its, DivideLines \[Rule] True];\), FontColor->RGBColor[0.501961, 0, 0]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", \(Z = ZeroMatrix[rows/2^its, cols/2^its];\), "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["newB", FontColor->GrayLevel[0]], StyleBox[" ", FontColor->GrayLevel[0]], StyleBox["=", FontColor->GrayLevel[0]], StyleBox[" ", FontColor->GrayLevel[0]], StyleBox[\(PutCorner[B, Z]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", RowBox[{ StyleBox[\(WaveletDensityPlot[newB, NumIterations \[Rule] its, DivideLines \[Rule] True];\), FontColor->RGBColor[0.501961, 0, 0]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["edges", FontColor->GrayLevel[0]], StyleBox["=", FontColor->GrayLevel[0]], StyleBox[\(IWT2D[newB, {1. , 1. }, NumIterations -> its]\), FontColor->RGBColor[0.501961, 0, 0]]}], StyleBox[";", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", RowBox[{ StyleBox[\(ImagePlot[edges, LinearScaling -> True];\), FontColor->RGBColor[0.501961, 0, 0]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0.501961, 0, 0]]}], "\[IndentingNewLine]", }], "Input"], Cell["To Consider ...", "Section"] }, Open ]], Cell["\<\ 1) What happens to the edge image if you increase the number of iterations? 2) Why do you suppose this happens? 3) How could you modify the process to improve the identification of edges?\ \>", "Subsubtitle"] }, Open ]] }, FrontEndVersion->"5.2 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 695}}, WindowSize->{1016, 651}, WindowMargins->{{0, Automatic}, {Automatic, 0}} ] (******************************************************************* 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[1776, 53, 39, 0, 51, "Subtitle"], Cell[1818, 55, 109, 4, 70, "Subsubtitle"], Cell[1930, 61, 28, 0, 73, "Section"], Cell[CellGroupData[{ Cell[1983, 65, 143, 3, 30, "Subsubtitle"], Cell[2129, 70, 30, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[2196, 75, 1186, 28, 192, "Subsubtitle"], Cell[3385, 105, 69, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[3491, 110, 146, 3, 30, "Subsubtitle"], Cell[3640, 115, 176, 3, 70, "Input"], Cell[3819, 120, 31, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[3887, 125, 133, 3, 30, "Subsubtitle"], Cell[4023, 130, 118, 2, 50, "Input"], Cell[4144, 134, 34, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[4215, 139, 282, 5, 50, "Subsubtitle"], Cell[4500, 146, 1098, 24, 190, "Input"], Cell[5601, 172, 35, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[5673, 177, 230, 4, 50, "Subsubtitle"], Cell[5906, 183, 1490, 31, 190, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[7433, 219, 426, 10, 231, "Subsubtitle"], Cell[7862, 231, 32, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[7931, 236, 46, 0, 30, "Subsubtitle"], Cell[7980, 238, 651, 13, 110, "Input"], Cell[8634, 253, 33, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[8704, 258, 99, 2, 30, "Subsubtitle"], Cell[8806, 262, 430, 9, 50, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[9273, 276, 503, 13, 70, "Subsubtitle"], Cell[9779, 291, 546, 11, 90, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10362, 307, 172, 3, 30, "Subsubtitle"], Cell[10537, 312, 399, 8, 50, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10973, 325, 69, 0, 30, "Subsubtitle"], Cell[11045, 327, 40, 0, 73, "Section"] }, Open ]], Cell[CellGroupData[{ Cell[11122, 332, 93, 2, 30, "Subsubtitle"], Cell[11218, 336, 2923, 71, 350, "Input"], Cell[14144, 409, 34, 0, 73, "Section"] }, Open ]], Cell[14193, 412, 222, 6, 110, "Subsubtitle"] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)