• No results found

Chapter 1 Exercises

7.1 Digital image compression

How do we reduce large images to manageable file sizes? One approach uses the singular value decomposition (SVD). A digital image can be represented as a matrix, where each entry represents a pixel and we assign a numeric value to each color. The singular values of the matrix are the key. Typically some of these are large, but many are very small. By keeping only the significant singular values and throwing out the rest, we can significantly reduce the amount data that we need to store.

To illustrate the idea, we have imported a small grayscale image file. It is 133×150, which means the matrix has 19,950 entries. The SVD is then used to generate several approximations at significantly reduced file sizes. The original and reduced images are shown in Figure7.1(the original, exact image is at the far right).

The first approximation uses only three singular values. That means we keep three σs, plus three columns of U and three columns of V. We can simply set the other values to 0 (then we don’t need to store that data), or delete them. We then multiply these smaller matrices back together to obtain a full size approximation of the original image. The upshot is we only have to store 3 + (3×133) + (3×150) = 852 data values, compared to the original total of 19,950. That is only 4% of the original size! Keeping a few more singular values, the second approximation uses 10 singular values and is 14% of the original size. The image quality is not bad, considering how much of the original data we threw away. The third approximation, with 30 singular values,

Figure 7.1: SVD approximations

looks almost as good as the original. But, it is only 43% of the original size. For comparison, the exact image is shown on the far right.

Octave supports several image file types. We will usejpg files, which are loaded as RGB (red,

green, blue) images, represented as a set of threem×nmatrices containing the color values for each pixel. For simplicity, we will convert this to a singlem×ngrayscale matrix. Some of the Octave commands needed for basic image processing are listed in the table below.

Image processing commands

Syntax Description

pkg install −forge image . . . install the image package from Octave Forge pkgload image . . . load the image package

im =imread('filename.jpg') ; load an image

name =imresize(im, 0.5); . . reduce image size by a specified factor (e.g., 0.5) name =rgb2gray(im); . . . convert to grayscale

imshow(im) . . . display an image

imagesc(im) . . . display a matrix as a scaled image colormap('gray') . . . set colormap to grayscale

colormap('default') . . . restore colormap to default

Problems

1. For this problem, you will use the SVD to produce a compressed image using k singular values. You choosek(something between 5 and 50 would be suitable). To begin, you will need a digital photo injpgformat. Make sure you have loaded the image package.

(a) Load the image in Octave as “imcolor,” then convert to a grayscale image.

(b) Check the size of your grayscale image and if it is larger than approximately 320×280, determine an appropriate reduction factor and reduce it. Reducing to a modest size makes it easier to open the variables in the variable editor to inspect their values. Name the reduced, grayscale format image “im.” This is the image that will be compressed via the SVD method. Display the reduced grayscale image usingimagesc and verify that it still looks like the original. Include a copy of this grayscale image with your problem solutions and state its size.

(c) Find the SVD of the matrix representation “im.”

(d) Use the SVD to calculate an approximation usingksingular values. That means you should only keep the first k columns of U, the k largest values of Σ, and the first

k columns of V. Set the other values to 0 (or delete the extra columns altogether), then compute UΣVT to recover an approximation of the original image. Save it as “im2” and display it using imagesc. Include a copy of the reduced image with your problem solution.

(e) How many nonzero values are saved in the compressed factorization compared to the original?

2. Using the “outer product” expansion of A = UΣVT, the matrix A can be calculated column-by-column as

A=σ1u1vT1 +σ2u2vT2 +· · ·+σrurvTr

where each term is anm×n matrix. For anyk < r, the sum of the firstk terms will be an approximation usingkm+kn+k data values.

(a) Use the steps outlined in problem 1to load a grayscale image matrix.

(b) Write a for-loop to generate an outer product expansion usingksingular values. (c) Run the loop through 1, 5, 10, 20, and 30 iterations, saving a copy of the output

image each time.

(d) How many singular values do you think are needed before the quality of the reduced image is “good enough”?

(e) At whatk-value does the SVD approximation actually require savingmoredata than the original image?

3. Use the iterative outer product method of problem 2 to create a slide show showing the progressive quality improvement as the number of singular values increases from 1 up to the point where the approximation and original are indistinguishable.

You will need to use a loop that produces an approximation for eachifrom 1 tok. Some special formatting is needed to create a series of file names that increment as you cycle through the loop. Load your grayscale image and find the SVD. Then use the code below as a template to generate a series of progressively better approximations.

>> % i n i t i a l i z e a p p r o x i m a t i o n and s e t number o f s i n g u l a r v a l u e s >> approx = z e r o s(s i z e( im ) ) ; >> k = 25 >> % l o o p t o c r e a t e a p p r o x i m a t i o n s and s a v e a s image f i l e s >> f o r i = 1 : k >> approx = approx + S ( i , i )*U ( : , i )*V ( : , i ) '; >> h = i m a g e s c( approx ) ; >> name = s p r i n t f('%s%d . png', 'approx', i ) ; >> s a v e a s( h , name ) >> end

Related documents