clear;
f0=imread('lena.jpg');
subplot(121)
imshow(uint8(f0));
xlabel('\fontsize{16}原始圖像');
f=abs(f0/4)-10;
[M,N]=size(f);
p=zeros(1,61);
for t=1:61
count=0;
for i=1:M
for j=1:N
if f(i,j)==t-1
count=count+1;
end
end
end
p(t)=count;p0=p;
end
core=cell(61,1);
sign=zeros(61);
for hh=1:60
re=M*N;
for t=1:61
if (p(t)<re)&(p(t)>0)
re=p(t);
end
end
t=1;
while (p(t)~=re)&(t<61)
t=t+1;
end
if sign(t,1)==0
core{t}='0';
else
core{t}=['0',core{t}];
i=1;
while (sign(t,i)~=0)&(i<61)
core{sign(t,i)}=['0',core{sign(t,i)}];
i=i+1;
end
end
p(t)=0;
cou=t;
re1=M*N;
for t=1:61
if (p(t)<re1)&(p(t)>0)
re1=p(t);
end
end
t=1;
while (p(t)~=re1)&(t<61)
t=t+1;
end
if sign(t,1)==0
core{t}='1';
else
core{t}=['1',core{t}];
i=1;
while (sign(t,i)~=0)&(i<61)
core{sign(t,i)}=['1',core{sign(t,i)}];
i=i+1;
end
end
p(t)=p(t)+re;
cou1=t;
i=1;
while (sign(t,i)~=0)&(i<61)
i=i+1;
end
sign(t,i)=cou;
i=i+1;
j=1;
while (sign(cou,j)~=0)&(j<61)
sign(t,i)=sign(cou,j);
i=i+1;
j=j+1;
end
end %產生huffman碼
fc=cell(M,N);
for i=1:M
for j=1:N
if f(i,j)<61
fc{i,j}=core{f(i,j)+1};
else
fc{i,j}='0';
end
end
end %fc
imcore=char();
for i=1:M
for j=1:N
imcore=[imcore,fc{i,j}];
end
end
save picture imcore core; %保存圖片碼流和編碼對應表
- clear;
- load picture.mat %載入圖片碼流和編碼對應表
- Nc=size(core);
- Nic=size(imcore);
- flag=0;
- i=1;
- j=1;
- n=1;
- cz=char();
- f=zeros(128);
- for n=1:400930
- if flag==0
- cz=[cz,imcore(n)];
- else
- cz=imcore(n);
- flag=0;
- end
- for t=1:61
- if strcmp(cz,core{t})
- f(j,i)=t;
- flag=1;
- if i>127;
- i=1;
- j=j+1;
- else
- i=i+1;
- end
- break;
- end
- end
- end
- f=uint8(f*4+35);
- subplot(122)
- imshow(f);xlabel('\fontsize{16}解碼后的圖像');
復制代碼
tuxiang.rar
(33.29 KB, 下載次數: 7)
2016-1-30 20:24 上傳
點擊文件名下載附件
|