You are on page 1of 4

MATLAB Scripts

% function SDVCompression_3(F, BlockSize, SP, EP)


% Image Compression, take out the mean
%
%
% F: input file, a 2-D array
% BlockSize: 64, 32, 16, 8
% SP: Staring percentage of singular values AREA used
% EP: Ending percentage of singular values AREA used
% SP < EP
%
%
% testing my idea of using cumulative singularvalues
%
% James Chen, 11/30/2000
% ECS 289K, Project
%
close all

if (SP > EP)


disp('SP must be smaller than EP!!!')
return
end

% read in the image


IMG1 = imread(F);

X1org = im2double(IMG1);
XSize = size(X1org);
figure(1);
colormap(gray);
imagesc(X1org);
title (sprintf('Original Image: %s, Size: %i x %i', F, XSize(1), XSize(2)));

% mean
m = mean(mean(X1org))

X1org = X1org - m;

% display the image


figure(2);
subplot(2,3,1);
% shown in original size: imshow(X1org);

% or: in matrix format:


colormap(gray);
imagesc(X1org+m);

%title ('Original');

NumBlocks = XSize(1)/BlockSize;

PercentageStep = (EP - SP)/4.0;


Percentage = EP;
pl = 2;
while (pl <= 6)
for i = 1:NumBlocks
r = (i-1) * BlockSize + 1;
for j = 1:NumBlocks

%disp(sprintf('Processing block %i %i', i,j))

c = (j-1) * BlockSize + 1;

TB = X1org(r:r+BlockSize-1, c: c+BlockSize-1);
[TU,TS,TV] = svd(TB);
%U(i, j, :, :) = TU;
%S(i, j, :, :) = TS;
%V(i, j, :, :) = TV;
SV = diag(TS); % a n by 1 vector

% cumulative sum of singular values


s = size(SV);
R(i,j) = s(1);
% rank for original block

TCS(1) = SV(1);

for k = 2: s(1)
TCS(k) = TCS(k-1) + SV(k);
end

%CS(i,j,:,:) = TCS;

%
% reconstruction, de-compression
%

v = TCS(s(1)) * Percentage *0.01;


% find the rank used for each block
rr = 1;
k = s(1)-1;
while (k >=1)
if ( (TCS(k) <= v) & (TCS(k+1) >= v))
rr = k+1;
break;
end;
k = k-1;
end

RUsed(i,j) = rr;
PRUsed(i,j) = rr/s(1);
%disp(sprintf('rank used = %i', rr))

X = TU(:, 1:rr) * TS(1:rr, 1:rr) * TV(:, 1:rr)';

CI(r:r+BlockSize-1, c: c+BlockSize-1) = X;

end

end

% add the mean back to image


CI = CI + m;

disp('--------------')
disp(sprintf('Rnaks used for percentage = %i:\n', Percentage))
RUsed

avg = sum(sum(RUsed))/(NumBlocks*NumBlocks);
disp(sprintf('Average ranks used = %f\n\n', avg))

disp(sprintf('Ranks used / %i:', s(1)))


PRUsed
avg = sum(sum(PRUsed))/(NumBlocks*NumBlocks);
disp(sprintf('Average percentage of ranks used = %f\n', avg))

%display
figure(2)
subplot(2,3, pl);
colormap(gray);
imagesc(CI);
title(sprintf('Percentage = %i', Percentage))

% !!!! add th emean back to the original picture

X1org = X1org + m;

Diff = X1org - CI;


figure(3)
subplot(2,3, pl);
colormap(gray);
imagesc(Diff);
title(sprintf('Difference for %i', Percentage))

figure(4)
subplot(2,3, pl);
colormap(gray);
imagesc(abs(Diff));
title(sprintf('Abs. Difference for %i', Percentage))

%figure(5)
%subplot(2,3,pl)
%bar(RUsed)
%title('Ranks used for each sub-block');
%axis([0 NumBlocks+1 0 s(1)]);

%figure(6)
%subplot(2,3,pl)
%bar(PRUsed)
%title('Percentage of the ranks used');
%axis([0 NumBlocks+1 0 1]);

Percentage = Percentage - PercentageStep;


pl = pl + 1;

% compute Peak SN ratio:


mse1 = 0;
for i = 1: XSize(1)
for j = 1: XSize(2)
mse1 = mse1 + Diff(i,j)*Diff(i,j);
end
end
mse1 = mse1 /(XSize(1) * XSize(2));
mse1 = sqrt(mse1)

PSNR = 10 * log10(255^2/mse1)

end

You might also like