实验三、图像压缩编码技术
一、实验目的
1、理解有损压缩和无损压缩的概念;
2、理解图像压缩的主要原则和目的; 3、了解几种常用的图像压缩编码方式; 4、利用MATLAB程序进行图像压缩编码。
二、实验原理
1、图像压缩原理
图像压缩主要目的是为了节省存储空间,增加传输速度。图像压缩的理想标准是信息丢失最少,压缩比例最大。不损失图像质量的压缩称为无损压缩,无损压缩不可能达到很高的压缩比;损失图像质量的压缩称为有损压缩,高的压缩比是以牺牲图像质量为代价的。压缩的实现方法是对图像重新进行编码,希望用更少的数据表示图像。
信息的冗余量有许多种,如空间冗余,时间冗余,结构冗余,知识冗余,视觉冗余等,数据压缩实质上是减少这些冗余量。高效编码的主要方法是尽可能去除图像中的冗余成分,从而以最小的码元包含最大的图像信息。
编码压缩方法有许多种,从不同的角度出发有不同的分类方法,从信息论角度出发可分为两大类。
(1)冗余度压缩方法,也称无损压缩、信息保持编码或熵编码。具体说就是解码图像和压缩编码前的图像严格相同,没有失真,从数学上讲是一种可逆运算。
(2)信息量压缩方法,也称有损压缩、失真度编码或烟压缩编码。也就是说解码图像和原始图像是有差别的,允许有一定的失真。
应用在多媒体中的图像压缩编码方法,从压缩编码算法原理上可以分为以下3类: (1)无损压缩编码种类
哈夫曼(Huffman)编码,算术编码,行程(RLE)编码,Lempel zev编码。 (2)有损压缩编码种类
预测编码,DPCM,运动补偿;
频率域方法:正交变换编码(如DCT),子带编码; 空间域方法:统计分块编码;
模型方法:分形编码,模型基编码;
基于重要性:滤波,子采样,比特分配,向量量化; (3)混合编码。
有JBIG,H.261,JPEG,MPEG等技术标准。
本实验主要利用MATLAB程序进行赫夫曼(Huffman)编码和行程编码(Run Length Encoding, RLE)。
三、实验内容
1、实现基本JPEG的压缩和编码分三个步骤: (1)首先通过DCT变换去除数据冗余; (2)使用量化表对DCT系数进行量化; (3)对量化后的系数进行Huffman编码。
四、实验步骤
1打开计算机,启动MATLAB程序;
2选择一幅图像,并进行赫夫曼和行程编码压缩处理;
3 将原图像在Photoshop软件中打开,分别以不同的位图文件格式进行“另保存”,比较它们的数据量。
4记录和整理实验报告
五、实验仪器
1计算机;
2 MATLAB、Photoshop等程序; 3移动式存储器(软盘、U盘等)。 4记录用的笔、纸。
六、实验报告内容
原图片
1、赫夫曼处理 源程序
function Huffman() clear
X=imread('456.jpg'); data=uint8(X);
[zipped,info]=huffencode(data); unzipped=huffdecode(zipped,info); subplot(121);imshow(data); subplot(122);imshow(unzipped);
%erms = compare(data(:),unzipped(:)); erms=0
cr=info.ratio
whos data unzipped zipped
function [zipped,info]=huffencode(vector) if ~isa(vector,'uint8')
error('input argument must be a uint8 vector'); end
[m,n]=size(vector); vector=vector(:)';
f=frequency(vector); simbols=find(f~=0); f=f(simbols);
[f,sortindex]=sort(f); simbols=simbols(sortindex); len=length(simbols);
simbols_index=num2cell(1:len); codeword_tmp=cell(len,1);
while length(f)>1 index1=simbols_index{1}; index2=simbols_index{2};
codeword_tmp(index1)=addnode(codeword_tmp(index1),uint8(0)); codeword_tmp(index2)=addnode(codeword_tmp(index2),uint8(1)); f=[sum(f(1:2)) f(3:end)];
simbols_index=[{[index1,index2]} simbols_index(3:end)]; [f,sortindex]=sort(f);
simbols_index=simbols_index(sortindex); end
codeword=cell(256,1);
codeword(simbols)=codeword_tmp; len=0;
for index=1:length(vector) len=len+length(codeword{double(vector(index))+1}); end
string=repmat(uint8(0),1,len); pointer=1;
for index=1:length(vector) code=codeword{double(vector(index))+1}; len=length(code);
string(pointer+(0:len-1))=code; pointer=pointer+len; end
len=length(string);
pad=8-mod(len,8); if pad>0
string=[string uint8(zeros(1,pad))]; end
codeword=codeword(simbols); codelen=zeros(size(codeword)); weights=2.^(0:23); maxcodelen=0;
for index=1:length(codeword) len=length(codeword{index}); if len>maxcodelen
maxcodelen=len; end
if len>0
code=sum(weights(codeword{index}==1)); code=bitset(code,len+1); codeword{index}=code; codelen(index)=len; end end
codeword=[codeword{:}]; cols=length(string)/8;
string=reshape(string,8,cols); weights=2.^(0:7);
zipped=uint8(weights*double(string)); huffcodes=sparse(1,1);
for index=1:nnz(codeword)
huffcodes(codeword(index),1)=simbols(index); end
info.pad=pad;
info.huffcodes=huffcodes;
info.ratio=cols./length(vector); info.length=length(vector); info.maxcodelen=maxcodelen; info.rows=m; info.cols=n;
function vector=huffdecode(zipped,info,image) if ~isa(zipped,'uint8')
error('input argument must be a uint8 vector'); end
len=length(zipped);
string=repmat(uint8(0),1,len.*8); bitindex=1:8; for index=1:len
string(bitindex+8.*(index-1))=uint8(bitget(zipped(index),bitindex)); end
string=logical(string(:)'); len=length(string);
string((len-info.pad+1):end)=[]; len=length(string); weights=2.^(0:51);
vector=repmat(uint8(0),1,info.length); vectorindex=1; codeindex=1; code=0;
for index=1:len
code=bitset(code,codeindex,string(index)); codeindex=codeindex+1;
byte=decode(bitset(code,codeindex),info); if byte>0
vector(vectorindex)=byte-1; codeindex=1; code=0;
vectorindex=vectorindex+1; end end
vector=reshape(vector,info.rows,info.cols); function codeword_new=addnode(codeword_old,item) codeword_new=cell(size(codeword_old)); for index=1:length(codeword_old)
codeword_new{index}=[item codeword_old{index}]; end
function f=frequency(vector) if ~isa(vector,'uint8')
error('input argument must be a uint8 vector'); end
f=repmat(0,1,256); len=length(vector); for index=0:255
f(index+1)=sum(vector==uint8(index)); end
f=f./len;
function byte=decode(code,info) byte=info.huffcodes(code);