Sub Band
Sub Band
October 2004
1
1.1
Introduction
Overview
A transform coder consists of three distinct parts: The transform, the quantizer and the entropy coder. In this laboration you will study all three parts and see how the choice of transform/quantizer/entropy coder aects the performance of the transform coder. The laboration runs in a MATLAB environment. In MATLAB images are naturally represented as matrices. During the laboration certain test images will be compressed. The test images can be thought of as original images in the sense that they are stored as raw samples with 24 bits/pixel. This usually gives more colours than the human eye can discern on a computer screen.
1.2
Preparations
We assume that you have studied the chapters on transform coding, subband and wavelet coding, quantization and entropy coding in the course literature. Read through this document carefully before the laboration. We also suggest that you read the manual (help <funcname>) for each function before you use it.
We will be working with colour images. An image can be read into MATLAB using the command imread. For future manipulation, we change the pixel values to be oating point values between 0 and 1: >> im1=double(imread(image1.png))/256;
The colour image is stored as a 512 768 3 matrix, meaning that we have one 512 768 matrix for each of the three RGB colour components (red, green and blue). The image can be viewed using the command imshow: >> imshow(im1) We can also view each colour plane separately: >> im1r=im1(:,:,1); im1g=im1(:,:,2); im1b=im1(:,:,3); >> imshow(im1r), figure, imshow(im1g), figure, imshow(im1b) When coding colour images, we usually use the YCbCr colour space (or another luminance/chrominance colour space), rather than the RGB colour space. To convert the image back and forth between the dierent colour spaces, use the functions rgb2ycbcr and ycbcr2rgb: >> im1ycbcr=rgb2ycbcr(im1); See what the luminance and chrominance components look like: >> im1y=im1ycbcr(:,:,1); im1cb=im1ycbcr(:,:,2); im1cr=im1ycbcr(:,:,3); >> imshow(im1y), figure, imshow(im1cb), figure, imshow(im1cr) The luminance component is basically a greyscale version of the colour image, while the two chrominance components contain information about the colour of the image. There are six images (named image1.png to image6.png) with varying content that you can use for your experiments. Take a look at the other images too.
Image transformations
The transform that we will use in this laboration is a dyadic subband decomposition. The image is ltered with a low-pass and a high-pass lter, both horizontally and vertically. The four ltered versions of the image are then subsampled. So, if the original image is of size 512 768 pixels, after one step of the decomposition we have 4 images of size 256 384. The lters are then applied recursively on the low-pass image. Typically, 4-5 such steps is sucient. For example, see gures 14.10, 14.11 and 14.12(a) in Sayoods book. The MATLAB function to do this subband decomposition is called dsbt2. As arguments, the function takes an image, the number of splitting steps and what set of lters to use. Read the manual about the function (help dsbt2) before using it. Example: >> im1ys = dsbt2(im1y, 2, 1); >> figure, imshow(im1ys,[]) This performs a two-level subband split on the image, using the 9/7 tap lter pair 2
and displays the transformed image. As you can see, dsbt2 returns the result of the subband split in a single matrix. We want to be able to treat each subband separately. The function sbdivide can be used to divide the transformed image into a number of separate subbands, and the function sbmerge can be used to put them back into a single matrix again. >> im1ysd=sbdivide(im1ys, 2); >> for k=1:length(im1ysd),figure,imshow(im1ysd{k},[]),end Note that sbdivide returns a cell array, which is indexed using curly braces, and that you need to give the number of subband splits performed as an argument. Inverse transformation is performed with the function idsbt2. Example: >> im1yr = idsbt2(im1ys, 2, 1); Without quantization, im1y and im1yr should be identical. Due to rounding errors in the computer, there might be a slight (but negligible) dierence.
Quantization
The second part of a transform image coder is the quantizer. We will only be looking at uniform quantization, using the function sbquant. This function takes a transformed image (either as a single matrix, or a cell array from sbdivide) and a quantization parameter as arguments. The quantization parameter is a vector of quantization steps, one for each subband in the transformed image. The quantization parameter can also be given as a scalar. In that case, the same stepsize is used for every subband. To do inverse quantization (reconstruction), use the function sbrec. Examples: >> >> >> >> >> >> >> >> >> >> Q1=0.2; im1ysq = sbquant(im1ys, Q1); im1ysr = sbrec(im1ysq, Q1); im1yr = idsbt2(im1ysr, 2, 1); figure, imshow(im1yr, []); Q2=[0.1 im1ysdq im1ysdr im1yr = figure, 0.2 0.2 0.2 0.4 0.4 0.4]; = sbquant(im1ysd, Q2); = sbrec(im1ysdq, Q2); idsbt2(sbmerge(im1ysdr), 2, 1); imshow(im1yr, []);
In the rst example, all subbands are quantized with the uniform stepsize 0.2. In the second example, the stepsize 0.1 is used for the lowpass signal, stepsize 0.2 is used for the three middle frequency bands and the stepsize 0.4 is used for the three high-frequency bands. The distortion (the mean square error) between the original image and a reconstructed image can be calculated as >> dist = mean((im1y(:)-im1yr(:)).^2) Usually, distortion in images is measured by the PSNR (Peak-to-peak Signal to Noise Ratio). For an image where the pixels take values between 0 and 1, it is given by >> psnr = 10*log10(1/dist) There is no standard way of dening PSNR for colour images, but one way is to average the PSNR of the three colour components (red, green and blue). By varying the quantization steps (Q1 or Q2), you get reconstructed images with dierent quality.
Entropy coding
The third part of a transform image coder is the entropy coder (or source coder). In this lab were going to study two methods: Memoryless Human coding, using the function huffman and memoryless arithmetic coding, using the function arithcode. Neither of these functions perform the atcual coding, they just estimate the number of bits needed. The source coding is done on the quantized transform image. To be able to do ecient coding, we rst need to estimate the distribution by calculating a histogram of the data we want to code. This can be done with the function ihist. The simplest case is if we use a single code for all subbands. Example: >> p = ihist(im1ysq(:)); >> bits = huffman(p) This returns the total number of bits required to code the quantized data. Note that the huffman function doesnt include the cost to code the human tree, and thus the real number of bits needed would be slightly higher. Normally, we measure the coding eciency in bits per pixel, which we can get by
just dividing the total number of bits with the image size: >> bpp = bits/(512*768) Since the distribution is typically dierent for dierent subbands, a more ecient coding can be performed if we have separate human codes for each subband. Applying this to im1ysdq: >> bits=0; for k=1:length(im1ysdq) p = ihist(im1ysdq{k}(:)); bits = bits + huffman(p); end >> bpp = bits/(512*768) Just change huffman to arithcode to try arithmetic coding.
Chrominance subsampling
Usually, the chrominance signals (Cb and Cr) can be subsampled before coding, without giving noticeable eects on the image quality. Subsampling can be done using the function imresize. Example: >> im1cb2 = imresize(im1cb, 0.5, bicubic); This will subsample the image with a factor 2 both horizontally and vertically. The subsampled image is then transformed, quantized and coded. In the decoder, the reconstructed image is upsampled before transformation back into the RGB colour space: >> im1cbr = imresize(im1cb2r, 2, bicubic); assuming that the reconstructed subsampled image was called im1cb2r. An alternative way to do subsampling in a subband coder is to just throw away the three highest frequency bands after transformation (set them to zero and dont code them).
Own experiments
Now that you know how to use all three parts (transform, quantizer, entropy coder) it is time for you to experiment freely. Try coding both greyscale images (just the luminance component) and colour images. You should try to nd answers to the following questions: What subband lter gives the best results? At least compare lter number 0 (Haar lter) and lter number 1 (9/7 tap lter). How does the number of subband splits aect coding performance? What quantization method gives the best results, using the same stepsize everywhere or using dierent stepsizes for dierent subbands? What entropy coding method gives the best results? Compare at several different bit rates. How does chrominance subsampling aect coding performance? Does the PSNR measure correspond well to visual quality? Compare images that have been coded to the same distortion using dierent methods. What is the lowest rate (in bits per pixel) that gives coded images that are indistinguishable from the original image at normal viewing distance? What is the lowest rate that gives an acceptable image quality? A simple MATLAB function to get you started can be found in /site/edu/icg/tsbk02/matlab/sbcoder.m. Copy it to your directory and edit it to suit your needs. NOTE: When comparing two dierent coders, you shouldnt just check at one rate or distortion. Rather you should code at dierent rates and measure the distortions and then compare the resulting rate distortion curves of the two coders.
Filter coecients
These are the lter coecients of the dierent synthesis lters. Haar lter Low pass lter High pass lter 0.707107 0.707107 0.707107 -0.707107 9/7 tap lter Low pass lter High pass lter -0.037828 -0.064539 -0.023849 -0.040689 0.110624 0.418092 0.377402 0.788486 -0.852699 0.418092 0.377402 -0.040689 0.110624 -0.064539 -0.023849 -0.037828 2/6 tap lter Low pass lter High pass lter -0.088388 0.088388 0.707107 0.707107 0.707107 -0.707107 0.088388 -0.088388 4 tap lter Low pass lter High pass lter 0.176777 -0.176777 0.530330 -0.530330 0.530330 0.530330 0.176777 0.176777
LeGall lter Low pass lter High pass lter 0.176777 0.353553 0.353553 0.707107 -1.060660 0.353553 0.353553 0.176777 6/10 tap lter Low pass lter High pass lter 0.018914 0.006989 -0.067237 0.129078 0.133389 0.047699 0.615051 -0.788486 0.615051 0.788486 0.133389 -0.047699 -0.067237 -0.129078 0.006989 0.018914