Code

SVD MATLAB Code

clear all;
clc;
close all;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%   Original   %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Read in .tif image
img = imread('sunflower.tif');

% Get dimensions of original image
[width, height, depth] = size(img);

% Display the image that was read in
imshow(img);
title('Original Image')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%   RGB Channels   %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Isolate RGB channels for display
imgRed = img;
imgRed(:,:,2:3) = 0;
imgGreen = img;
imgGreen(:,:,1) = 0;
imgGreen(:,:,3) = 0;
imgBlue = img;
imgBlue(:,:,1:2) = 0;

%Display RGB channels of Image
figure;
subplot(1,3,1);
imshow(imgRed);
title('Red Channel')
subplot(1,3,2);
imshow(imgGreen);
title('Green Channel')
subplot(1,3,3);
imshow(imgBlue);
title('Blue Channel');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%   RGB   %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Split image into RGB channels for calculation
img_r = im2double(img(:,:,1));
img_g = im2double(img(:,:,2));
img_b = im2double(img(:,:,3));

% Apply svd to rgb matrices and get their respective U, S, % and V values
[Ur,Sr,Vr] = svd(img_r, 'econ');
[Ug,Sg,Vg] = svd(img_g, 'econ');
[Ub,Sb,Vb] = svd(img_b,'econ');

% Percent Errors we want to look at
perErrors = [0, 1, 5, 15, 25, 30, 35, 40, 50, 75, 100];

% Initialize index matrix (will contain index of singular 
% values at specified percent error)
index = ones(length(perErrors),1);

% Find the smallest dimesion of image --> rank
[x,y] = size(Sr);
if(x > y)
min = y;
else
min = x;
end

% Search for indices that correlate with percent errors 
% given in perErrors
for i = 1:1:length(perErrors)
temp = 0;
for j = 1:1:min
temp = temp + Sr(j,j);
error = (1 - temp/sum(sum(Sr)))*100;
if(error > perErrors(i))
index(i) = j;
j = min + 1;
end
end
end

figure;
compSize = [0 0 0 0 0 0 0];

% k will help us iterate through index matrix for each 
% percent Error
for k = 1:1:length(perErrors)

% Getting the index values from the index matrix
ind = index(k);

% Set all values that are not the number of terms we want 
% equal to 0
Dr = Sr;
Dg = Sg;
Db = Sb;
Dr(ind+1:end , ind+1:end) = 0;
Dg(ind+1:end , ind+1:end) = 0;
Db(ind+1:end , ind+1:end) = 0;

% Define new compressed matrix
r = Ur * Dr * Vr';
g = Ug * Dg * Vg';
b = Ub * Db * Vb';

% Combining RGB matrices to reconstruct our image
combRGB(:,:, 1) = r;
combRGB(:,:, 2) = g;
combRGB(:,:, 3) = b;

% Store the image
%imwrite(combRGB, sprintf('combRGB_%d.tif',k));

% Calculate compression ratio
% Ratio is k*(m+n+1)/m*n
compRatio = (width*height)/(ind *(width + height + 1));

% Plot new RGB image
subplot(4,3,k);
imshow(combRGB);

title(sprintf('%dx Comp %d terms & ~%d%% error', compRatio, ind , perErrors(k)))

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%   GRAYSCALE   %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Convert image to grayscale and cast to double (allows for SVD)
imgGray = im2double(rgb2gray(img));

% Display grayscale image
figure;
imshow(imgGray)
title('Grayscale Image')

% Apply svd to grayscale image matrix and get U, S, and V
[U,S,V] = svd(imgGray);

figure;
for k = 1:1:length(perErrors)
% Getting the index values from the index matrix
ind = index(k);

% Set all values that are not the number of terms we want 
% equal to 0
D = S;
D(ind+1:end , ind+1:end) = 0;

% Define new compressed matrix
newImgGray = U * D * V';

% Store image
%imwrite(newImgGray, sprintf('combGray_%d.tif',k));

% Calculate compression ratio
compRatio = round((width*height)/(ind *(width + height + 1)),2);

% Plot new Grayscale image
subplot(4,3,k)
imshow(newImgGray);

title(sprintf('%dx Comp %d terms & ~%d%% error', compRatio, ind , perErrors(k)))
end

SVD K Analysis MATLAB Code

clear all;
clc;
close all;

% Read in .tif image
img = imread('sunflower.tif');

% Convert image to grayscale and cast to double 
% (allows for SVD). Grayscale is easier to work with
imgGray = im2double(rgb2gray(img));

% Get dimensions of original image
[width, height, depth] = size(imgGray);

% Find the maximum number of terms for k
k = ceil((width*height)/(width+height+1));
compRatio = zeros(1, k);

% Apply svd to grayscale image matrix and get U, S, and V
[U,S,V] = svd(imgGray);

for i = 1:1:k
    
% Set all values that are not the number of terms we want 
% equal to 0
 D = S;
 D(i+1:end , i+1:end) = 0;
 
 % Define new compressed matrix
 newImgGray = U * D * V';   
  
 % Calculate compression ratio
 compRatio (i) = round((width*height)/(i *(width + height + 1)),2);
end

% Plot 
plot(compRatio, 1:1:k)
title('Number of k Terms vs Compression Ratio');
xlabel('k terms');
ylabel('Compression Ratio');