stough 202-

DIP 6, 8.9
In this activity you’re going to use the Haar wavelet block transform and lossy compression in two different contexts
A single single-channel “image” block with fairly high contrast.
A larger color image with blocks.
In each case we’re going to
Transform: Turn a block of image pixels into a block of Haar wavelet coefficients.
Compress: We’re going to effectively compress by zeroing out insigficant/small coefficients.
Reconstruct: Take the “compressed” coefficent block and reconstruct the block of image pixels, with some loss.
Recall from view_basis_blocks that the Haar transform matrix is like so:
To transform a block of pixels into a block of coefficients :
where is the transpose. Through the above we basically replace the set of independent pixels with the set of coefficients with respect to the Haar basis patterns, as we’ll see with vis_blocks. Once we have , we can quantize it to the degree that we wish, zeroing out small coefficients, potentially replacing large coefficients with nearby approximations on a standard grade (e.g. replace anything in with 256).
To reconstruct the original , we can simply
Imports¶
We’re going to use wavelet_utils.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# For importing from alternative directory sources
import sys
sys.path.insert(0, '../dip_utils')
from matrix_utils import (arr_info,
make_linmap)
from vis_utils import (vis_rgb_cube,
vis_hists,
vis_pair,
vis_triple,
vis_surface)
from wavelet_utils import (make_haar_matrix,
make_random_basis,
make_klt_basis,
make_dct_matrix,
make_standard_matrix,
vis_blocks)1. Haar Wavelet Compression¶
Use make_haar_matrix and vis_blocks to see the 16 Haar patterns for the matrix.
The function vis_blocks shows all possible outer products of rows in the input matrix. If the input is a transform matrix (whose rows are all orthogonal to one another, unit length, spanning the -D space), then vis_blocks shows the collection of patterns that the transform matrix implies.
H = make_haar_matrix(4)
vis_blocks(H)Transform a block of pixels.¶
We’ll define the block of pixels in question. Then you transform it into the coefficient block . See np.matmul to compute . You can think of as a little patch of an image.
B = np.array([[0,1,1,1],
[0,1,0,0],
[0,1,1,1],
[0,1,0,0]])
plt.figure(figsize=(4,4))
plt.imshow(B, cmap='gray')A. Viewing the transform coefficients in the matrix , what are the row and column coords for the two largest coefficients (in absolute value)?¶
Do not view with vis_blocks. is not a transform matrix; it is a matrix of coefficients, each coefficient corresponding to one of the patterns that you saw with vis_blocks(H) for a given transform matrix .
B. Show the two basis blocks corresponding to those two coefficients.¶
C. Show and explain as explicitly as you can how the two coefficients are what they are.¶
D. Show the reconstruction of the image using only those basis blocks/coefficients.¶
Is this reconstruction trending in the right direction? Explain.
2. Larger Image Block Transform and Reconstruction.¶
Use the code in block_viewing_demo and basis_decomp_64pix to perform a similar transform/compress/reconstruct sequence on a larger color image of your choice.
Use blocks. However, you should process each color channel separately.
Use
resizeon the image you choose to ensure that 8 evenly divides it in height and width. This is for convenience: compression schemes have other mechanisms to deal with odd-shaped images, which we will ignore here.Perform reconstructions using the largest coefficients in absolute value per block. Note that keeping 16 coefficients for example is equivalent to x compression -- that is, moving from storing 64 pixel values to storing 16 coefficient values.
For each coefficient visualization, you should keep coefficients per color channel. So for example with 1 coefficient, that’s 1 red, 1 blue, 1 green.
For at least the reconstructions using , use
vis_pairand zoom into an area to observe the effectiveness of the reconstruction on such limited basis (32x, 16x compression). Usevis_triplefor example to view the original image along with these lossy reconstructions.