Skip to content

Instantly share code, notes, and snippets.

@jxnl
Created November 14, 2016 16:03
Show Gist options
  • Save jxnl/2407f87ffbc950d4c7a16dc9a544a33e to your computer and use it in GitHub Desktop.
Save jxnl/2407f87ffbc950d4c7a16dc9a544a33e to your computer and use it in GitHub Desktop.
function [cmpImg] = svdCompress(img,k)
% Obtains SVD
[u,s,v] = svd(img);
% Vectorized sum of k rank one matrices
cmpImg = u(:,1:k)*s(1:k,1:k)*v’(1:k,:);
end;
function [normError] = svdVisualize(FILENAME,k,plot)
% Loads Image and takes one of RBG matrix
img = imread(FILENAME)(:,:,1);
% Obtains SVD
[u,s,v] = svd(img);
% Vectorized sum of rank = k Matrix
cmpImg = uint8(u(:,1:k)*s(1:k,1:k)*v’(1:k,:));
if plot == 1
% original image
subplot(1,3,1);
image(img./5)
axis off
% compressed image of rank k
subplot(1,3,2);
image(cmpImg./5)
axis off
% difference error
subplot(1,3,3);
image(img-cmpImg);
axis off
else
errMatrix = double(img-cmpImg);
normError = norm(errMatrix);
end
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment