\(\newcommand{\bx}{\textbf{x}} \newcommand{\bo}{\textbf{0}} \newcommand{\bv}{\textbf{v}} \newcommand{\bu}{\textbf{u}} \newcommand{\bq}{\textbf{q}} \newcommand{\by}{\textbf{y}} \newcommand{\bb}{\textbf{b}} \newcommand{\ba}{\textbf{a}} \newcommand{\grad}{\boldsymbol{\nabla}} \newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\pdd}[2]{\frac{\partial^2 #1}{\partial #2^2}} \newcommand{\pddm}[3]{\frac{\partial^2 #1}{\partial #2 \partial #3}} \newcommand{\deriv}[2]{\frac{d #1}{d #2}} \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section5.6Image and Data Compression

One particular use of the SVD is for data compression. The following code will read the file Mountain.jpg into MATLAB and convert it to a rectangular matrix of values.

A = imread('Mountain.jpg');
A = A(:,:,1);
A = im2double(A);
imshow(A)

Once the matrix is in MATLAB do the following. In this we assume that \(A\) is an \(m \times n\) matrix.

  1. Find the SVD of the image (remember the semicolons!!!!!!). This will take over a minute with our code so be patient. Once it is done check that your SVD code does a decent approximation of the original image.

    = MySVD(A);
    error = norm(A - U*S*V')
    

  2. Get the singular values out of \(\Sigma\) and find the largest \(P\)% of the singular values. Let's say that this is \(N\) values. Create four new matrices \(U_{new}\), \(\Sigma_{new}\), \(V_{new}\), and \(A_{new}\) in the following way.

    1. \(U_{new}\) is \(m \times N\) and contains only the first \(N\) columns of \(U\).

    2. \(\Sigma_{new}\) is \(N \times N\) and contains only the top \(P\)% of the singular values of \(A\).

    3. \(V_{new}\) is \(n \times N\) and contains the first \(N\) columns of \(V\).

    4. \(A_{new}\) is \(m \times n\) and is formed by \(U_{new} \Sigma_{new} V_{new}^T\).

  3. Show the newly compressed image with imshow(Anew)

  4. Compute the storage savings with the following code:

    StorageOld = prod(size(U))+prod(size(S))+prod(size(V))
    StorageNew = prod(size(Unew))+prod(size(Snew))+prod(size(Vnew))
    PercentStorage = StorageNew / StorageOld
    

  5. Your code should be written in such a way that you can change \(P\) and see the new image that results (without having to wait for the SVD code every time). Make both qualitative and quantitative decisions about what percent of the singular values to save in order to keep an image that is close enough to the original image.