The SVD Decomposition and Image Compression In this laboratory session we will learn how to 1. Find the SVD decomposition of a matrix using MATLAB 2. Use the SVD to perform Image Compression. Introduction Let A be an mxn matrix (mn). Then A can be factored as 0 199 02 0 AN 0 0 A = USV = 01 11₂ FAX 1 11m V₂ 0 0 1839 on ANN WXn mXm *** 0 0 1083 MX* By convention on 2022... 20,20. U and V are orthogonal matrices (square matrices with orthonormal column vectors). Note that the matrix S is usually denoted by Σ, however we will use S to be consistent with MATLAB notation. One useful application of the SVD is the approximation of a large matrix by another matrix of lower rank. The lower-rarik maitrix contains less drita and so can be represented more compactly than the original one. This is the idea behind one form of signil compression. The Frobenius Norm and Lower-Rank Approximation The Frobenius norm is one way to measure the size of a matrix 10 (1)
ver, you should be able to editie in class. -2 Enter the matrices A -1-3-1 B= For each question below • Compute the Left Hand Side and the Right Hand Side of the equation and store them in the variables LHS1, RHS1, LHS2, RHS2 and so on . Use the logical operator to determine whether the statements below are true or false. LHSRHS does element by element comparison between the variables LHS and RHS and returns an array with elements set to 1 (True) where the relation is true and elements set to 0 (False) where it is not. Store the result of question 1 in the variable ans1, the result of question 2 in ans2, and so on. (e.g. ans1 LHS1==RHS1) You will be asked specific written questions about your computations in part II of this problem. 1. A(B+C) = AB+ AC 2. A(B+C) BA+CA 3. (A+B) = A²+2AB+B 4. (AB)² = A³B² 5. (A-B)(A+B)=A²-B²
(a) Enter the matrix A and compute U, S and V using the svd comand. Verify that A = USVT. Answer: >> A-[-2,8,20;14,19,10;2, -2, 1); >> [U,S,V]-svd (A) U= 0.6000 -0.8000 0.0000 0.8000 0.6000 0.0000 0.0000 -0.0000 1.0000 30.0000 0 15.0000 0 0 3.0000 0.3333 0.6667 0.6667 0.6667 0.3333 -0.6667 0.6667 -0.6667 0.3333 We can easily verify that U.S.V' returns the matrix A. S-
In general, the best rank-r approximation to A is given by 01 0 A 072 0 +46 1 II 112 000 mxr 0 0 rxn =${{{v} +02u₂v? +...+0,u,v ||A = Ar = √√²+1 +² +2+...+02/ The MATLAB command [U, S, V] = svd (A) returns the SVD decomposition of the matrix A, that is, it returns matrices U, S and V such that A = USVT. Example The SVD of the following matrix A is: F30 T-2 8 201 16 A 14 2 01 and it can be shown that 19 10 = =2 1 15 0 03
The Frobenius Norm and Lower-Rank Approximation The Frobenius norm is one way to measure the size of a matrix Ital =√₁² +6²+²+² 1/2 and in general, A||= (2) that is, the square root of the sum of squares of the entries of A. If A is an approximation to A, then we can quantify the goodness of fit by A-A This is just in least squares measure. The SVD has the property that, if you want to approximate a matrix A by a matrix A of lower rank, then the matrix that minimizes ||A-A among all rank-1 matrices is the matrix HA John L Ixn
>> A2 = A1+S (2,2) *U(:,2) *V(:,2)' A2 F -2.0000 8.0000 20.0000 14.0000 19.0000 10.0000 -0.0000 0.0000 0.0000 >> rank (A2) ans = 2 >> norm (A-A2, fro") ans = 3.0000 >> S (3,3) ans = 3 Note that we need 14 entries to generate A2: (2 singular values) + ((2 columns of U) + (2 columns of V)=2+2·3+2+3=14. This is more than the number of entries in the original matrix A.
Image Compression Exercises Instructions: The following problems can be done interactively in the live script template or by writing the commands in an M-file and then printing them in the livescript (or by a combination of the two). In either case, record all MATLAB input commands and output. For problem 2, include a picture of the rank-1 approximation. For problem 3, include a picture of the rank-10 approximation and for problem 4, include a picture of the lower rank approximation that, in your opinion, gives an acceptable approximation to the original picture. Do not forget to answer the written questions. Step 1. Download the file gauss.jpg and save it to your working MATLAB directory. Then load it into MATLAB with the command A - imread ('gauss.jpg'); % note the semicolon! The semicolon is necessary so that MATLAB does not print out many screenfuls of data. The result is a matrix of grayscale values corresponding to a black and white picture of a dog. (The matrix has 287655 entries). Now, A is actually 453 x 635 x 3. To create an image from the matrix A, MATLAB uses the three values A(,, 1:3) as the RGB values to use to color the pixel in the i-th row and j-th column. We have a black and white picture so A(:,:,1) need to work with A(:,:,1). A(:,:,2)= A(:,:,3) and we only
(b) Use MATLAB to find the best rank-1 approximation to A (with respect to the Frobenius norm), that is, A1 = Use the command rank to verify that the matrix A1 has indeed rank one. Evaluate ||A=A1|| by typing norm (A - A1, 'fro') and verify Eq. (2). Answer: >> A1 = S(1,1)+U(:,1) *V(:,1)> A1 = 6.0000 12.0000 12.0000 8.0000 16.0000 16.0000 0.0000 0.0000 0.0000 >> rank (A1) ans = 1 >> norm (A-A1, "fro') ans = 15.2971 >> sqrt (S(2,2)^2+S (3,3)*2) ans - 15.2971 Note that, although A1 is 3x3 and therefore it contains nine entries, we only need seven values to generate it: (one singular value)+ (one column of U) +( one column of V) =143+3=7. This is less than the number of entries in the matrix A. (e) Find the best rank-2 approximation to A. Check the rank and the Frobenius norm of A-42 using MATLAB and verify (2). Answer:
(b) Use MATLAB to find the best rank-1 approximation to A (with respect to the Frobenius norm), that is, A1 = Use the command rank to verify that the matrix A1 has indeed rank one. Evaluate ||A=A1|| by typing norm (A - A1, 'fro') and verify Eq. (2). Answer: >> A1 = S(1,1)+U(:,1) *V(:,1)> A1 = 6.0000 12.0000 12.0000 8.0000 16.0000 16.0000 0.0000 0.0000 0.0000 >> rank (A1) ans = 1 >> norm (A-A1, "fro') ans = 15.2971 >> sqrt (S(2,2)^2+S (3,3)*2) ans - 15.2971 Note that, although A1 is 3x3 and therefore it contains nine entries, we only need seven values to generate it: (one singular value)+ (one column of U) +( one column of V) =143+3=7. This is less than the number of entries in the matrix A. (e) Find the best rank-2 approximation to A. Check the rank and the Frobenius norm of A-42 using MATLAB and verify (2). Answer:
Step 6. View the resulting image: image (C), axis image % no semicolon Include the code and the figure in your report. the original picture (Use PROBLEM 3. Create and view a rank-10 approximation to Steps 4-6 but with rank10 instead of rank1. If you mess up for example, you get an all-black picture - then start over from Step 3.) It is convenient to create an M-file with a for loop to evaluate ajuv+...+ Include the code and the figure. PROBLEM 4. Repeat with rank-20, 30 and 40 approximations (and any other ranks that you'd like to experiment with). What is the smallest rank that, in your opinion, gives an acceptable approximation to the original picture? In your lab write-up, include only the code that gives an acceptable approximation and the corresponding picture. PROBLEM 5. What rank-r approximation exactly reproduces the original picture? You only need to answer the question. Do not include the picture. Hint: Think about the original picture. Do not just answer this question by looking at the figures and guessing.
Step 3. Let's visualize rank1. To do that, first create C = zeros(size (A)); %semicolon! This creates an array of zeros, C. of the same dimension as the original image matrix A. THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED 2021 v14 Copyright School of Mathematical and Statistical Sciences, Arizona State University Step 4. Copy the rank-1 image into C as follows: C(:,:,1) rankl; C(:,:,2) = ranki; C(:,:,3) = ranki; Step We are almost done, except for one hitch. MATLAB does all its scaling using values from 0 to 1 (and maps them to values between 0 and 255 for the graphics hardware). Lower-rank approximations to the actual image can have values that are slightly less than 0 and greater than 1. So we will trancate them to fit, as follows: C- max (0,min (1,C));
PROBLEM 7. If we use a high rank approximation, then the amount of data needed for the approximation may exceed the amount of data used in the original representation of the picture. Find the smallest value of k such that the rank-k approximation of the matrix uses the same or more amount of data as the original picture. Approximations with ranks higher than this & cannot be used for image compression since they defeat the purpose of using less data than in the original representation. Hint: Use the general compression rate formula you found in Problem 6, part (ii). When you substitute the numbers for m and n, your & value will be a decimal numbers. Since k represents rank, it must be an integer. Think about whether you should round down or up.
PROBLEM 6. (i) Suppose that a rank-k approximation, for some k, gives an acceptable approximation. How much data is needed to represent the rank-k approximation? Your answer should be an expression in terms of k, m and n. Hint: you need & columns of U, k columns of V, and k singular values of S. (ii) The ratio of the amount of data used for the approximation (which you found in part (i)) and the amount of data of the (original format of the) picture is the compression rite. Find the compression rate for the value of the rank you determined in Problem 4. What does the compression rate represent? Hint: After finding the compression rate for the value of the rank you determined in Problem 4, you may want to present this number as a percentage. Think about how this percentage relates to the amount of data of the original approximation.
The SVD Decomposition and Image Compression In this laboratory session we will learn how to 1. Find the SVD decompositio
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
The SVD Decomposition and Image Compression In this laboratory session we will learn how to 1. Find the SVD decompositio
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!