Skip to content

Instantly share code, notes, and snippets.

@cryptospectrum
Created March 14, 2013 01:31
Show Gist options
  • Save cryptospectrum/5158094 to your computer and use it in GitHub Desktop.
Save cryptospectrum/5158094 to your computer and use it in GitHub Desktop.
Singular Value Decomposition and Compression of an Image Demo
G=imread('garlic_chive_flowers-color.tif','tif');
%G=imread('Cavolfiore_Di_Sicilia_Violetto.jpg','jpg');
G=double(G);
MG1 = ones(size(G(:,:,1),1),1)*mean(G(:,:,1));
MG2 = ones(size(G(:,:,2),1),1)*mean(G(:,:,2));
MG3 = ones(size(G(:,:,3),1),1)*mean(G(:,:,3));
G1 = G(:,:,1)-MG1;
G2 = G(:,:,2)-MG2;
G3 = G(:,:,3)-MG3;
[UG1,SG1,VG1]=svd(G1);
[UG2,SG2,VG2]=svd(G2);
[UG3,SG3,VG3]=svd(G3);
compress=0;
headr='svd_movie_02';
movname=strcat(headr,'.avi');
m=1;
while exist(movname, 'file')~=0,
m=m+1;
movname= sprintf('%s_%02d.avi', headr, m);
end
if compress
mov = avifile( movname, 'compression', 'Cinepak','keyframe',5', 'fps', 2,'quality', 100);
else
mov = avifile(movname, 'compression', 'None', 'fps', 1);
end
fig=figure;
set(fig,'DoubleBuffer','on');
set(gca,'xlim',[-80 80],'ylim',[-80 80],'NextPlot','replace','Visible','off')
r=[size(G1,1):-145:100, 90:-10:50, 45:-5:30, 30:-2:1,1, 1, 1, 1:5:56];
for p=1:length(r)
GI1=MG1+UG1(:,1:r(p))*SG1(1:r(p),1:r(p))*VG1(:,1:r(p))';
GI2=MG2+UG2(:,1:r(p))*SG2(1:r(p),1:r(p))*VG2(:,1:r(p))';
GI3=MG3+UG3(:,1:r(p))*SG3(1:r(p),1:r(p))*VG3(:,1:r(p))';
GF(:,:,1)=uint8(round(GI1));
GF(:,:,2)=uint8(round(GI2));
GF(:,:,3)=uint8(round(GI3));
h =image(GF);
axis off;
axis fill;
set(h,'EraseMode','xor');
F = getframe(gca);
mov = addframe(mov,F);
end
mov = close(mov);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment