Ip=imread('lenah1.bmp'); %装入原始图象
figure(1);
imshow(Ip)
title('原始图象') %显示原图
I=double(Ip); %将数据类型转换为双精度形式
T=dctmtx(8); %产生8*8的DCT变换系数矩阵
B=blkproc(I,[8 8],'P1*x*P2',T,T'); %对图象I的每个8*8块的DCT变换
Q=[16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 60
18 22 37 56 68 109 103 77
24 25 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99]; %标准量化矩阵
B1=blkproc(B,[8 8],'round(x./P1)',Q);%对DCT系数矩阵进行有损量化
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Z扫描 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sx=[1 2 6 7 15 16 28 29
3 5 8 14 17 27 30 43
4 9 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64];
out=zeros([1 32*32*64]);
for a=1:32
for b=1:32
for i=1:8
for j=1:8
out(((a-1)*32+b)*64-64+sx(i,j))=B1(a*8-8+i,b*8-8+j);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 稀疏矩阵 游程编码 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
r=1; zero_num=0;
for i=1:65536
if out(i)~=0
YC(r,1)=zero_num;
YC(r,2)=out(i);
r=r+1;
zero_num=0;
else zero_num=zero_num+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Huffman编码 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% 对游程编码后的第一分量(0游程的个数)进行 Huffman编码(1) %%%%%%%%%%%
YC1=YC(:,1);[x,y]=size(YC)
sig=zeros([max(x,y),3]);
k=1; sig(1,1)=YC1(1);
for i=1:max(x,y)
flag=0;
for j=1:k
if sig(j,1)==YC1(i)
sig(j,2)=sig(j,2)+1;
flag=1;
break
end
end
if flag==0
k=k+1;
sig(k,1)=YC1(i);
sig(k,2)=1;
end
end
sig=sig(1:k,:);
SUM=sum(sig(:,2));
sig(:,3)=sig(:,2)/SUM;
[dict1,avglen1] = huffmandict(sig(:,1),sig(:,3)); % Create dictionary.
actualsig1 = YC1; % actual data
comp1 = huffmanenco(actualsig1,dict1); % Encode the data.
%%% 对游程编码后的第二个分量(幅度)进行 Huffman编码(2) %%%%%%%%%%%
YC2=YC(:,2);[x,y]=size(YC)
sigg=zeros([max(x,y),3]);
k=1; sigg(1,1)=YC2(1);
for i=1:max(x,y)
flag=0;
for j=1:k
if sigg(j,1)==YC2(i)
sigg(j,2)=sigg(j,2)+1;
flag=1;
break
end
end
if flag==0
k=k+1;
sigg(k,1)=YC2(i);
sigg(k,2)=1;
end
end
sigg=sigg(1:k,:);
SUM=sum(sigg(:,2));
sigg(:,3)=sigg(:,2)/SUM;
[dict2,avglen2] = huffmandict(sigg(:,1),sigg(:,3)); % Create dictionary.
actualsig2 = YC2; % actual data
comp2 = huffmanenco(actualsig2,dict2); % Encode the data.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Huffman 解码 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
dsig1 = huffmandeco(comp1,dict1); % 对游程编码后的第一分量(0游程的个数)进行 Huffman解码(1)
dsig2 = huffmandeco(comp2,dict2); % 对游程编码后的第二分量(幅度)进行 Huffman解码(2)
%%%%%% 合并成原始游程编码序列 YCC %%%%%%
[x,y]=size(dsig1);
YCC=zeros([max(x,y),2]);
YCC(:,1)=dsig1;
YCC(:,2)=dsig2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 稀疏矩阵 游程解码 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
out1=zeros([1 32*32*64]);
[x,y]=size(YCC); t=1;
for i=1:x
if YCC(i,1)==0
out1(t)=YCC(i,2);
else
t=t+YCC(i,1);
out1(t)=YCC(i,2);
end
t=t+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 反Z扫描 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for a=1:32
for b=1:32
for i=1:8
for j=1:8
B11(a*8-8+i,b*8-8+j)=out1(((a-1)*32+b)*64-64+sx(i,j));
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
B2=blkproc(B11,[8 8],'x.*P1',Q); %对DCT系数矩阵进行反量化
B3=blkproc(B2,[8 8],'P1*x*P2',T',T); %进行反DCT变化后重构原图
B4=uint8(B3); %转换数据类型
figure(2);
imshow(B4)
title('压缩后的图象') %显示压缩后的图像
%%%%%%%%%%%%%%%%%%%%%%%% 计算峰值信噪比 PSNR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p=(Ip-B4).^2;
h=sum(p);
h1=sum(h');
mse=h1/256/256;
psnr=10*log10(255*255/mse)
%运行结果 psnr =36.3820
评论1