📄 encode_main.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% encode %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% by zzf %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% started on 11.6 2006 %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function encode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT u0 = original image
% OUTPUT: dwt image and rec image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
%%%%%%%%%%%%%%%%%% original image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%the beginning
% read the original data u0 in BMP format
u0=imread('Lena.bmp');
u0=double (u0);
% cut a piece, convert to double
u0=double(u0); [M0 N0]=size(u0);
% visualize the image u0 in Matlab (rescaled)
figure(1); subplot(1,2,1);
imagesc(u0); axis image;
title('original data');
% image(u0)
% axis off;
colormap(gray);
%%%%%%%%%%%%%%%%%%%%%%% wavedec %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[a1,h1,v1,d1]=dwt2(u0,'bior3.7');
[a2,h2,v2,d2]=dwt2(a1,'bior3.7');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameter
%---max max of u0(i,j)
%---min min of u0(i,j)
%---delta
%---step number of different values
%---step_i
[M N]=size(a2)
max=0;min=256;
step=170;
for i=1:M
for j=i:N
if a2(i,j)>max
max=a2(i,j);
end
if a2(i,j)<min
min=a2(i,j);
end
end
end
delta=(max-0.1-min)/(step-1);
%------------------------probility-------------------------------
pro=zeros(1,step);
data_table=zeros(1,M*N);
data_k=0;
for i=1:M
for j=1:N
data_k=data_k+1;
step_i=round((a2(i,j)-min)/delta)+1;
a2(i,j)=min+(step_i-1)*delta+delta/2;
pro(step_i)=pro(step_i)+1/(M*N);
data_table(1,data_k)=step_i;
end
end
%-------------------------- rate -----------------------------
h=0;
for i=1:step
if pro(i)~=0
h=h-pro(i)*log2(pro(i))
end
end
rate=8*M0*N0/h/(M*N)
%%%%%%%%%%%%%%%%%% huffman end encode %%%%%%%%%%%%%%%%%%%%%%%%%%%%
huff_table=HuffmanTree(pro);
code_table=encode(huff_table);
for i=1:(M*N)
if i==1
code_out=code_table(1,:);
else
p=data_table(1,i);
code_out=strvcat(code_out,code_table(p,:))
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% decode and rec %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A3=zeros(75,75);
for i=1:M*N
code=deblank(code_out(i,:));
step_i=strmatch(code,code_table);
data_table(1,i)=min+(step_i-1)*delta+delta/2;
end
for i=1:M
k=((i-1)*N+1);
a2(i,:)=data_table(1,k:(k+N-1));
end
a1=idwt2(a2,[],[],[],'bior3.7');
% A3=wkeep(A4,[135 135]);
u=idwt2(a1,[],[],[],'bior3.7');
subplot(1,2,2);
imagesc(u); axis image;
title('rec picture');
% image(u0)
% axis off;
colormap(gray);
%%%%%%%%%%%%%%%%%% psnr %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------- psnr--------------------------------------------------
sum=0;
for i=1:M0
for j=1:N0
k=u0(i,j)-u(i,j);
sum=sum+k^2;
end
end
sum=sum/(M0*N0);
sum=255*255/sum;
PSNR=10*log10(sum)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% encode %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% by zzf %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% started on 11.9 2006 %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -