3
$\begingroup$

I need to plot the relative error vs compression ratio diagram of a discrete wavelet transform on an image, but no luck so far .. Here's the problem in short:

  1. random_image.jpg
  2. compress the image
  3. plot the relative error vs compression ratio diagram

Here's the code and below is the link I am using as a reference:

cumulativeEnergy[data_] := 
 Module[{c = Sort[Flatten[data]^2, Greater]}, Accumulate[c]/Total[c]]

data = (* insert random image*)

wtdata = DiscreteWaveletTransform[data]

wtE = cumulativeEnergy[Last /@ wtdata[Automatic]];

ListLinePlot[Sqrt[1 - wtE]]

The problem is I don't get a curve like in the example..

Tutorial used as a reference

Thank you !

$\endgroup$
  • $\begingroup$ What do you get? $\endgroup$ – Jagra Feb 8 '13 at 3:10
11
$\begingroup$

In the old Mathematica wavelet explorer, the one used in the tutorial, coefficients were arranged in decreasing order of energy. You can see that from the following line from the tutorial,

In[8]:= Dimensions /@ wtdata
Out[8]= {{32,32},{3,32,32},{3,64,64}....}

In the new Mathematica 8 wavelets the coefficients are arranged in increasing order of energy as seen from the following example.

 In[1]:= data=ExampleData[{"TestImage","Boat"}];

 In[2]:= wtdata=DiscreteWaveletTransform[data]
Out[2]= DiscreteWaveletData[<<DWT>>,<9>,{512,512}]

 In[3]:= wpdata=DiscreteWaveletPacketTransform[data]
Out[3]= DiscreteWaveletData[<<DWPT>>,<4>,{512,512}]

 In[4]:= wtdata["Dimensions"]
Out[4]= {{0}->{1,256,256},{1}->{1,256,256},{2}->{1,256,256},{3}->{1,256,256},{0,0}->{1,128,128},{0,1}->{1,128,128},{0,2}->{1,128,128},{0,3}->{1,128,128},{0,0,0}->{1,64,64},{0,0,1}->{1,64,64},...

All you need to do is reverse the order of the coefficients,

In[5]:= wtE = Reverse@cumulativeEnergy[wtdata[Automatic, "Values"]];
In[6]:= wtP = Reverse@cumulativeEnergy[wpdata[Automatic, "Values"]];

In[81]:= ListLinePlot[{Sqrt[1 - wtE], Sqrt[1 - wtP]},AxesOrigin -> {1000, 0.1}, PlotLabel -> "relative error vs. compression ratio", PlotStyle -> {Blue, {Red, Dashed}}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you for the in-depth explanataion :) ! $\endgroup$ – Sektor Feb 8 '13 at 7:54
  • $\begingroup$ @Leonid It is nice to be part of the Mathematica community :-) $\endgroup$ – VivekJ Feb 10 '13 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.