Using SVD to compress an image in MATLAB

前端 未结 4 1915
我寻月下人不归
我寻月下人不归 2020-12-24 00:31

I am brand new to MATLAB but am trying to do some image compression code for grayscale images.

Questions

How can I use SVD to trim off low-v

4条回答
  •  臣服心动
    2020-12-24 01:05

    For example, here's a 512 x 512 B&W image of Lena:

    We compute the SVD of Lena. Choosing the singular values above 1% of the maximum singular value, we are left with just 53 singular values. Reconstructing Lena with these singular values and the corresponding (left and right) singular vectors, we obtain a low-rank approximation of Lena:

    Instead of storing 512 * 512 = 262144 values (each taking 8 bits), we can store 2 x (512 x 53) + 53 = 54325 values, which is approximately 20% of the original size. This is one example of how SVD can be used to do lossy image compression.


    Here's the MATLAB code:

    % open Lena image and convert from uint8 to double
    Lena = double(imread('LenaBW.bmp'));
    
    % perform SVD on Lena
    [U,S,V] = svd(Lena);
    
    % extract singular values
    singvals = diag(S);
    
    % find out where to truncate the U, S, V matrices
    indices = find(singvals >= 0.01 * singvals(1));
    
    % reduce SVD matrices
    U_red = U(:,indices);
    S_red = S(indices,indices);
    V_red = V(:,indices);
    
    % construct low-rank approximation of Lena
    Lena_red = U_red * S_red * V_red';
    
    % print results to command window
    r = num2str(length(indices));
    m = num2str(length(singvals));
    disp(['Low-rank approximation used ',r,' of ',m,' singular values']);
    
    % save reduced Lena
    imwrite(uint8(Lena_red),'Reduced Lena.bmp');
    

提交回复
热议问题