(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 58629, 1833] NotebookOptionsPosition[ 50341, 1571] NotebookOutlinePosition[ 50828, 1589] CellTagsIndexPosition[ 50785, 1586] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Digital Images and Measures", "Title", CellChangeTimes->{{3.421431042796875*^9, 3.421431044359375*^9}}], Cell["\<\ Wavelet Workshop June 4-7, 2008 University of St. Thomas\ \>", "Subtitle", CellChangeTimes->{{3.421426484890625*^9, 3.42142648821875*^9}}], Cell["\<\ Catherine Beneteau Caroline Haddad David Ruch Patrick Van Fleet\ \>", "Subsubtitle", CellChangeTimes->{{3.42144927371875*^9, 3.42144928703125*^9}}], Cell[CellGroupData[{ Cell["Overview", "Section", CellChangeTimes->{{3.421428159890625*^9, 3.421428161*^9}}], Cell[CellGroupData[{ Cell["Objectives", "Subsection"], Cell["\<\ The purpose of this notebook is to give you a brief introduction to the \ software package DiscreteWavelets and show you how to use it to load images. \ Some basic image manipulation is illustrated as well. You will also learn \ how to use measures and tools such as cumulative energy, entropy, PSNR, and \ Huffman coding.\ \>", "Text", CellChangeTimes->{{3.421456620265625*^9, 3.421456650625*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["DiscreteWavelets", "Subsection"], Cell[TextData[{ "You should run this cell each time you open this notebook!! It loads the \ ", StyleBox["Mathematica", FontSlant->"Italic"], " package DiscreteWavelets for use in subsequent computations." }], "Text", CellChangeTimes->{{3.421429438203125*^9, 3.421429441265625*^9}}], Cell[BoxData[ RowBox[{"<<", "DiscreteWavelets`DiscreteWavelets`"}]], "Input", CellChangeTimes->{{3.421448451359375*^9, 3.42144845165625*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Help on DiscreteWavelets", "Subsection"], Cell["\<\ If you ever need help with DiscreteWavelets, go to Help, then Documentation \ Center. On the bottom right, you will find a link for Installed Add-Ons. \ Click this link and then the link to DiscreteWavelets to access \ documentation, examples, and applications of the software package.\ \>", "Text", CellChangeTimes->{{3.42142653203125*^9, 3.421426604453125*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Image Basics", "Section", CellChangeTimes->{{3.421428254109375*^9, 3.421428255890625*^9}}], Cell[CellGroupData[{ Cell["Digital Images in the Package", "Subsection"], Cell["\<\ The DiscreteWavelets package comes with 18 grayscale images and 9 color \ images for you to use. There are three functions available to tell you more \ about these images. \ \>", "Text", CellChangeTimes->{{3.421426678765625*^9, 3.421426688421875*^9}}], Cell[CellGroupData[{ Cell["ImageList", "Subsubsection", CellChangeTimes->{{3.4214266908125*^9, 3.421426693453125*^9}}], Cell[TextData[{ "The first function is called ", StyleBox["ImageList", FontWeight->"Bold"], ". This function can tell you the names and sizes of the digital images in \ the package. Click the cells below to see how to use ", StyleBox["ImageList", FontWeight->"Bold"], "." }], "Text", CellChangeTimes->{{3.421426678765625*^9, 3.421426688421875*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"ImageList", "[", RowBox[{"ImageType", "\[Rule]", "GrayScale"}], "]"}]}]], "Input", CellChangeTimes->{{3.421430850546875*^9, 3.421430850765625*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"ImageList", "[", RowBox[{"ImageType", "\[Rule]", "Color"}], "]"}]}]], "Input", CellChangeTimes->{{3.4214308521875*^9, 3.42143085234375*^9}}], Cell[BoxData[ RowBox[{"ImageList", "[", "]"}]], "Input", CellChangeTimes->{{3.421430853921875*^9, 3.4214308540625*^9}}], Cell[TextData[{ "Clicking on ", StyleBox["Kernel", FontWeight->"Bold"], " then ", StyleBox["Delete All Output", FontWeight->"Bold"], " will remove output from this notebook." }], "Text", CellChangeTimes->{3.4214267046875*^9, 3.42142674603125*^9}] }, Open ]], Cell[CellGroupData[{ Cell["ImageNames", "Subsubsection", CellChangeTimes->{{3.42142670821875*^9, 3.421426709671875*^9}}], Cell[TextData[{ "Another useful function is ", StyleBox["ImageNames", FontWeight->"Bold"], ". This function returns a list of absolute path names to each image. \ Since the package can be installed in multiple locations, this functions \ takes the work out of locating the images." }], "Text", CellChangeTimes->{{3.4214267046875*^9, 3.421426718421875*^9}}], Cell[BoxData[ RowBox[{"color", " ", "=", " ", RowBox[{ RowBox[{"ImageNames", "[", RowBox[{"ImageType", "\[Rule]", "Color"}], "]"}]}]}]], "Input"], Cell[TextData[{ "\nYou'll notice a list of 9 path/file names. If you want to use ", StyleBox["building.png", FontWeight->"Bold"], " in an application (the second name in the list), you simply use \ color[[2]].", "\n\n", "From ", StyleBox["ImageList", FontWeight->"Bold"], ", you probably noticed that the images were quite large. Sometimes it's \ handy to work with smaller images. If you set the directive ", StyleBox["ListThumbnails", FontWeight->"Bold"], " to ", StyleBox["True", FontWeight->"Bold"], ", then the file names returned are those of the thumbnails.\n" }], "Text", CellChangeTimes->{{3.4214269214375*^9, 3.421426938828125*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"ImageNames", "[", RowBox[{ RowBox[{"ImageType", "\[Rule]", "Color"}], ",", RowBox[{"ListThumbnails", "\[Rule]", "True"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.421430857171875*^9, 3.421430857890625*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["ShowThumbnails", "Subsubsection", CellChangeTimes->{{3.421426779203125*^9, 3.4214267818125*^9}}], Cell[TextData[{ "If you wish to view thumbnails of all images included in the package, use \ the ", StyleBox["ShowThumbnails", FontWeight->"Bold"], " command. You can specify the ", StyleBox["ImageType", FontWeight->"Bold"], " as either ", StyleBox["GrayScale", FontWeight->"Bold"], " or ", StyleBox["Color", FontWeight->"Bold"], ". If no image type is given the function shows thumbnails of all included \ images." }], "Text", CellChangeTimes->{{3.421426787890625*^9, 3.4214268661875*^9}}], Cell[BoxData[ RowBox[{"ShowThumbnails", "[", RowBox[{"ImageType", "\[Rule]", "GrayScale"}], "]"}]], "Input", CellChangeTimes->{{3.421426884046875*^9, 3.42142689428125*^9}, { 3.421430859578125*^9, 3.421430860078125*^9}}], Cell[BoxData[ RowBox[{"ShowThumbnails", "[", RowBox[{"ImageType", "\[Rule]", "Color"}], "]"}]], "Input", CellChangeTimes->{{3.42142696096875*^9, 3.42142696165625*^9}, { 3.4214308620625*^9, 3.421430862390625*^9}}], Cell[BoxData[ RowBox[{"ShowThumbnails", "[", "]"}]], "Input", CellChangeTimes->{3.421426952359375*^9}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Loading a Digital Image", "Subsection"], Cell[TextData[{ StyleBox["DiscreteWavelets", FontWeight->"Bold"], " comes with a command called ", StyleBox["ImageRead", FontWeight->"Bold"], ". This command essentially needs one argument - the location, either on a \ computer or on the internet, of a digital image. In the case of a grayscale \ image, ", StyleBox["ImageRead", FontWeight->"Bold"], " returns a matrix (named A in the commands below) containing the gray scale \ intensity values for each pixel. \n\nHere are some examples." }], "Text", CellChangeTimes->{{3.4214269689375*^9, 3.421426970390625*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"gray", "=", RowBox[{"ImageNames", "[", RowBox[{"ImageType", "\[Rule]", "GrayScale"}], "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"A", "=", RowBox[{"ImageRead", "[", RowBox[{"gray", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"Dimensions", "[", "A", "]"}]}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.421430865671875*^9, 3.421430865953125*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"gray", "=", RowBox[{"ImageNames", "[", RowBox[{ RowBox[{"ImageType", "\[Rule]", "GrayScale"}], ",", RowBox[{"ListThumbnails", "\[Rule]", "True"}]}], "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"A", "=", RowBox[{"ImageRead", "[", RowBox[{"gray", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"Dimensions", "[", "A", "]"}]}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.42143086809375*^9, 3.421430868328125*^9}}], Cell["Here is the first row of A:", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"A", "[", RowBox[{"[", "1", "]"}], "]"}]}]], "Input", CellChangeTimes->{{3.421430870328125*^9, 3.42143087059375*^9}}], Cell["Here is the 100th row of A:", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"A", "[", RowBox[{"[", "100", "]"}], "]"}]}]], "Input", CellChangeTimes->{{3.421430872390625*^9, 3.4214308725625*^9}}], Cell["Here is the 100th column of A:", "Text"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"Transpose", "[", "A", "]"}], "[", RowBox[{"[", "100", "]"}], "]"}]}]], "Input", CellChangeTimes->{{3.421430874703125*^9, 3.421430874875*^9}}], Cell[TextData[{ "There are two additional directives for ", StyleBox["ImageRead", FontWeight->"Bold"], ". The first is called ", StyleBox["PrintInfo", FontWeight->"Bold"], " and if set to ", StyleBox["True", FontWeight->"Bold"], ", will give you the dimensions and type (grayscale, color) of image you \ have read. The ", StyleBox["PowersOfTwo", FontWeight->"Bold"], " option, if set (say to K) will chop off enough rows on the bottom and \ columns on the left to make the dimensions of the image divisible by ", Cell[BoxData[ SuperscriptBox["2", "K"]]], ". This is really handy when students pull their own images off the web and \ we need the dimensions to be divisible by ", Cell[BoxData[ SuperscriptBox["2", "K"]]], "." }], "Text"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"gray", ",", "color"}], "}"}], "=", RowBox[{"ImageNames", "[", RowBox[{ RowBox[{"ImageType", "\[Rule]", "All"}], ",", RowBox[{"ListThumbnails", "\[Rule]", "True"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", RowBox[{"ImageRead", "[", RowBox[{ RowBox[{"gray", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"PrintInfo", "\[Rule]", "True"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"B", "=", RowBox[{"ImageRead", "[", RowBox[{ RowBox[{"color", "[", RowBox[{"[", "3", "]"}], "]"}], ",", RowBox[{"PrintInfo", "\[Rule]", "True"}]}], "]"}]}], ";"}]}], "Input", CellChangeTimes->{{3.42143087721875*^9, 3.42143087740625*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"A", "=", RowBox[{"ImageRead", "[", RowBox[{ RowBox[{"gray", "[", RowBox[{"[", "3", "]"}], "]"}], ",", RowBox[{"PrintInfo", "\[Rule]", "True"}], ",", RowBox[{"PowersOfTwo", "\[Rule]", "6"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"Dimensions", "[", "A", "]"}], ",", "\"\<.\>\""}], "]"}]}], "Input",\ CellChangeTimes->{{3.421430880046875*^9, 3.421430881640625*^9}}], Cell["\<\ Note that B was processed as a color image. This means that B returned three \ matrices - the first matrix is the red portion of the image (with values 0 to \ 255), the second matrix is the green portion of the image, and the third \ matrix is the blue portion of the image. Notation-wise, we have the matrices B[[ 1 ]] (red), B[[ 2 ]] (green), and B[[ \ 3 ]] (blue), but it might be easier to make the call as follows:\ \>", "Text", CellChangeTimes->{{3.42145836678125*^9, 3.42145837171875*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}], " ", "=", RowBox[{"ImageRead", "[", RowBox[{ RowBox[{"color", "[", RowBox[{"[", "3", "]"}], "]"}], ",", RowBox[{"PrintInfo", "\[Rule]", "True"}]}], "]"}]}], ";"}]], "Input", CellChangeTimes->{{3.421430884578125*^9, 3.42143088515625*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Plotting a Digital Image", "Subsection"], Cell[TextData[{ "It is easy to plot digital images read with ", StyleBox["ImageRead", FontWeight->"Bold"], " in Mathematica. The command is called ", StyleBox["ImagePlot", FontWeight->"Bold"], ". \n\nOnce you have an image loaded via ", StyleBox["ImageRead", FontWeight->"Bold"], ", here's what you can do:" }], "Text"], Cell[BoxData[ RowBox[{"ImagePlot", "[", "A", "]"}]], "Input", CellChangeTimes->{ 3.42142707075*^9, {3.421430886703125*^9, 3.421430886875*^9}}], Cell[BoxData[ RowBox[{"ImagePlot", "[", "B", "]"}]], "Input", CellChangeTimes->{ 3.421427071578125*^9, {3.42143088875*^9, 3.42143088890625*^9}}], Cell[BoxData[ RowBox[{"ImagePlot", "[", RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}], "]"}]], "Input", CellChangeTimes->{ 3.4214270728125*^9, {3.421430889984375*^9, 3.421430890328125*^9}}], Cell[TextData[{ "You can plot individual channels of a color image if you like. It will be \ grayscale by default, but if you want to see it in its original color, simply \ add the directive ", StyleBox["ChannelColor", FontWeight->"Bold"], ":" }], "Text"], Cell[BoxData[{ RowBox[{"ImagePlot", "[", "red", "]"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"red", ",", RowBox[{"ChannelColor", "\[Rule]", "Red"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"green", ",", RowBox[{"ChannelColor", "\[Rule]", "Green"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"blue", ",", RowBox[{"ChannelColor", "\[Rule]", "Blue"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}], "]"}]}], "Input", CellChangeTimes->{{3.42142709346875*^9, 3.42142709615625*^9}, { 3.421430892265625*^9, 3.421430892421875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Reading and Plotting Images from the Internet", "Subsection", CellChangeTimes->{{3.421427651046875*^9, 3.421427658390625*^9}}], Cell["\<\ It is quite easy to read and plot images from the internet. All you need is \ a connection to the internet and a url for the image. You can find the url \ for an image in Internet Explorer by simply right-clicking on it and then \ clicking on Properties:\ \>", "Text", CellChangeTimes->{{3.421427667296875*^9, 3.42142770025*^9}, { 3.421427843515625*^9, 3.4214279033125*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"r", ",", "g", ",", "b"}], "}"}], "=", RowBox[{ "ImageRead", "[", "\"\\"", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"{", RowBox[{"r", ",", "g", ",", "b"}], "}"}], "]"}]}], "Input", CellChangeTimes->{{3.421427704578125*^9, 3.42142770553125*^9}, { 3.421427797640625*^9, 3.421427808203125*^9}, {3.42143098959375*^9, 3.421431005109375*^9}}], Cell[TextData[{ StyleBox["Warning :", FontColor->RGBColor[1, 0, 0]], " Most of our applications will require the dimensions of the image to be \ divisible by some power of 2. Use the PowersOfTwo directive to appropriately \ resize internet images. Images that appear as grayscale are often stored as \ color images on the internet. Use the PrintInfo directive to check this \ before using internet images in applications." }], "Text", CellChangeTimes->{{3.421427906296875*^9, 3.42142799559375*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Manipulating Images", "Subsection"], Cell["\<\ Once you have an image loaded and know how to plot it, you can do all kinds \ of things with it. Convert color to grayscale:\ \>", "Text"], Cell[BoxData[ RowBox[{"ImagePlot", "[", RowBox[{ RowBox[{"(", RowBox[{"red", "+", "green", "+", "blue"}], ")"}], "/", "3"}], "]"}]], "Input", CellChangeTimes->{ 3.42142710846875*^9, {3.421431008125*^9, 3.421431008765625*^9}}], Cell["Darken the image", "Text"], Cell[BoxData[ RowBox[{"ImagePlot", "[", RowBox[{"A", "/", "2"}], "]"}]], "Input", CellChangeTimes->{ 3.421427113140625*^9, {3.42143100984375*^9, 3.4214310101875*^9}}], Cell["Image negative", "Text"], Cell[BoxData[ RowBox[{"ImagePlot", "[", RowBox[{"255", "-", "A"}], "]"}]], "Input", CellChangeTimes->{ 3.42142711625*^9, {3.42143101159375*^9, 3.421431011953125*^9}}], Cell["Color negative", "Text"], Cell[BoxData[ RowBox[{"ImagePlot", "[", RowBox[{"255", "-", RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{ 3.421427125765625*^9, {3.4214310131875*^9, 3.421431013453125*^9}}], Cell["Flip it upside down", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"ImagePlot", "[", RowBox[{"Reverse", "[", "A", "]"}], "]"}]}]], "Input", CellChangeTimes->{{3.42143101553125*^9, 3.421431015765625*^9}}], Cell["What does the transpose do?", "Text"], Cell[BoxData[ RowBox[{"ImagePlot", "[", RowBox[{"Transpose", "[", "A", "]"}], "]"}]], "Input", CellChangeTimes->{{3.42142717653125*^9, 3.42142719546875*^9}, { 3.421431016953125*^9, 3.421431017109375*^9}}], Cell["\<\ The previous cell is basically a rotation of the image counterclockwise 90 \ degrees. In the cell below, can you write code to rotate the image \ clockwise 90 degrees?\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "Remove", " ", "this", " ", "comment", " ", "and", " ", "put", " ", "code", " ", "here"}], " ", "*)"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Converting Color Images to YCbCr Space", "Subsection"], Cell[TextData[{ "DiscreteWavelets contains a command for converting from RGB color space to \ YCbCr space. The command is called ", StyleBox["RGBToYCbCr", FontWeight->"Bold"], ". Here is an example call:" }], "Text", CellChangeTimes->{{3.42142723834375*^9, 3.421427238734375*^9}, { 3.421459798796875*^9, 3.42145980209375*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"Y", ",", "Cb", ",", "Cr"}], "}"}], " ", "=", " ", RowBox[{"RGBToYCbCr", "[", RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"Y", ",", RowBox[{"LinearScaling", "\[Rule]", "True"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"Cb", ",", RowBox[{"LinearScaling", "\[Rule]", "True"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"Cr", ",", RowBox[{"LinearScaling", "\[Rule]", "True"}]}], "]"}]}], "Input", CellChangeTimes->{{3.42142720225*^9, 3.42142720365625*^9}, { 3.42143101953125*^9, 3.421431019734375*^9}}], Cell[TextData[{ "Note that we have used the directive ", StyleBox["LinearScaling", FontWeight->"Bold"], " set to ", StyleBox["True", FontWeight->"Bold"], ". This maps the minimum value of the matrix to 0 and the maximum value of \ the matrix to 255." }], "Text", CellChangeTimes->{{3.421427217734375*^9, 3.421427217828125*^9}}], Cell[TextData[{ "You can convert back to RGB space using the command ", StyleBox["YCbCrToRGB", FontWeight->"Bold"], ":" }], "Text", CellChangeTimes->{3.42142724303125*^9}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}], "=", RowBox[{"YCbCrToRGB", "[", RowBox[{"{", RowBox[{"Y", ",", "Cb", ",", "Cr"}], "}"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", RowBox[{"{", RowBox[{"red", ",", "green", ",", "blue"}], "}"}], "]"}]}], "Input", CellChangeTimes->{ 3.42142724790625*^9, {3.42143102153125*^9, 3.421431021703125*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Subsection"], Cell[CellGroupData[{ Cell["Exercise 1", "Subsubsection"], Cell["\<\ In the cell below, find and plot the grayscale image of the clown that comes \ with the DiscreteWavelets package.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "Remove", " ", "this", " ", "comment", " ", "and", " ", "put", " ", "code", " ", RowBox[{"here", "."}]}], " ", "*)"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 2", "Subsubsection"], Cell["\<\ In the cell below, find and plot the red, green, and blue color channels \ (when reading the image, name the three matrices red, green, and blue) of the \ image of the goats that comes with the DiscreteWavelets package.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "Remove", " ", "this", " ", "comment", " ", "and", " ", "put", " ", "code", " ", RowBox[{"here", "."}]}], " ", "*)"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 3", "Subsubsection"], Cell["\<\ In an example above, we converted a color image to grayscale by simply \ averaging the channels. The National Television System Committee suggests \ using a weight average of these three channels for this conversion. The NTSC \ map is gray = .299*red + .587*green + .114*blue In the cell below, load and plot the legos image that comes with the package. \ Next create two grayscale images from this color image. Build the first by \ averaging the three channels and construct the second using the NTSC map. \ Call the first image gray1 and the second gray2. Plot both images - do you \ notice a difference? Repeat the exercise with some other color images included with the package or \ one from the internet.\ \>", "Text", CellChangeTimes->{{3.421427335640625*^9, 3.4214275079375*^9}, { 3.421427539640625*^9, 3.4214276243125*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "Remove", " ", "this", " ", "comment", " ", "and", " ", "put", " ", "code", " ", RowBox[{"here", "."}]}], " ", "*)"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 4", "Subsubsection"], Cell["\<\ Gamma correction is a simple image processing tool that can be used to \ lighten or darken an image. The process is quite simple. Suppose A is a \ grayscale image matrix. We first divide all elements of A by 255 to obtain \ values in the interval [0, 1] and then raise each value to some positive \ exponent \[Gamma]. We then multiply each element by 255 and round the result \ to the nearest integer. In Mathematica we can do \"incorrect\" mathematical operations. If A is our \ image matrix, then A/255 makes some sense, but (A/255)^gamma actually raises \ each element of A to the gamma power. In the cell below, using the Round[ ] \ function, write code that will perform gamma correction on the image from \ Exercise 1. Call the output B. Try several values of gamma (I would suggest \ naming a variable gamma and then changing its values) and plotting the \ result. Can you characterize what different values of gamma do to the image? Feel free to load other grayscale images in the package and perform gamma \ correction on them.\ \>", "Text", CellChangeTimes->{{3.421428095203125*^9, 3.421428137953125*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "Remove", " ", "this", " ", "comment", " ", "and", " ", "put", " ", "code", " ", RowBox[{"here", "."}]}], " ", "*)"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 5", "Subsubsection", CellChangeTimes->{{3.421493495745909*^9, 3.421493497432891*^9}}], Cell["\<\ In an example above we computed the negative of a color image. In the \ original image, each pixel was represented by a linear combination of vectors \ representing red, green, and blue. For the image negative, what vectors are \ used to form the linear combination for each pixel? What are the associated \ colors?\ \>", "Text", CellChangeTimes->{{3.421493503759072*^9, 3.4214936484021235`*^9}}], Cell[BoxData[ RowBox[{"(*", " ", RowBox[{ "Remove", " ", "this", " ", "comment", " ", "and", " ", "put", " ", "code", " ", RowBox[{"here", "."}]}], " ", "*)"}]], "Input"] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Cumulative Energy", "Section", CellChangeTimes->{{3.421428300375*^9, 3.421428305953125*^9}}], Cell["\<\ One of the important skills students develop in this course is the ability to \ write modules or functions. The cumulative energy function is an excellent \ starting point for this development. \ \>", "Text", CellChangeTimes->{{3.42142831625*^9, 3.421428581828125*^9}, 3.421429171921875*^9, 3.4214295424375*^9}], Cell[CellGroupData[{ Cell["Calculating Cumulative Energy", "Subsection", CellChangeTimes->{{3.42142954565625*^9, 3.421429551046875*^9}}], Cell["\<\ To compute the cumulative energy of vector v, we sort the absolute values of \ the elements largest to smallest and then square the components of the \ resulting vector. Call this vector y. We find the kth element of the \ cumulative energy vector by summing the first k elements of y and dividing \ the result by the norm of v squared. The first two steps of this process are easy. Let's use a small vector as an \ example.\ \>", "Text", CellChangeTimes->{{3.42142831625*^9, 3.421428581828125*^9}, 3.421429171921875*^9, {3.4214295424375*^9, 3.421429558125*^9}}], Cell[BoxData[{ RowBox[{"v", "=", RowBox[{"{", RowBox[{ RowBox[{"-", "3"}], ",", " ", RowBox[{"-", "2"}], ",", " ", "0", ",", " ", "0", ",", " ", "5", ",", " ", RowBox[{"-", "8"}], ",", " ", "3", ",", " ", "1", ",", "1", ",", "2"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"Reverse", "[", RowBox[{"Sort", "[", RowBox[{"Abs", "[", "v", "]"}], "]"}], "]"}]}], "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"y", "^", "2"}]}]}], "Input", CellChangeTimes->{{3.4214285835625*^9, 3.421428670390625*^9}, { 3.421429164625*^9, 3.421429165921875*^9}, {3.421429288515625*^9, 3.42142930028125*^9}, {3.42143083834375*^9, 3.421430838484375*^9}}], Cell[TextData[{ "Next comes the cumulative sums. We could write a loop for k = 1 through 10 \ and an inner loop j = 1 to k to perform the task, but we instead encourage \ our students to take advantage of built-in functions that accomplish tasks. \ In most cases, these built-in functions are much faster than attempting to \ extract individual elements from a vector or list. For cumulative sums, the \ built-in command ", StyleBox["FoldList", FontWeight->"Bold"], " is well-suited for our needs. It needs an operation (Plus), a starting \ point (0), and a list of numbers (y)" }], "Text", CellChangeTimes->{{3.42142868615625*^9, 3.42142876425*^9}, { 3.42142880765625*^9, 3.421428811890625*^9}, {3.421429042671875*^9, 3.42142908128125*^9}, {3.421429132640625*^9, 3.421429145359375*^9}, { 3.421429180234375*^9, 3.421429182015625*^9}}], Cell[BoxData[ RowBox[{"x", "=", RowBox[{"FoldList", "[", RowBox[{"Plus", ",", "0", ",", "y"}], "]"}]}]], "Input", CellChangeTimes->{{3.421429085375*^9, 3.42142912584375*^9}, { 3.421429244390625*^9, 3.4214292453125*^9}, 3.421429366953125*^9, { 3.421430836578125*^9, 3.42143083671875*^9}}], Cell[TextData[{ "Notice that there is a small problem. Our cumulative sum list is close but \ contains an unwanted 0 at the beginning. We can get rid of the 0 by using \ the ", StyleBox["Mathematica", FontSlant->"Italic"], " command ", StyleBox["Drop", FontWeight->"Bold"], "." }], "Text", CellChangeTimes->{{3.421429189984375*^9, 3.421429233921875*^9}}], Cell[BoxData[ RowBox[{"z", "=", RowBox[{"Drop", "[", RowBox[{"x", ",", "1"}], "]"}]}]], "Input", CellChangeTimes->{{3.421429249265625*^9, 3.421429250953125*^9}, { 3.421429342296875*^9, 3.421429371671875*^9}, {3.421430834765625*^9, 3.42143083490625*^9}}], Cell["\<\ Dividing the above list by norm v squared completes the task.\ \>", "Text", CellChangeTimes->{{3.4214292785*^9, 3.421429313546875*^9}}], Cell[BoxData[ RowBox[{"ce", " ", "=", " ", RowBox[{"z", "/", RowBox[{"(", RowBox[{"v", ".", "v"}], ")"}]}]}]], "Input", CellChangeTimes->{{3.421429346359375*^9, 3.421429351046875*^9}, { 3.42143083325*^9, 3.42143083340625*^9}}], Cell[TextData[{ "We can plot the cumulative energy using the ", StyleBox["Mathematica", FontSlant->"Italic"], " command ", StyleBox["ListPlot", FontWeight->"Bold"], ". Note the x-axis is the element number of ce." }], "Text", CellChangeTimes->{{3.421429377984375*^9, 3.4214294225625*^9}, { 3.421429462546875*^9, 3.421429476765625*^9}}], Cell[BoxData[{ RowBox[{"ListPlot", "[", "ce", "]"}], "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"ce", ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"{", RowBox[{"Red", ",", RowBox[{"PointSize", "[", ".02", "]"}]}], "}"}]}]}], "]"}]}], "Input", CellChangeTimes->{{3.421429455171875*^9, 3.42142952690625*^9}, { 3.421430830921875*^9, 3.421430831171875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["The Cumulative Energy Module", "Subsection", CellChangeTimes->{{3.421429590328125*^9, 3.421429596125*^9}}], Cell[TextData[{ "In ", StyleBox["Mathematica", FontSlant->"Italic"], ", it is very easy to write modules or functions. If you have experience \ writing modules or functions, there are three important things to note: (1) \ use a Return statement to indicate what, if anything, is being returned by \ the module, (2) a local variables list is highly recommended, and (3) you \ cannot pass a module command a variable (say x) and then overwrite x within \ the module - you must make a copy of it.\n\nIn the cell below, we will write \ a module for computing the cumulative energy of a vector (or matrix!) \ together." }], "Text", CellChangeTimes->{{3.42142960221875*^9, 3.4214297696875*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"MyCE", "[", "v_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", " ", RowBox[{"(*", " ", RowBox[{"put", " ", "local", " ", "variables", " ", "here"}], " ", "*)"}], "}"}], ","}], "\[IndentingNewLine]", "]"}]}], ";"}]], "Input", CellChangeTimes->{{3.421429772625*^9, 3.421429776078125*^9}, { 3.4214298411875*^9, 3.42142986128125*^9}, {3.421430015671875*^9, 3.421430076859375*^9}, {3.421430110140625*^9, 3.421430110296875*^9}, { 3.421430148234375*^9, 3.42143014890625*^9}, 3.421495138954275*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Testing the Module", "Subsection", CellChangeTimes->{{3.421429875328125*^9, 3.42142988528125*^9}}], Cell["\<\ The cell below creates a vector of 20 random integers and then calls the MyCE \ module and the DiscreteWavelets command CE to compute the cumulative energy. \ Don't execute the cell until you have completed and executed the cell above.\ \>", "Text", CellChangeTimes->{{3.42142988934375*^9, 3.421429950484375*^9}, { 3.42142999834375*^9, 3.421429998546875*^9}, {3.42143011984375*^9, 3.42143014153125*^9}, 3.421495141642084*^9}], Cell[BoxData[{ RowBox[{"v", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RandomInteger", "[", RowBox[{"{", RowBox[{"0", ",", "10"}], "}"}], "]"}], ",", RowBox[{"{", "20", "}"}]}], "]"}]}], "\[IndentingNewLine]", RowBox[{"MyCE", "[", "v", "]"}], "\[IndentingNewLine]", RowBox[{"CE", "[", "v", "]"}]}], "Input", CellChangeTimes->{{3.421429954828125*^9, 3.42143000825*^9}, {3.4214308275*^9, 3.421430828234375*^9}, 3.42149513604769*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Subsection", CellChangeTimes->{{3.421430182453125*^9, 3.421430199328125*^9}}], Cell[CellGroupData[{ Cell["Exercise 1", "Subsubsection", CellChangeTimes->{{3.42143022953125*^9, 3.421430232265625*^9}}], Cell[TextData[{ "The MyCE module above will not work on matrices. The module is expecting a \ list of numbers and a matrix is a list of lists in ", StyleBox["Mathematica", FontSlant->"Italic"], ". How can you modify the MyCE module so that it will work on matrices? \ (Hint: Check the command ", StyleBox["Flatten", FontWeight->"Bold"], " in the Documentation Center.)" }], "Text", CellChangeTimes->{{3.421430254421875*^9, 3.42143027165625*^9}, { 3.421430342265625*^9, 3.42143041053125*^9}, {3.421495145736306*^9, 3.421495149252336*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 1M", "Subsubsection", CellChangeTimes->{{3.42143022953125*^9, 3.421430232265625*^9}, { 3.421430432171875*^9, 3.4214304336875*^9}}], Cell[TextData[{ "The MyCE module, as it's written, performs all computations symbolically. \ This is fine for short-length vectors but for longer vectors, the computation \ time increases. How can you modify the MyCE module so that it will work on \ matrices? (Hint: Check the command ", StyleBox["N", FontWeight->"Bold"], " in the Documentation Center.)" }], "Text", CellChangeTimes->{{3.421430254421875*^9, 3.42143027165625*^9}, { 3.421430342265625*^9, 3.42143041053125*^9}, {3.4214304481875*^9, 3.421430559296875*^9}, {3.4214951514713416`*^9, 3.4214951551280127`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 2", "Subsubsection", CellChangeTimes->{{3.42143022953125*^9, 3.42143024271875*^9}}], Cell["\<\ Load the grayscale image of the chess pieces from the DiscreteWavelets \ package, find its cumulative energy using the MyCE module, and then plot the \ cumulative energy of the image. How many elements of the image constitute \ 90% of the energy? How many zeros are in the image?\ \>", "Text", CellChangeTimes->{{3.4214305744375*^9, 3.42143064846875*^9}, { 3.421430710671875*^9, 3.42143072971875*^9}, 3.4214951599254403`*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 3", "Subsubsection", CellChangeTimes->{{3.42143022953125*^9, 3.421430245046875*^9}}], Cell[TextData[{ "Describe the plot of the cumulative energy of a (nonzero) constant vector. \ (Hint: If you wish, you can generate a constant vector using the ", StyleBox["ConstantArray", FontWeight->"Bold"], " command - see the Documentation Center for more information.)" }], "Text", CellChangeTimes->{{3.421430741*^9, 3.421430814515625*^9}}] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Entropy", "Section", CellChangeTimes->{{3.42144834953125*^9, 3.421448350828125*^9}}], Cell[TextData[{ "The DiscreteWavelets command for entropy is ", StyleBox["Entropy", FontWeight->"Bold"], ". The module takes either a vector or matrix as input. Here are some \ example calls:" }], "Text", CellChangeTimes->{{3.4214483785625*^9, 3.42144841728125*^9}}], Cell[BoxData[{ RowBox[{"v", "=", RowBox[{"Range", "[", "16", "]"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"Entropy", "[", "v", "]"}], ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", RowBox[{"IdentityMatrix", "[", "8", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"MatrixForm", "[", "A", "]"}], ",", "\"\< is \>\"", ",", RowBox[{"Entropy", "[", "A", "]"}], ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.42144842671875*^9, 3.421448561765625*^9}, { 3.421449231546875*^9, 3.421449231984375*^9}}], Cell[CellGroupData[{ Cell["Exercises", "Subsection", CellChangeTimes->{{3.421448580109375*^9, 3.421448581125*^9}}], Cell[CellGroupData[{ Cell["Exercise 1 (Challenge)", "Subsubsection", CellChangeTimes->{{3.421448587578125*^9, 3.421448599765625*^9}}], Cell[TextData[{ "Write a module that will compute the entropy of a vector or matrix. Hint: \ Commands that will be useful are ", StyleBox["Map", FontWeight->"Bold"], ", ", StyleBox["Length", FontWeight->"Bold"], ", ", StyleBox["Sort", FontWeight->"Bold"], ", ", StyleBox["Split", FontWeight->"Bold"], ", ", StyleBox["Flatten", FontWeight->"Bold"], ", and ", StyleBox["Log", FontWeight->"Bold"], ". See the Documentation Center." }], "Text", CellChangeTimes->{{3.421448602203125*^9, 3.42144862403125*^9}, { 3.421449006453125*^9, 3.4214490630625*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"MyEntropy", "[", "v_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"(*", " ", RowBox[{"Place", " ", "local", " ", "variables", " ", "here"}], " ", "*)"}], "}"}], ","}], "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"Place", " ", "commands", " ", "here"}], " ", "*)"}], "\[IndentingNewLine]", "]"}]}], ";"}]], "Input", CellChangeTimes->{{3.42144906565625*^9, 3.421449203046875*^9}, 3.421495791069819*^9}], Cell["Use the cell below to test your code.", "Text", CellChangeTimes->{{3.421449205578125*^9, 3.421449211453125*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"v", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RandomInteger", "[", RowBox[{"{", RowBox[{"0", ",", "10"}], "}"}], "]"}], ",", RowBox[{"{", "10", "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Entropy", "[", "v", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"MyEntropy", "[", "v", "]"}], "\n"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RandomInteger", "[", RowBox[{"{", RowBox[{"0", ",", "10"}], "}"}], "]"}], ",", RowBox[{"{", "8", "}"}], ",", RowBox[{"{", "8", "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Entropy", "[", "A", "]"}], "\[IndentingNewLine]", RowBox[{"MyEntropy", "[", "A", "]"}]}], "Input", CellChangeTimes->{{3.42144906565625*^9, 3.421449219484375*^9}, { 3.421495793663519*^9, 3.4214957955541077`*^9}}] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["PSNR", "Section", CellChangeTimes->{{3.42144835946875*^9, 3.42144836034375*^9}}], Cell[TextData[{ "The DiscreteWavelets command for peak signal-to-noise ratio is ", StyleBox["PSNR", FontWeight->"Bold"], ". The input values are two matrices of equal size." }], "Text", CellChangeTimes->{{3.421449322328125*^9, 3.421449329359375*^9}, { 3.421451066796875*^9, 3.42145110771875*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"img", "=", RowBox[{"ImageNames", "[", RowBox[{ RowBox[{"ImageType", "\[Rule]", "GrayScale"}], ",", RowBox[{"ListThumbnails", "\[Rule]", "True"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", RowBox[{"ImageRead", "[", RowBox[{"img", "[", RowBox[{"[", "4", "]"}], "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"rows", ",", "cols"}], "}"}], "=", RowBox[{"Dimensions", "[", "A", "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A1", "=", RowBox[{"A", "+", RowBox[{"Table", "[", RowBox[{ RowBox[{"RandomInteger", "[", RowBox[{"{", RowBox[{ RowBox[{"-", "5"}], ",", "5"}], "}"}], "]"}], ",", RowBox[{"{", "rows", "}"}], ",", RowBox[{"{", "cols", "}"}]}], "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A2", "=", RowBox[{"A", "+", RowBox[{"Table", "[", RowBox[{ RowBox[{"RandomInteger", "[", RowBox[{"{", RowBox[{ RowBox[{"-", "20"}], ",", "20"}], "}"}], "]"}], ",", RowBox[{"{", "rows", "}"}], ",", RowBox[{"{", "cols", "}"}]}], "]"}]}]}], ";"}]}], "Input", CellChangeTimes->{{3.42145113659375*^9, 3.421451269546875*^9}, { 3.421451331890625*^9, 3.42145135959375*^9}, {3.4214514215*^9, 3.421451444875*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"p1", "=", RowBox[{"ImagePlot", "[", "A", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"p2", "=", RowBox[{"ImagePlot", "[", "A1", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"p3", "=", RowBox[{"ImagePlot", "[", "A2", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"GraphicsGrid", "[", RowBox[{"{", RowBox[{"{", RowBox[{"p1", ",", "p2", ",", "p3"}], "}"}], "}"}], "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"psnr1", "=", RowBox[{"PSNR", "[", RowBox[{"A", ",", "A1"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"psnr2", "=", RowBox[{"PSNR", "[", RowBox[{"A", ",", "A2"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "psnr1", ",", "\"\< and the PSNR for A and A2 is \>\"", ",", "psnr2", ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.421451232359375*^9, 3.421451241078125*^9}, { 3.421451276609375*^9, 3.421451314515625*^9}, {3.421451374875*^9, 3.421451411390625*^9}, {3.4214514415*^9, 3.421451441875*^9}, { 3.42145148309375*^9, 3.4214514873125*^9}}], Cell[CellGroupData[{ Cell["Exercises", "Subsection", CellChangeTimes->{{3.421451447140625*^9, 3.421451448234375*^9}}], Cell[CellGroupData[{ Cell["Exercise 1", "Subsubsection", CellChangeTimes->{{3.421451453359375*^9, 3.421451455484375*^9}}], Cell[TextData[{ "Write a module to compute the PSNR of two matrices. Useful ", StyleBox["Mathematica", FontSlant->"Italic"], " commands are ", StyleBox["Flatten", FontWeight->"Bold"], ", ", StyleBox["Total", FontWeight->"Bold"], ", and ", StyleBox["Log", FontWeight->"Bold"], "." }], "Text", CellChangeTimes->{{3.42145146175*^9, 3.42145146871875*^9}, { 3.42145152715625*^9, 3.421451540359375*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{ RowBox[{"MyPSNR", "[", RowBox[{"a_", ",", "b_"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{"{", RowBox[{"(*", " ", RowBox[{"put", " ", "local", " ", "variables", " ", "here"}], " ", "*)"}], ",", "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"put", " ", "commands", " ", "here"}], " ", "*)"}], "\[IndentingNewLine]"}]}]}], "]"}], ";"}]], "Input", CellChangeTimes->{{3.421451551625*^9, 3.421451574875*^9}, 3.42149671401784*^9}], Cell["Here is some code to test your module :", "Text", CellChangeTimes->{{3.421451580375*^9, 3.421451588875*^9}}], Cell[BoxData[{ RowBox[{"mypsnr1", "=", RowBox[{"MyPSNR", "[", RowBox[{"A", ",", "A1"}], "]"}]}], "\[IndentingNewLine]", RowBox[{"mypsnr2", "=", RowBox[{"MyPSNR", "[", RowBox[{"A", ",", "A1"}], "]"}]}]}], "Input", CellChangeTimes->{{3.42145160178125*^9, 3.42145161665625*^9}, { 3.4214967155022054`*^9, 3.421496716642823*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 2", "Subsubsection", CellChangeTimes->{{3.42145267846875*^9, 3.421452681390625*^9}}], Cell["\<\ How do you suppose we compute the PSNR between two color images? Load the \ Legos image from the DiscreteWavelets package and add matrices of random \ integers ranging from -10 to 10 to each channel. Then compute the PSNR \ between the original image and the \"noisy\" one.\ \>", "Text", CellChangeTimes->{{3.421452687984375*^9, 3.42145276534375*^9}}], Cell[BoxData[ RowBox[{"(*", " ", RowBox[{"put", " ", "your", " ", "code", " ", "here"}], " ", "*)"}]], "Input", CellChangeTimes->{{3.42145277075*^9, 3.42145277446875*^9}}] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Huffman Coding", "Section", CellChangeTimes->{{3.421448365703125*^9, 3.42144836790625*^9}}], Cell[CellGroupData[{ Cell["MakeHuffmanCodes", "Subsection", CellChangeTimes->{{3.4214523053125*^9, 3.421452307875*^9}}], Cell["\<\ The DiscreteWavelets module MakeHuffmanCodes can be used to generate Huffman \ codes for a list of integers, a string, or a matrix of integers. Note that \ integer input must be nonnegative. The routine returns three pieces of information. The first is a list of \ triples. Each triple consists of an integer or character that appeared in \ the input, its relative frequency in the input, and its Huffman code. The \ second output is the original bitstream length and the third output is the \ new bitstream length.\ \>", "Text", CellChangeTimes->{{3.42145173325*^9, 3.42145177953125*^9}, { 3.42145186346875*^9, 3.42145196303125*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"codes", ",", "origlen", ",", "newlen"}], "}"}], "=", RowBox[{"MakeHuffmanCodes", "[", "\"\\"", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "codes", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "origlen", ",", " ", "\"\< and the new bitstream length is \>\"", ",", "newlen", ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.421451636859375*^9, 3.421451643765625*^9}, { 3.421451698828125*^9, 3.42145170846875*^9}, 3.42145178834375*^9, { 3.42145196678125*^9, 3.421452049109375*^9}}], Cell["We can also find the Huffman codes of an image:", "Text", CellChangeTimes->{{3.4214522941875*^9, 3.421452317640625*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"img", "=", RowBox[{"ImageNames", "[", RowBox[{ RowBox[{"ImageType", "\[Rule]", "GrayScale"}], ",", RowBox[{"ListThumbnails", "\[Rule]", "True"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", RowBox[{"ImageRead", "[", RowBox[{"img", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"ImagePlot", "[", "A", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"codes", ",", "origlen", ",", "newlen"}], "}"}], "=", RowBox[{"MakeHuffmanCodes", "[", "A", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "origlen", ",", "\"\< and the new bitstream length is \>\"", ",", "newlen", ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.42145231940625*^9, 3.42145239225*^9}, { 3.421452574234375*^9, 3.421452574375*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["HuffmanTree", "Subsection", CellChangeTimes->{{3.421452415953125*^9, 3.4214524175*^9}}], Cell[TextData[{ "For small input, we can use ", StyleBox["HuffmanTree", FontWeight->"Bold"], " to visualize the output of ", StyleBox["MakeHuffmanCodes", FontWeight->"Bold"], ". The input of ", StyleBox["HuffmanTree", FontWeight->"Bold"], " is the first output of ", StyleBox["MakeHuffmanCodes", FontWeight->"Bold"], ". There are several graphics options for ", StyleBox["HuffmanTree", FontWeight->"Bold"], ". See the Documentation Center for more information." }], "Text", CellChangeTimes->{{3.42145242296875*^9, 3.421452504625*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"codes", ",", "origlen", ",", "newlen"}], "}"}], "=", RowBox[{"MakeHuffmanCodes", "[", "\"\\"", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "codes", "]"}], "\[IndentingNewLine]", RowBox[{"HuffmanTree", "[", "codes", "]"}]}], "Input", CellChangeTimes->{{3.42145252715625*^9, 3.421452570078125*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Subsection", CellChangeTimes->{{3.421452655984375*^9, 3.421452658140625*^9}}], Cell[CellGroupData[{ Cell["Exercise 1", "Subsubsection", CellChangeTimes->{{3.421452668578125*^9, 3.42145266946875*^9}, { 3.4214527864375*^9, 3.4214527865*^9}}], Cell["\<\ Load the surfer image (small version) from the DiscreteWavelets package and \ find the bits per pixel that results from the Huffman coded version of the \ image.\ \>", "Text", CellChangeTimes->{{3.42145279340625*^9, 3.421452815484375*^9}, { 3.42145294334375*^9, 3.4214529630625*^9}}], Cell[BoxData[ RowBox[{"(*", " ", RowBox[{"put", " ", "your", " ", "code", " ", "here"}], " ", "*)"}]], "Input", CellChangeTimes->{{3.42145296796875*^9, 3.42145297325*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise 2 (Challenge)", "Subsubsection", CellChangeTimes->{{3.42145297578125*^9, 3.421452978171875*^9}, { 3.4214532173125*^9, 3.421453219765625*^9}}], Cell[TextData[{ "Suppose you pass a string ", StyleBox["s", FontWeight->"Bold"], " to MakeHuffmanCodes. Write a module that will take the codes returned by \ MakeHuffmanCodes and s and return the new bitstream. Call the module ", StyleBox["MakeBitstream", FontWeight->"Bold"], ".\n\nFor example:" }], "Text", CellChangeTimes->{{3.421453221859375*^9, 3.42145331728125*^9}, { 3.421453409640625*^9, 3.421453419328125*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"s", "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"codes", ",", "origlen", ",", "newlen"}], "}"}], "=", RowBox[{"MakeHuffmanCodes", "[", "s", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"HuffmanTree", "[", "codes", "]"}]}], "Input", CellChangeTimes->{{3.42145331946875*^9, 3.421453355625*^9}, { 3.421453462390625*^9, 3.4214534625*^9}}], Cell["\<\ Using the tree above and s, we see that the new bitstream for \"boohoo\" is \ 01110011.\ \>", "Text", CellChangeTimes->{{3.42145336553125*^9, 3.42145340290625*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{ RowBox[{"MakeBitStream", "[", RowBox[{"s_", ",", "codes_"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{"{", RowBox[{"(*", " ", RowBox[{"put", " ", "local", " ", "variables", " ", "here"}], " ", "*)"}], ",", "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"put", " ", "code", " ", "here"}], " ", "*)"}], "\[IndentingNewLine]"}]}]}], "]"}], ";"}]], "Input", CellChangeTimes->{{3.421453432890625*^9, 3.421453454046875*^9}}] }, Open ]] }, Open ]] }, Closed]] }, Open ]] }, AutoGeneratedPackage->None, ScreenStyleEnvironment->"Presentation", WindowSize->{1272, 683}, WindowMargins->{{-3, Automatic}, {Automatic, 0}}, FrontEndVersion->"6.0 for Microsoft Windows (32-bit) (April 20, 2007)", StyleDefinitions->FrontEnd`FileName[{"Creative"}, "NaturalColor.nb", CharacterEncoding -> "WindowsANSI"] ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 110, 1, 116, "Title"], Cell[703, 26, 149, 5, 161, "Subtitle"], Cell[855, 33, 158, 6, 108, "Subsubtitle"], Cell[CellGroupData[{ Cell[1038, 43, 87, 1, 107, "Section"], Cell[CellGroupData[{ Cell[1150, 48, 32, 0, 61, "Subsection"], Cell[1185, 50, 411, 7, 89, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[1633, 62, 38, 0, 61, "Subsection"], Cell[1674, 64, 288, 7, 64, "Text"], Cell[1965, 73, 143, 2, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2145, 80, 46, 0, 61, "Subsection"], Cell[2194, 82, 377, 6, 63, "Text"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[2620, 94, 97, 1, 107, "Section"], Cell[CellGroupData[{ Cell[2742, 99, 51, 0, 61, "Subsection"], Cell[2796, 101, 264, 5, 63, "Text"], Cell[CellGroupData[{ Cell[3085, 110, 98, 1, 39, "Subsubsection"], Cell[3186, 113, 360, 10, 63, "Text"], Cell[3549, 125, 187, 4, 57, "Input"], Cell[3739, 131, 180, 4, 57, "Input"], Cell[3922, 137, 121, 2, 57, "Input"], Cell[4046, 141, 256, 9, 37, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[4339, 155, 100, 1, 39, "Subsubsection"], Cell[4442, 158, 364, 8, 63, "Text"], Cell[4809, 168, 157, 4, 57, "Input"], Cell[4969, 174, 665, 19, 193, "Text"], Cell[5637, 195, 256, 6, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5930, 206, 103, 1, 39, "Subsubsection"], Cell[6036, 209, 510, 17, 63, "Text"], Cell[6549, 228, 226, 4, 57, "Input"], Cell[6778, 234, 219, 4, 57, "Input"], Cell[7000, 240, 104, 2, 57, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[7153, 248, 45, 0, 61, "Subsection"], Cell[7201, 250, 581, 14, 141, "Text"], Cell[7785, 266, 549, 14, 126, "Input"], Cell[8337, 282, 622, 16, 126, "Input"], Cell[8962, 300, 43, 0, 37, "Text"], Cell[9008, 302, 156, 4, 57, "Input"], Cell[9167, 308, 43, 0, 37, "Text"], Cell[9213, 310, 157, 4, 57, "Input"], Cell[9373, 316, 46, 0, 37, "Text"], Cell[9422, 318, 193, 5, 57, "Input"], Cell[9618, 325, 765, 23, 117, "Text"], Cell[10386, 350, 817, 25, 126, "Input"], Cell[11206, 377, 531, 14, 92, "Input"], Cell[11740, 393, 509, 9, 115, "Text"], Cell[12252, 404, 372, 10, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[12661, 419, 46, 0, 61, "Subsection"], Cell[12710, 421, 334, 11, 89, "Text"], Cell[13047, 434, 146, 3, 57, "Input"], Cell[13196, 439, 148, 3, 57, "Input"], Cell[13347, 444, 217, 5, 57, "Input"], Cell[13567, 451, 261, 7, 63, "Text"], Cell[13831, 460, 716, 18, 194, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[14584, 483, 133, 1, 61, "Subsection"], Cell[14720, 486, 389, 7, 63, "Text"], Cell[15112, 495, 520, 14, 92, "Input"], Cell[15635, 511, 507, 9, 89, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[16179, 525, 41, 0, 61, "Subsection"], Cell[16223, 527, 151, 5, 89, "Text"], Cell[16377, 534, 243, 7, 57, "Input"], Cell[16623, 543, 32, 0, 37, "Text"], Cell[16658, 545, 173, 4, 57, "Input"], Cell[16834, 551, 30, 0, 37, "Text"], Cell[16867, 553, 173, 4, 57, "Input"], Cell[17043, 559, 30, 0, 37, "Text"], Cell[17076, 561, 244, 6, 57, "Input"], Cell[17323, 569, 35, 0, 37, "Text"], Cell[17361, 571, 175, 4, 57, "Input"], Cell[17539, 577, 43, 0, 37, "Text"], Cell[17585, 579, 211, 4, 57, "Input"], Cell[17799, 585, 193, 4, 63, "Text"], Cell[17995, 591, 181, 5, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[18213, 601, 60, 0, 61, "Subsection"], Cell[18276, 603, 336, 8, 63, "Text"], Cell[18615, 613, 761, 21, 160, "Input"], Cell[19379, 636, 338, 10, 63, "Text"], Cell[19720, 648, 177, 6, 37, "Text"], Cell[19900, 656, 459, 13, 92, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[20396, 674, 31, 0, 61, "Subsection"], Cell[CellGroupData[{ Cell[20452, 678, 35, 0, 39, "Subsubsection"], Cell[20490, 680, 137, 3, 37, "Text"], Cell[20630, 685, 201, 6, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[20868, 696, 35, 0, 39, "Subsubsection"], Cell[20906, 698, 243, 4, 63, "Text"], Cell[21152, 704, 201, 6, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[21390, 715, 35, 0, 39, "Subsubsection"], Cell[21428, 717, 857, 18, 271, "Text"], Cell[22288, 737, 201, 6, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[22526, 748, 35, 0, 39, "Subsubsection"], Cell[22564, 750, 1140, 21, 323, "Text"], Cell[23707, 773, 201, 6, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[23945, 784, 101, 1, 39, "Subsubsection"], Cell[24049, 787, 410, 7, 89, "Text"], Cell[24462, 796, 184, 5, 57, "Input"] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[24707, 808, 99, 1, 69, "Section"], Cell[24809, 811, 327, 6, 63, "Text"], Cell[CellGroupData[{ Cell[25161, 821, 116, 1, 61, "Subsection"], Cell[25280, 824, 582, 12, 167, "Text"], Cell[25865, 838, 706, 17, 126, "Input"], Cell[26574, 857, 850, 15, 115, "Text"], Cell[27427, 874, 302, 6, 57, "Input"], Cell[27732, 882, 366, 11, 64, "Text"], Cell[28101, 895, 267, 6, 57, "Input"], Cell[28371, 903, 146, 3, 37, "Text"], Cell[28520, 908, 242, 6, 57, "Input"], Cell[28765, 916, 348, 10, 38, "Text"], Cell[29116, 928, 397, 9, 92, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[29550, 942, 113, 1, 61, "Subsection"], Cell[29666, 945, 695, 13, 168, "Text"], Cell[30364, 960, 598, 13, 92, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[30999, 978, 105, 1, 61, "Subsection"], Cell[31107, 981, 444, 7, 63, "Text"], Cell[31554, 990, 471, 11, 126, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[32062, 1006, 97, 1, 61, "Subsection"], Cell[CellGroupData[{ Cell[32184, 1011, 100, 1, 39, "Subsubsection"], Cell[32287, 1014, 555, 13, 64, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[32879, 1032, 148, 2, 39, "Subsubsection"], Cell[33030, 1036, 585, 11, 89, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[33652, 1052, 99, 1, 39, "Subsubsection"], Cell[33754, 1055, 441, 7, 89, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[34232, 1067, 100, 1, 39, "Subsubsection"], Cell[34335, 1070, 351, 7, 63, "Text"] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[34747, 1084, 91, 1, 69, "Section"], Cell[34841, 1087, 274, 7, 37, "Text"], Cell[35118, 1096, 776, 19, 194, "Input"], Cell[CellGroupData[{ Cell[35919, 1119, 94, 1, 61, "Subsection"], Cell[CellGroupData[{ Cell[36038, 1124, 113, 1, 39, "Subsubsection"], Cell[36154, 1127, 581, 23, 63, "Text"], Cell[36738, 1152, 534, 14, 126, "Input"], Cell[37275, 1168, 119, 1, 37, "Text"], Cell[37397, 1171, 923, 24, 262, "Input"] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[38381, 1202, 87, 1, 69, "Section"], Cell[38471, 1205, 305, 7, 37, "Text"], Cell[38779, 1214, 1454, 44, 228, "Input"], Cell[40236, 1260, 1210, 32, 296, "Input"], Cell[CellGroupData[{ Cell[41471, 1296, 97, 1, 61, "Subsection"], Cell[CellGroupData[{ Cell[41593, 1301, 101, 1, 39, "Subsubsection"], Cell[41697, 1304, 418, 16, 38, "Text"], Cell[42118, 1322, 550, 15, 126, "Input"], Cell[42671, 1339, 115, 1, 37, "Text"], Cell[42789, 1342, 344, 8, 92, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[43170, 1355, 100, 1, 39, "Subsubsection"], Cell[43273, 1358, 364, 6, 63, "Text"], Cell[43640, 1366, 179, 4, 57, "Input"] }, Open ]] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[43880, 1377, 98, 1, 69, "Section"], Cell[CellGroupData[{ Cell[44003, 1382, 99, 1, 61, "Subsection"], Cell[44105, 1385, 651, 12, 167, "Text"], Cell[44759, 1399, 711, 16, 160, "Input"], Cell[45473, 1417, 127, 1, 37, "Text"], Cell[45603, 1420, 1000, 27, 228, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[46640, 1452, 94, 1, 61, "Subsection"], Cell[46737, 1455, 559, 18, 63, "Text"], Cell[47299, 1475, 408, 9, 126, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[47744, 1489, 97, 1, 61, "Subsection"], Cell[CellGroupData[{ Cell[47866, 1494, 142, 2, 39, "Subsubsection"], Cell[48011, 1498, 296, 6, 63, "Text"], Cell[48310, 1506, 179, 4, 57, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[48526, 1515, 159, 2, 39, "Subsubsection"], Cell[48688, 1519, 433, 11, 115, "Text"], Cell[49124, 1532, 446, 11, 126, "Input"], Cell[49573, 1545, 175, 4, 37, "Text"], Cell[49751, 1551, 538, 14, 126, "Input"] }, Open ]] }, Open ]] }, Closed]] }, Open ]] } ] *) (* End of internal cache information *)