I was given this code function svd-figure(filename) % % svd-figure(filename) % %
ID: 3605831 • Letter: I
Question
I was given this code
function svd-figure(filename)
%
% svd-figure(filename)
%
% read in an image and spit out svd approximations
more off;
A = imread(filename);
A = double(A);
m = size(A,1);
n = size(A,2);
h = size(A,3);
for i=1:h
ranks(i) = rank(A(:,:,i));
end
rmax=min(ranks);
for i=1:h
[U(i).data,S(i).data,V(i).data] = svd(A(:,:,i));
end
indices = [1:5];
%indices = [1:10, 20:10:rmax];
for r=indices
fprintf(1,'r = %d ',r);
for i=1:h
Amod(:,:,i)=U(i).data(:,1:r)*S(i).data(1:r,1:r)*V(i).data(:,1:r)';
Adiff(:,:,i) = U(i).data(:,r)*S(i).data(r,r)*V(i).data(:,r)';
end
Amod = uint8(Amod);
Adiff = uint8(Adiff);
rstring = sprintf('%d.jpg',r);
imwrite(Amod, rstring,'jpg');
end
more on;
and use it to import a photo, with Matlab’s imread command. Use the SVD to create 8:1, 4:1, and 2:1 compressed versions of the photo. Note that this compression refers to the total memory required to store the approximation. Please report how many singular values were required for each of the compression ratios.
Explanation / Answer
The SVD of orthagonal matrix will give the number of singular values
so [U,S,V] = svd(A)
% consider
A = imread(filename);
A = double(A);
[U,S,V] = svd(A)
Then result will be.. [this is sample output.. to show how output will be using SVD ]
U =
-0.1525 -0.8226 -0.3945 -0.3800
-0.3499 -0.4214 0.2428 0.8007
-0.5474 -0.0201 0.6979 -0.4614
-0.7448 0.3812 -0.5462 0.0407
S =
14.2691 0
0 0.6268
0 0
0 0
V =
-0.6414 0.7672
-0.7672 -0.6414
% so for any compresison image.. load image using imread and then pass that matrix to SVD to get number of singular values..
We can perform same operation for all compresison rations.. 8:1, 4:1, and 2:1 compressed versions of the photo
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.