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: Basis and reconstruction of 64 pixel image.

DIP

Decompose and partially reconstruct according to the Haar basis.

If we think of an image as a collection of independent pixels, then the only way to describe an image would be “the first pixel’s color is x, the second pixel’s color is y, ...” However, you would never describe an image like that, and spatial coherency states that neighboring pixels are usually closely related in color. Rather than think of an image as a collection pixels, in this demo we reimagine the image as a collection of patterns, where we simply mix how much of each pattern we need.

This is an application of a change of basis in linear algebra.

haarReconstructionDemo: Works only on 64 pixel images...
<Figure size 800x800 with 64 Axes>
<Figure size 800x800 with 64 Axes>

What the figures show

The cell above takes an 8×88 \times 8 RGB pixel-art image I\mathbf{I} and computes its 2D Haar transform, one channel at a time:

C=H I H⊺\mathbf{C} = \mathbf{H}\,\mathbf{I}\,\mathbf{H}^\intercal

where the rows of the 8×88 \times 8 matrix H\mathbf{H} are the 1D Haar basis vectors (see view_basis_blocks). Each coefficient CijC_{ij} says how much of one 2D basis image Bij=hihj⊺\mathbf{B}_{ij} = \mathbf{h}_i \mathbf{h}_j^\intercal (the outer product of rows ii and jj of H\mathbf{H}) is in the image. Because H\mathbf{H} is orthonormal, the original image is exactly the sum of all 64 basis images, each weighted by its coefficient:

I=∑i=07∑j=07Cij Bij\mathbf{I} = \sum_{i=0}^{7} \sum_{j=0}^{7} C_{ij}\, \mathbf{B}_{ij}
  • The first figure shows the 64 basis images Bij\mathbf{B}_{ij} in their (i,j)(i,j) positions, with the coefficient (summed over the three color channels) printed in cyan. The top-left B00\mathbf{B}_{00} is constant: its coefficient is proportional to the average color. Moving right or down, the patterns vary over smaller and smaller scales in the horizontal or vertical direction.

  • The second figure builds up the reconstruction by adding the basis images in order of distance from the top-left corner, roughly from coarse to fine scale. Panel (i,j)(i,j) shows the running sum after adding CijBijC_{ij}\mathbf{B}_{ij}. The top-left panel is just the average color, and the image sharpens as you move toward the bottom-right panel, which is the exact original.

Notice how recognizable the face already is long before all 64 patterns have been added. Most of the image’s structure lives in the coarse patterns, and that observation is the foundation of transform coding.

 

🔨 Your Turn: Your Own Pixel Art, and Other Bases

Task:

  1. Run the decomposition on your own 8×88 \times 8 pixel art image. There are many examples out there, or maybe make your own.

  2. Then try a different reconstruction basis. Update the line

    H = make_haar_matrix(8)

    and use an alternative like make_dct_matrix(8) or even make_random_basis(8). How does the order of reconstruction compare to Haar? How soon is the image recognizable?

  3. Try the standard basis, make_standard_matrix(8). That’s the basis that says an image is a collection of pixels rather than a collection of patterns; it’s also called the Euclidean basis. Look at the coefficients that are reported in cyan. How are those coefficients distributed, versus when you use a pattern basis like Haar?

Guidance:

  • Copy the code cell above into the empty cell below, rather than editing it, so that you keep the Haar version to compare against.

  • The code works only on 8×88 \times 8 RGB images with values in [0,1][0,1]. Check yours with arr_info. Many downloaded “8×8” images are actually saved larger, with each art pixel drawn as a square of screen pixels; take every kk-th pixel to get back to 8×88 \times 8. Remove a fourth (alpha) channel if there is one.

  • All the bases in wavelet_utils are orthonormal (HH⊺=I\mathbf{H}\mathbf{H}^\intercal = \mathbf{I}), which is what makes the reconstruction exact. You can check that numerically for each one.

  • For the comparison, focus on the reconstruction figure: in each basis, how many of the 64 panels does it take before you would recognize the picture?