﻿ 实验8 彩色图片的压缩 - 计算数学达人 - 专，学者，数值代数，微分方程数值解

### 实验8 彩色图片的压缩

imshow(I);

[m,n]=size(I(:,:,1));

O=zeros(m,n);

R=I(:,:,1);

G=I(:,:,2);

B=I(:,:,3);

R=double(R);

G=double(G);

B=double(B);

A=[O G R B];

[U S V]=svdQ(A);

k=1;

Sx=S(1:k,1:k);

while(sum(sum(Sx))/sum(sum(S))<0.95)%sum(sum(Sx)对矩阵Sx的所有元素求和；sum(sum(S))对矩阵S的所有元素求和；sum(Sx)是对矩阵各个列求和

Q=timesQ(U(:,[1:k,m+1:m+k,2*m+1:2*m+k,3*m+1:3*m+k]),[Sx,zeros(k,3*k)]);

Q=timesQ(Q,transQ(V(:,[1:k,n+1:n+k,2*n+1:2*n+k,3*n+1:3*n+k])));

[Qm,Qn]=size(Q);

Qn=Qn/4;%？？不知道，是因为四元函数的原因吗？

X(:,:,2)=Q(:,Qn+1:2*Qn);

X(:,:,1)=Q(:,2*Qn+1:3*Qn);

X(:,:,3)=Q(:,3*Qn+1:4*Qn);

figure;

imshow(uint8(X));

k=k+1;

Sx=S(1:k,1:k);

end

imshow(I);

R=I(:,:,1);

G=I(:,:,2);

B=I(:,:,3);

R=double(R);

G=double(G);

B=double(B);

[m,n]=size(R);

% O=zeros(m,n);

% A=[O G R B];

Q=[zeros(m,n) G R B];%四个元素矩阵

[U S V]=svdQ(Q);%R,G,B分别用svdQ压缩处理之后的组装

for k=1:20

%选择u的前k列，即是u0.u1u2u3的前k列。

Uk=U(:,[1:k,m+1:m+k,2*m+1:2*m+k,3*m+1:3*m+k]);

Vk=V(:,[1:k,n+1:n+k,2*n+1:2*n+k,3*n+1:3*n+k]);

Sk=[S(1:k,1:k) zeros(k,k) zeros(k,k) zeros(k,k)];

Qk=timesQ(timesQ(Uk,Sk),transQ(Vk));

[m,n]=size(Qk);%重新组装后的矩阵大小

n=n/4;%读出其长度

Gk=Qk(:,n+1:2*n);

Rk=Qk(:,2*n+1:3*n);

Bk=Qk(:,3*n+1:4*n);

%再将R,G,B提出来

Ik(:,:,1)=Rk;

Ik(:,:,2)=Gk;

Ik(:,:,3)=Bk

figure;

imshow(uint8(Ik));

end