(************** 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[ 17071, 537]*) (*NotebookOutlinePosition[ 17837, 563]*) (* CellTagsIndexPosition[ 17793, 559]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Haar Image Compression", "Title"], Cell["\<\ Wavelet Workshop June 6-9, 2007 University of St. Thomas\ \>", "Subtitle"], Cell["Objectives", "Subsubtitle"], Cell[TextData[{ "The purpose of this notebook is to show you how to perform naive image \ compression using the two-dimensional Discrete Haar Wavelet Transform.\n\nThe \ notebook also contains a color image compression lab that makes up part of ", StyleBox["Computer Session Three", FontWeight->"Bold"], "." }], "Text"], Cell[CellGroupData[{ Cell["DiscreteWavelets", "Section"], Cell[TextData[{ StyleBox["You should run this cell each time you open this notebook!!", FontColor->RGBColor[1, 0, 0]], " It loads the ", StyleBox["Mathematica", FontSlant->"Italic"], " package ", StyleBox["DiscreteWavelets", FontFamily->"Courier"], " for use in subsequent computations." }], "Text"], Cell[BoxData[ StyleBox[\(<< DiscreteWavelets`DiscreteWavelets`\), FontColor->GrayLevel[0.500008]]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Help on DiscreteWavelets", "Section"], Cell[TextData[{ "If you ever need help with ", StyleBox["DiscreteWavelets", FontFamily->"Courier"], ", go to ", StyleBox["Help", FontSlant->"Italic"], ", then ", StyleBox["Help Browser", FontSlant->"Italic"], ", and click on ", StyleBox["AddOns & Links", FontSlant->"Italic"], ". If you scroll down you will find ", StyleBox["DiscreteWavelets", FontFamily->"Courier"], ". " }], "Text"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Load an Image and Compute the Modified Haar Wavelet Transform\ \>", "Section"], Cell["\<\ We begin by loading an image and computing its Haar Wavelet Transform. Note \ that we are altering the filter slightly - we'll have to compensate when we \ compute inverse transforms.\ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"gray", "=", RowBox[{ StyleBox["ImageNames", FontColor->GrayLevel[0.500008]], "[", \(ImageType \[Rule] GrayScale, ListThumbnails \[Rule] True\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", RowBox[{ StyleBox["ImageRead", FontColor->GrayLevel[0.500008]], "[", \(gray[\([5]\)]\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", "A", "]"}], ";"}], "\[IndentingNewLine]", \({rows, cols} = Dimensions[A];\)}], "Input"], Cell[TextData[{ "We next compute three iterations of the HWT. Since we wish to alter the \ filter, we will use the more general ", StyleBox["WT2D", FontWeight->"Bold"], " module. This module lets us pass the matrix, the number of iterations \ (as a directive), and a filter. We can load the filter by using the command \ ", StyleBox["Haar[ ]", FontWeight->"Bold"], " ." }], "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"h", "=", RowBox[{ RowBox[{"N", "[", StyleBox[\(Haar[]\), FontColor->GrayLevel[0.500008]], "]"}], "*", \(Sqrt[2]\)}]}], ";"}], "\[IndentingNewLine]", RowBox[{\(Print["\", h, "\<.\>"];\), "\[IndentingNewLine]"}], "\[IndentingNewLine]", \(i = 3;\), "\[IndentingNewLine]", RowBox[{ RowBox[{"wt", "=", RowBox[{ StyleBox["WT2D", FontColor->GrayLevel[0.500008]], "[", \(A, h, NumIterations \[Rule] i\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["WaveletDensityPlot", FontColor->GrayLevel[0.500008]], "[", \(wt, NumIterations \[Rule] i, DivideLines \[Rule] True, DivideLinesColor \[Rule] Coral\), "]"}], ";"}]}], "Input"], Cell[TextData[{ "Let's build the histogram of the image. The ", StyleBox["Table", FontWeight->"Bold"], " loops through all possible pixel values. The ", StyleBox["Flatten", FontWeight->"Bold"], " turns ", StyleBox["A", FontWeight->"Bold"], " into a vector (of length 38400) and ", StyleBox["Select", FontWeight->"Bold"], " makes lists where the elements in the vector satisfy the given criteria - \ in this case, the number is equal to the current ", StyleBox["k", FontWeight->"Bold"], ". \n\nThe rule ", StyleBox["# == k &", FontWeight->"Bold"], " is a bit confusing - think of the ", StyleBox["#", FontWeight->"Bold"], " as a dummy element, ", StyleBox["==", FontWeight->"Bold"], " (two equal signs) as a comparison, and the ", StyleBox["&", FontWeight->"Bold"], " says to apply it to everything in ", StyleBox["Flatten[A]", FontWeight->"Bold"], "." }], "Text"], Cell[BoxData[{ \(\(t = Table[Length[Select[Flatten[A], # \[Equal] k &]], {k, 0, 255}];\)\[IndentingNewLine]\), "\[IndentingNewLine]", \(\(Histogram[t, FrequencyData \[Rule] True, Ticks \[Rule] {{0, 64, 128, 196, 255}, {200, 400, 600, 800, 1000}}, BarStyle \[Rule] Maroon, BarEdges \[Rule] False];\)\)}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Quantize the Transform", "Section"], Cell["Next we compute and plot the cumulative energy. ", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"ce", "=", RowBox[{ StyleBox["CE", FontColor->GrayLevel[0.500008]], "[", "wt", "]"}]}], ";"}], "\[IndentingNewLine]", \(ListPlot[ce, PlotStyle \[Rule] Brown];\)}], "Input"], Cell["\<\ The choice of 99.99% was quite arbitrary - feel free to change the \ percentage.\ \>", "Text"], Cell[BoxData[{\(pct = .9999;\), "\[IndentingNewLine]", RowBox[{ RowBox[{"k", "=", RowBox[{ StyleBox["nCE", FontColor->GrayLevel[0.500008]], "[", \(ce, pct\), "]"}]}], ";"}], "\[IndentingNewLine]", \(Print["\", 100*pct, "\<% of the energy, we retain the largest (in modulus) \>", k, "\< values of the transform and set the remaining \>", rows*cols - k, "\< to 0.\>"];\)}], "Input"], Cell["Now quantize:", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"newwt", "=", RowBox[{ StyleBox["Comp", FontColor->GrayLevel[0.500008]], "[", \(wt, k\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["WaveletDensityPlot", FontColor->GrayLevel[0.500008]], "[", \(newwt, NumIterations \[Rule] i, DivideLines \[Rule] True, DivideLinesColor \[Rule] Maroon\), "]"}], ";"}]}], "Input"], Cell["Here is a plot of the error:", "Text"], Cell[BoxData[ RowBox[{ RowBox[{ StyleBox["WaveletDensityPlot", FontColor->GrayLevel[0.500008]], "[", \(Abs[wt - newwt], NumIterations \[Rule] i, DivideLines \[Rule] True, DivideLinesColor \[Rule] Maroon\), "]"}], ";"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Encoding the transform", "Section"], Cell["\<\ The final step is to make the Huffman codes for the quantized transform. The \ code I'm using is from a student and runs REALLY slow! The code needs all values to be nonnegative integers, so we shift the data by \ the minimum value in the quantized matrix. While we have been using the {1., 1.} filter, the computations are integers \ but done numerically. Thus we round the shifted data before encoding. Finally, the routine needs a vector not a matrix, so we have to Flatten the \ input. Here we go. Get comfortable - this takes a while...\ \>", "Text"], Cell[BoxData[{\(m = Min[Flatten[newwt]];\), "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"bits", "=", RowBox[{ StyleBox["MakeHuffmanCodes", FontColor->GrayLevel[0.500008]], "[", \(Round[Flatten[newwt - m]]\), "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", \(Print["\", bits[\([2]\)], \ "\", bits[\([3]\)], "\<.\>"];\)}], "Input"], Cell[BoxData[{ RowBox[{\(Print["\", N[bits[\([3]\)]/\((rows*cols)\)], "\<.\>"];\), "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ent", "=", RowBox[{"N", "[", RowBox[{ StyleBox["Entropy", FontColor->GrayLevel[0.500008]], "[", \(Flatten[Round[newwt]]\), "]"}], "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", \(Print["\", ent, "\<.\>"];\)}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Invert the Quantized Transform", "Section"], Cell[TextData[{ "Finally, we invert the quantized transform. Remember, we multiplied the \ Haar filter by ", Cell[BoxData[ \(\@2\)]], " when computing the transform, so now we have to divide the inverse Haar \ transform by ", Cell[BoxData[ \(\@2\)]], " (a good application of another property of matrix inverses.) We plot the \ original as well for comparative purposes." }], "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"newA", "=", RowBox[{ StyleBox["IWT2D", FontColor->GrayLevel[0.500008]], "[", \(newwt, N[Haar[]/Sqrt[2]], NumIterations \[Rule] i\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", "newA", "]"}], ";"}]}], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", "A", "]"}], ";"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Section"], Cell[TextData[{ "There are two exercises that. The first is experimental in nature testing \ different parameters in the example above. The second involves color image \ compression.\n\n", StyleBox["If you are not so familiar with ", FontWeight->"Bold"], StyleBox["Mathematica", FontWeight->"Bold", FontSlant->"Italic"], StyleBox[" or would just prefer to work with a partner, I strongly \ encourage it.", FontWeight->"Bold"], "\n" }], "Text"], Cell[CellGroupData[{ Cell["Exercise One", "Subsection"], Cell["\<\ The entropy for the quantized wavelet transform above is roughly 4.12. The \ bpp for our compression scheme (3 iterations of the wavelet transform and \ 99.99% threshold for the energy) was about 4.21. Can you change the \ parameters (iterations and/or threshold percentage) to achieve a bpp of 2? \ If so, how does the compressed image look? I would suggest you copy and paste code from above in the cell below and \ simply experiment with different values of i and/or the threshold percentage.\ \ \>", "Text"], Cell[BoxData[ \( (*\ Put\ your\ Mathematica\ code\ here\ *) \)], "Input", FontColor->RGBColor[0, 0, 1]] }, Open ]], Cell[CellGroupData[{ Cell["Exercise Two", "Subsection"], Cell[TextData[{ "In this task, you will be asked to use the ideas presented above to \ compress a color image.\n\nThe algorithm for compressing a color image is \ pretty much the same as the one for a grayscale image - it's just three times \ the work!\n\nThe first step is to read the color image into R, G, and B \ matrices and then convert to YCbCr. The routine ", StyleBox["RGBToYCbCr", FontWeight->"Bold"], " will be useful in this endeavor. \n\nAfter you have the Y, Cb, and Cr \ channels, you simply employ the image compression algorithm we used for \ grayscale images on each individual channels. When you are done, use the \ routine ", StyleBox["YCbCrToRGB", FontWeight->"Bold"], " to convert back to R,G,B.\n\nI would encourage cutting and pasting from \ above. Feel free to try different percentages for different channels when \ quantizing. You might also want to try different numbers of iterations to \ see if it makes a difference.\n\nIf you finish early, repeat the exercise but \ DO NOT first convert from RGB space to YCbCr space. Try to go for a low bpp \ and see what happens to the compressed image." }], "Text"], Cell[CellGroupData[{ Cell["To Help You Get Started:", "Subsubsection"], Cell["\<\ In the cell below, I have read in the image and done the conversion for you \ to get you started. Note that the way I've read the image, you can do at most 4 iterations of the \ wavelet transform.\ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"color", "=", RowBox[{ StyleBox["ImageNames", FontColor->GrayLevel[0.500008]], "[", \(ImageType \[Rule] Color, ListThumbnails \[Rule] True\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{\({r, g, b}\), "=", RowBox[{ StyleBox["ImageRead", FontColor->GrayLevel[0.500008]], "[", \(color[\([7]\)]\), "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{\({rows, cols} = Dimensions[r];\), "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", \({r, g, b}\), "]"}], ";"}]}], "Input"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{\({Y, Cb, Cr}\), " ", "=", " ", RowBox[{ StyleBox["RGBToYCbCr", FontColor->GrayLevel[0.500008]], "[", \({r, g, b}, DisplayMode \[Rule] True\), "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", \(Y, LinearScaling \[Rule] True\), "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", \(Cb, LinearScaling \[Rule] True\), "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", \(Cr, LinearScaling \[Rule] True\), "]"}], ";"}]}], "Input"], Cell[BoxData[ \( (*\ Put\ your\ Mathematica\ code\ here\ *) \)], "Input", FontColor->RGBColor[0, 0, 1]] }, Open ]], Cell[CellGroupData[{ Cell["Converting Back to RGB Space", "Subsubsection"], Cell["\<\ The conversion routine YCbCrtoRGB is needs some refining. Assuming you have \ named your compressed Y, Cb, Cr channels newY, newCb, and newCr, \ respectively, the command below will successfully convert them back to RGB \ space. The DisplayMode directive helps the YCbCrtoRGB module convert to \ something \"viewable\".\ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{\({newr, newg, newb}\), "=", RowBox[{"Round", "[", RowBox[{ StyleBox["YCbCrToRGB", FontColor->GrayLevel[0.500008]], "[", \({newY, newCb, newCr}, DisplayMode \[Rule] True\), "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["ImagePlot", FontColor->GrayLevel[0.500008]], "[", \({newr, newg, newb}\), "]"}], ";"}]}], "Input"] }, Open ]] }, Open ]] }, Open ]] }, Open ]] }, FrontEndVersion->"5.2 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 685}}, AutoGeneratedPackage->None, ScreenStyleEnvironment->"Presentation", WindowSize->{1012, 651}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, ShowSelection->True, StyleDefinitions -> "Report.nb" ] (******************************************************************* 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, 117, "Title"], Cell[1818, 55, 84, 4, 143, "Subtitle"], Cell[1905, 61, 33, 0, 67, "Subsubtitle"], Cell[1941, 63, 329, 7, 106, "Text"], Cell[CellGroupData[{ Cell[2295, 74, 35, 0, 96, "Section"], Cell[2333, 76, 324, 10, 63, "Text"], Cell[2660, 88, 118, 2, 51, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2815, 95, 43, 0, 96, "Section"], Cell[2861, 97, 429, 17, 64, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[3327, 119, 88, 2, 96, "Section"], Cell[3418, 123, 208, 4, 62, "Text"], Cell[3629, 129, 692, 19, 129, "Input"], Cell[4324, 150, 402, 11, 84, "Text"], Cell[4729, 163, 880, 22, 207, "Input"], Cell[5612, 187, 948, 33, 150, "Text"], Cell[6563, 222, 361, 6, 155, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[6961, 233, 41, 0, 96, "Section"], Cell[7005, 235, 65, 0, 40, "Text"], Cell[7073, 237, 261, 7, 77, "Input"], Cell[7337, 246, 104, 3, 40, "Text"], Cell[7444, 251, 479, 9, 155, "Input"], Cell[7926, 262, 29, 0, 40, "Text"], Cell[7958, 264, 461, 12, 103, "Input"], Cell[8422, 278, 44, 0, 40, "Text"], Cell[8469, 280, 275, 6, 77, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[8781, 291, 41, 0, 96, "Section"], Cell[8825, 293, 574, 14, 260, "Text"], Cell[9402, 309, 543, 11, 155, "Input"], Cell[9948, 322, 580, 13, 155, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10565, 340, 49, 0, 96, "Section"], Cell[10617, 342, 407, 11, 86, "Text"], Cell[11027, 355, 402, 11, 77, "Input"], Cell[11432, 368, 148, 4, 51, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[11617, 377, 28, 0, 96, "Section"], Cell[11648, 379, 471, 13, 129, "Text"], Cell[CellGroupData[{ Cell[12144, 396, 34, 0, 66, "Subsection"], Cell[12181, 398, 526, 10, 150, "Text"], Cell[12710, 410, 109, 2, 51, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[12856, 417, 34, 0, 66, "Subsection"], Cell[12893, 419, 1158, 20, 370, "Text"], Cell[CellGroupData[{ Cell[14076, 443, 49, 0, 42, "Subsubsection"], Cell[14128, 445, 224, 7, 106, "Text"], Cell[14355, 454, 742, 20, 155, "Input"], Cell[15100, 476, 876, 23, 155, "Input"], Cell[15979, 501, 109, 2, 51, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[16125, 508, 53, 0, 42, "Subsubsection"], Cell[16181, 510, 346, 6, 84, "Text"], Cell[16530, 518, 489, 13, 103, "Input"] }, Open ]] }, Open ]] }, Open ]] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)