Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

stough 202-

Preview: Viewing different basis blocks, along with their transform matrices

DIP Chapter 6, 8.9

Discrete wavelets broadly can be seen as a way of looking at a signal as a collection of patterns instead of as a collection of independent samples. In order to uniquely and fully describe a signal, these basis patterns should be orthogonal to one another (all dot products zero), have magnitude 1, and cover the space of all possible signals. For us, the signal is a NxN block of pixels within an image. wavelet_utils provides numerous orthonormal basis sets for the block size of our choosing. Here, we look at them in turn.

When I talk about the power of any basis, I am thinking of how efficient any such basis will be in terms of concentrating the signal in as few coefficients (basis patterns) as possible. This is also dependent on the kind of image data being considered.

First up, the standard, or Euclidean matrix.

This is the most natural basis, and the one you think about when you imagine a block to consist of a collection of pixels.

HEuc=∣1000010000100001∣\mathbf{H_{Euc}} = \begin{vmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{vmatrix}

If we consider just the 2D (2x2) version:

HEuc=∣1001∣\mathbf{H_{Euc}} = \begin{vmatrix} 1 & 0 \\ 0 & 1 \end{vmatrix}

This is how you describe any location in a Cartesian 2D space. This is a basis pattern, where each coefficient will precisely reflect the signal in just one of the two pixels. Recall, these basis patterns should be orthogonal to one another, have magnitude 1, and cover the space of all possible signals. So

  • ⟨1,0⟩⋅⟨0,1⟩=0\langle 1,0 \rangle \cdot \langle 0,1 \rangle = 0, so the basis is orthogonal

  • Each of ⟨1,0⟩\langle 1,0 \rangle and ⟨0,1⟩\langle 0,1 \rangle has magnitude 1 (square root of the sum of squares of the components).

  • We know every point in 2D can be described with a coordinate in x and a coordinate in y. Or, a coefficient with respect to ⟨1,0⟩\langle 1,0 \rangle and a coefficient with respect to ⟨0,1⟩\langle 0,1 \rangle.

We take outer products of the rows to extend this basis set to square-sized basis patterns.

<Figure size 500x500 with 16 Axes>

 

Next up, the Haar matrix.

Most useful basis sets will account for spatial coherence by incorporating the mean as one of the basis vectors. If there is just one piece of information you can store about a block, the mean intensity of the block would be the first choice. The Haar basis construction can be found analytically in DIP 6.9, with the implementation details available to you in waveletUtil.py.

HHaar=12∣111111−1−12−200002−2∣\mathbf{H_{Haar}} = \frac{1}{2}\begin{vmatrix} 1 & 1 & 1 & 1\\ 1 & 1 & -1 & -1 \\ {\sqrt 2} & -{\sqrt 2} & 0 & 0 \\ 0 & 0 & {\sqrt 2} & -{\sqrt 2} \end{vmatrix}

If we consider just the 2D (2x2) version:

HHaar=12∣111−1∣\mathbf{H_{Haar}} = \frac{1}{\sqrt 2}\begin{vmatrix} 1 & 1 \\ 1 & -1 \end{vmatrix}

Rather than store a coefficient for each pixel as the Euclidean basis does, this seems to dedicate one coefficient to the average of the two pixels and one coefficient to difference between the two. This is also a basis pattern. Recall, these basis patterns should be orthogonal to one another, have magnitude 1, and cover the space of all possible signals. So

  • ⟨12,12⟩⋅⟨12,−12⟩=0\langle \frac{1}{\sqrt 2}, \frac{1}{\sqrt 2} \rangle \cdot \langle \frac{1}{\sqrt 2},-\frac{1}{\sqrt 2} \rangle = 0, so the basis is orthogonal

  • E.g.: ⟨12,−12⟩\langle \frac{1}{\sqrt 2}, -\frac{1}{\sqrt 2} \rangle has magnitude 1: (12)2+(−12)2=1\Bigl( \frac{1}{\sqrt 2} \Bigr)^2 + \Bigl( -\frac{1}{\sqrt 2} \Bigr)^2 = 1

  • The basis covers all possible signals, here points in 2D:

∀(x,y)∈R2,leta=⟨12,12⟩⋅⟨x,y⟩=x+y2b=⟨12,−12⟩⋅⟨x,y⟩=x−y2.Thena⟨12,12⟩+b⟨12,−12⟩=(x+y2)⟨12,12⟩+(x−y2)⟨12,−12⟩=⟨x+y2,x+y2⟩+⟨x−y2,−x+y2⟩=⟨2x+y−y2,x−x+2y2⟩=⟨x,y⟩\forall (x,y) \in R^2, \verb+let+ \\ a = \langle \frac{1}{\sqrt 2}, \frac{1}{\sqrt 2} \rangle \cdot \langle x,y \rangle = \frac{x + y}{\sqrt 2}\\ b = \langle \frac{1}{\sqrt 2}, -\frac{1}{\sqrt 2} \rangle \cdot \langle x,y \rangle = \frac{x - y}{\sqrt 2}.\\ \verb+Then+ \\ a\langle \frac{1}{\sqrt 2}, \frac{1}{\sqrt 2} \rangle + b\langle \frac{1}{\sqrt 2}, -\frac{1}{\sqrt 2} \rangle \\ = \Bigl( \frac{x + y}{\sqrt 2} \Bigr)\langle \frac{1}{\sqrt 2}, \frac{1}{\sqrt 2} \rangle + \Bigl( \frac{x - y}{\sqrt 2} \Bigr)\langle \frac{1}{\sqrt 2}, -\frac{1}{\sqrt 2} \rangle \\ = \langle \frac{x + y}{2}, \frac{x + y}{2} \rangle + \langle \frac{x - y}{2}, \frac{-x + y}{2} \rangle \\ = \langle \frac{2x + y - y}{2}, \frac{x - x + 2y}{2} \rangle \\ = \langle x,y \rangle
<Figure size 500x500 with 16 Axes>

 

Next up, the Discrete Cosine transform matrix.

Each row of this matrix represents a sampled cosine wave with varying frequency over the signal space (s-D space, where s is the number of pixels). In that it satisfies the three conditions we specified above (left to you to prove), it can serve just as well as either of the above two. It is based in Fourier analysis on the idea that any signal can be constructed from cosine and sine waveforms.

The DCT is even more powerful for natural images because unlike the above two, here every basis pattern extends over the whole block. In fact, some form of DCT is usually so powerful, it has been adopted for use in JPEG, MPEG, and many other image and video compression standards.

Recall ss is the number of pixels, i.e. the size of our square matrix. The first row is again the mean, while later rows (uu) are sampled (by column jj) cosines:

DCT0=1sDCTu=2scos((2j+1)uπ2s),j=0,…,s−1,u=1,…,s−1DCT_0 = \sqrt \frac{1}{s} \\ DCT_u = \sqrt \frac{2}{s}cos\Bigl(\frac{(2j+1)u\pi}{2s}\Bigr), j = 0,\dots,s-1, u = 1,\dots,s-1
[[ 0.5         0.5         0.5         0.5       ]
 [ 0.65328148  0.27059805 -0.27059805 -0.65328148]
 [ 0.5        -0.5        -0.5         0.5       ]
 [ 0.27059805 -0.65328148  0.65328148 -0.27059805]]
<Figure size 500x500 with 16 Axes>
<Figure size 640x480 with 1 Axes>
np.float64(1.1102230246251565e-16)
np.float64(0.9999999999999999)
<Figure size 400x400 with 2 Axes>

 

Random: with the magic of Gram-Schmidt

orthonormalization, we can actually make a basis from any set of N vectors in N-D, as long as no one of them can be written as a linear combination of the others. There is of course no telling how powerful any such basis will be in terms of concentrating the signal in as few coefficients (basis patterns) as possible.

<Figure size 500x500 with 16 Axes>

 

Principal Component Analysis (PCA):

Rather than either a predefined basis (e.g., Euclidean, Haar, DCT) or a random basis, let’s make a basis that is ideal for the data that we want to compress.

Principal components analysis is a much larger topic than we can cover just yet here, but if you’re interested, take a look at this youtube channel. For us, PCA finds directions in the data space that account for the most variance in the data. That is, the first basis pattern PCA will compute is the one that accounts for as much of all the data differences as possible. After that, PCA will compute an orthogonal direction that accounts for as much of what’s left as possible.

  • Here, and unlike the previous cases, we need the image data that we’re interested in.

  • Unlike the predefined cases, we would have to explicitly send the transform matrix along with the coefficients to allow the receiving end to decode. This is like Huffman coding, where we construct just the right encoder/decoder to do the best job on the data at hand.

  • Using a pretty small size (like 4), PCA on many natural images will lead to a basis that bears some resemblance to DCT. This is congruent with what we have been told about the power of the DCT transform: on lots of natural images, the DCT is close to as good as it gets (which is PCA).

<Figure size 400x300 with 1 Axes>
<Figure size 500x500 with 16 Axes>
<Figure size 900x400 with 32 Axes>
<Figure size 400x400 with 2 Axes>