image compression(wavelet)

图像压缩算法

理论基础

香农的信息论,在不产生任何失真的前提下,通过合理的编码,对于每个信源符号分配不等长的码字,平均码长可以任意接近于信源的熵。在这个理论框架下产生了几种不同的无失真的信源编码方法:哈夫曼编码,算术编码、字典编码等。后来出现了更多的编码方式:如KLT编码、分形编码、模型编码、子带编码和基于小波的编码方法。

  • Image compression algorithm

    Theoretical basis

    In Shannon's information theory, the average code length can be arbitrarily close to the entropy of the source by assigning different code words to each source symbol through reasonable coding without any distortion.Under the framework of this theory, several different undistorted source coding methods are produced: Huffman coding, arithmetic coding, dictionary coding, etc.Later, more coding methods emerged, such as KLT coding, fractal coding, model coding, subband coding, and wavelet based coding.

压缩的必要性和可能性

存储和传输上的消耗。

图像数据的冗余数据:同一帧临近位置的数据相同或相似;连续帧图像数据有大量相同的数据;人眼对图像分辨率的局限性、监视器显示分辨率的限制,容许一定限度的失真

  • The necessity and possibility of compression

    Consumption on storage and transport.

    Redundant data of image data: the data near the same frame are identical or similar;Continuous frame image data has a lot of the same data;The limitation of human eyes on image resolution and the limitation of monitor display resolution allow certain limit of distortion.

方法简介

基于信息论角度

冗余度压缩方法:(无损压缩),在数学上可逆:哈夫曼编码-算术编码-行程编码-Lempel-zev编码

信息量压缩方法:(有损压缩),解码图像与原始图像有差:预测编码-频率域方法(正交变换编码如DCT、子带编码)-空间域方法(统计分块编码)-模型方法(分形编码、模型编码)-基于重要性(粒子、子采样、比特分配、矢量量化)

  • Methods introduction

    Based on information theory

    Redundancy compression method: lossless compression, mathematically reversible: Huffman coding - arithmetic coding - stroke coding - Lempel-ZEv coding

    Information compression method (lossy compression), decoded image and original image there: predictive coding - frequency domain method (orthogonal transform coding such as DCT, subband coding) - spatial domain methods (statistical block coding) - model (model of fractal coding, coding) - based on importance (particle, sampling, bit allocation, vector quantization)

按照压缩技术所依据和使用的数学方法进行分类

预测编码:基本思想是根据数据统计特性得到预测值,然后传输图像像素与其预测值的差值信号,使传输的码率降低,达到压缩的目的。

统计编码:主要针对无记忆信源,根据信息码字出现概率的分布特征而进行压缩编码,寻找概率与码字长度间的最优匹配,其编码的实质就是用短码来表示较大的码字,而用长码表示出现概率较小的码字。常用的有:行程编码哈夫曼编码算术编码三种。

变换编码:基本思想是利用图像块内像素之间的相关性,把图像变换到一组新的基(一般是频率域)上,使得能量集中到少数几个变换系数上,通过存储这些系数达到压缩数据的目的。常用的有:DCT-整数DCT-小波变换。

  • The classification is based on the mathematical methods used in compression techniques

    Prediction coding: the basic idea is to get the predicted value according to the statistical characteristics of data, and then transmit the difference signal between the image pixel and the predicted value, so as to reduce the transmission bit rate and achieve the purpose of compression.

    Statistical coding: Compression coding is mainly aimed at memoryless information sources, according to the distribution characteristics of the occurrence probability of information code words, to find the optimal match between the probability and the length of the code word. The essence of its coding is to use short code to represent the larger code words, and use long code to represent the code words with smaller occurrence probability.Commonly used are: stroke code, Huffman code and arithmetic code.

    Transform coding: The basic idea is to use the correlation between pixels in the image block, transform the image to a new set of basis (usually frequency domain), so that the energy can be concentrated on a few transform coefficients, and achieve the purpose of data compression by storing these coefficients.Commonly used are: CT- integer CT- wavelet transform.

图像压缩编码的评价标准

1、一个是对图像质量的评价客观主观。主观就是用人去观察评分;客观就是对压缩还原后的图像原始图像误差进行定量计算。一般都是进行某种平均,得到均方误差;另一种是信噪比

2、一个是对图像压缩效果的评价压缩比=原始图像每像素的比特数同压缩后图像每像素的比特数的比值

  • Evaluation criteria of image compression coding

    1. One is the evaluation of image quality: objective and subjective.Subjective is that people observe the score;Objective is to quantify the error between the compressed and restored image and the original image.Generally, we take some kind of average and get the MSE;The other is the signal to noise ratio.

    2. One is the evaluation of image compression effect: compression ratio = the ratio between the number of bits per pixel of the original image and the number of bits per pixel of the compressed image.

CODE:

clear
%装入图像
X=imread('send_src1564018005.42.jpg');
tic
%显示图像
subplot(2,2,1);
image(X);
title('原始图像');
axis square
disp('压缩前图像X的大小');
whos('X')
%对图像用小波进行层小波分解
[c,s]=wavedec2(X,2,'db1');
%提取小波分解结构中的一层的低频系数和高频系数
cal=appcoef2(c,s,'db1',1);
%水平方向
ch1=detcoef2('h',c,s,1);
%垂直方向
cv1=detcoef2('v',c,s,1);
%斜线方向
cd1=detcoef2('d',c,s,1);
%各频率成份重构
a1=wrcoef2('a',c,s,'db1',1);
h1=wrcoef2('h',c,s,'db1',1);
v1=wrcoef2('v',c,s,'db1',1);
d1=wrcoef2('d',c,s,'db1',1);
c1=[a1,h1;v1,d1];
%显示分频信息
subplot(2,2,2);
c1=uint8(c1);
image(c1);
axis square;
title ('分解后低频和高频信息'); 
%进行图像压缩
%保留小波分解第一层低频信息
%首先对第一层信息进行量化编码
ca1=appcoef2(c,s,'db1',1);
ca1=wcodemat(ca1,440,'mat',0);
%改变图像高度并显示
ca1=0.5*ca1;
ca1=uint8(ca1*1.5);
subplot(2,2,3);
image(ca1);
axis square;
title('第一次压缩图像');
disp('第一次压缩图像的大小为:');
whos('ca1')
toc
disp(['运行时间: ',num2str(toc)]);
%保留小波分解第二层低频信息进行压缩
ca2=appcoef2(c,s,'db1',2);
%首先对第二层信息进行量化编码
ca2=wcodemat(ca2,440,'mat',0); 
%改变图像高度并显示
ca2=0.5*ca2;
ca2=uint8(ca2*1.5);
subplot(2,2,4);
image(ca2);
axis square;
title('第二次压缩图像');
disp('第二次压缩图像的大小为:');
whos('ca2')
toc
disp(['运行时间: ',num2str(toc)]);
imwrite(ca2,'output.jpg'); %保存压缩前图像

result:

压缩前图像X的大小
  Name        Size                Bytes  Class    Attributes

  X         480x640x3            921600  uint8              

第一次压缩图像的大小为:
  Name        Size                Bytes  Class    Attributes

  ca1       240x320x3            230400  uint8              

时间已过 3.147600 秒。
运行时间: 3.1635
第二次压缩图像的大小为:
  Name        Size               Bytes  Class    Attributes

  ca2       120x160x3            57600  uint8              

时间已过 3.206097 秒。
运行时间: 3.2062

 

基于DCT的图像压缩及Matlab实现:

  • Image compression and Matlab based on DCT:

JPEG2000静态图像编码标准中的图像变换技术就采用了离散小波变换。最大的特点是在不丢失重要信息的同时,能以比较高的比率压缩图像,并且算法计算量小。

  • The image transformation technique in JPEG2000 static image coding standard USES discrete wavelet transform.The most important feature is that the image can be compressed at a relatively high rate without losing important information, and the computation of the algorithm is small.

在http协议中,传输的是bytes,请求的一些列string 先通过encode转化成bytes ,然后在服务器端,再进行decode,成 string 最后返回相应的时候 还要通过encode 转化成bytes才行.

  • In the HTTP protocol, bytes are transmitted. Some column strings requested are converted into bytes through encode, then decode at the server side, and finally convert into Bytes through encode when the corresponding is returned.

 图像编码和解码具体算法核心是离散余弦变换Huffman编码算术编码等技术等。
 在计算机中, 图像是由一个个像素点组成,像素点就是颜色点,而颜色最简单的方式就是用RGBRGBA表示。图像二维数据,数据在内存中只能一维存储,二维转一维有不同的对应方式。比较常见的只有两种方式: 按像素“行排列”从上往下或者从下往上。像素是构成图像的最小单位。

  • The core of image coding and decoding algorithms are discrete cosine transform, Huffman coding, arithmetic coding and other techniques.

    In a computer, an image is made up of pixels, which are color points, and the easiest way to represent color is with RGB or RGBA.The image is a two-dimensional data, data can only be stored in one dimension in memory, two dimensional to one dimensional has different corresponding ways.There are only two common ways: from top to bottom or from bottom to top in "rows" of pixels.The pixel is the smallest unit of an image.

图像分割越细,像素数越多,取样点越多,图像清晰度越高;反之,像素数越少,图像清晰度越低。实际上,对图像压缩处理,就是对图像区块的数据、像素的数据进行压缩处理

静止图像的数据压缩技术,即帧内数据压缩技术;

活动图像的数据压缩技术,即帧间数据压缩技术

  • The finer the image segmentation, the more pixels, the more sample points, the higher the image definition;Conversely, the fewer pixels, the lower the image resolution.In fact, the image compression processing, is the image block data, pixel data compression processing.

  • Still image data compression technology, namely in-frame data compression technology;

  • The moving image data compression technology, namely the frame data compression technology.

离散余弦变换简称为DCT(是英Discrete Cosine Transform的缩写词),是一种数字处理方法,经常用于数据处理。DCT是多种数字变换方法的一种,它是把空间域图像变换到频率域进行分析的方法。由于DCT的变换核构成的基向量与图像内容无关,而且变换核是可以分离的,既二维DCT可以用两次一维DCT来完成,使得数学运算难度大大简化,再配以已经发现的其它快速算法,使得DCT编码得到了广泛的应用。

  • Discrete Cosine Transform, also known as DCT (short for the British Discrete Cosine Transform), is a numeric processing method often used in data processing.DCT is one of many digital transformation methods. It is a method to transform the spatial image to the frequency domain for analysis.As the basis vector of DCT transformation core is independent of the image content and the transformation core can be separated, two-dimensional DCT can be completed by two one-dimensional DCT, which greatly simplifies the mathematical operation difficulty. Coupled with other fast algorithms already found, DCT coding has been widely used.

DCT应用于图像数据压缩,可以减少代表图像亮度(或色度)层次数码信息,达到数据压缩的目的。利用DCT不仅可将图像编码,还可以在编码变换过程发现图像细节的位置,以便删去或略去对视觉不敏感的部分,而更加突出视觉的敏感部分,通过选择主要数据来传输、重组图像

  • The application of DCT in image data compression can reduce the digital information representing the image brightness (or chroma) level and achieve the purpose of data compression.DCT can not only encode the image, but also find the location of the image details in the encoding transformation process, so as to delete or omit the visually insensitive part, and highlight the visually sensitive part, and transmit and recombine the image by selecting the main data.

-- 离散余弦变换(DCT)编码

利用DCT压缩图像数据,主要是根据图像信号在频率域的统计特性。在空间域看来,图像内容千差万别;但在频率域上,经过对大量图像的统计分析发现,图像经过DCT变换后,其频率系数的主要成分集中于比较小的范围,且主要位于低频部分。利用DCT变换揭示出这种规律后,可以再采取一些措施把频谱中能量较小的部分舍弃,尽量保留传输频谱中主要的频率分量,就能够达到图像数据压缩目的

  • -- Discrete cosine transform (DCT) coding

    Using DCT to compress image data is mainly based on the statistical characteristics of image signal in frequency domain.In the spatial domain, the image content is very different;However, in the frequency domain, after statistical analysis of a large number of images, it is found that the main components of the frequency coefficient of images after DCT transformation are concentrated in a relatively small range and mainly located in the low-frequency part.After this rule is revealed by DCT transformation, some measures can be taken to remove the part with less energy in the spectrum, and the main frequency component in the transmission spectrum can be retained as far as possible, so as to achieve the purpose of image data compression.

 图像是由亮度、色度信息来描述的,在各相邻帧图像内,若分别比较同一相对位置的亮度、色度信号,通常其差别较小。经大量统计发现,在各个像素当中仅有10%以下的像素点的亮度差值变化超过去时2%,而色度差值变化在0.1%以下。在各帧图像中具有大量重复内容,这些重复内容的数据属于多余(冗余)信息,于是,可以通过减少时域冗余信息的方法,即运作帧间数据压缩技术,来减少图像传输的数码率。

  • The image is described by the brightness and chromaticity information. If the brightness and chromaticity signals at the same relative position are compared in each adjacent frame image, the differences are usually small.A large number of statistics found that among each pixel, only less than 10% of the pixel point brightness difference changes more than 2%, while the chroma difference changes less than 0.1%.There is a large amount of duplicate content in each frame, and the data of the duplicate content belongs to redundant (redundant) information. Therefore, the digital rate of image transmission can be reduced by the method of reducing the redundant information in the time domain, namely, the operation of inter-frame data compression technology.

code:

%%=========================================================================
%%基于DCT的图像压缩及Matlab实现
%%=========================================================================
RGB=imread('srcimage.jpg');%读取图片
tic
%RGB=imresize(RGB,[480,640]);%因为1.bmp大小为169*220,所以我改为168*224
%imwrite(RGB,'start.jpg'); %保存压缩前图像
%下面是对RGB三个分量进行分离,此时他们依然为整数
R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
figure,imshow(RGB),title('原来的RGB图像');


%下面是进行测试:由压缩前.jpg,压缩后.jpg,压缩后后.jpg,压缩后后后.jpg的大小可以知道,matlab自身对图像有着压缩功能
%imwrite(RGB,'压缩前.jpg'); %保存压缩前图像
%由于1.jpg和压缩前.jpg大小差距很大,从中可以看出matlab对图像进行了压缩
%RGB=imread('压缩前.jpg');%读取图片
%imwrite(RGB,'压缩后.jpg'); %保存压缩前图像

%RGB=imread('压缩后.jpg');%读取图片
%imwrite(RGB,'压缩后后.jpg'); %保存压缩前图像

%RGB=imread('压缩后后.jpg');%读取图片
%imwrite(RGB,'压缩后后后.jpg'); %保存压缩前图像
%测试结束


%RGB->YUV
Y=0.299*double(R)+0.587*double(G)+0.114*double(B);
U=-0.169*double(R)-0.3316*double(G)+0.5*double(B);
V=0.5*double(R)-0.4186*double(G)-0.0813*double(B);
YUV=cat(3,Y,U,V);%YUV图像
figure,imshow(uint8(YUV)),title('通过计算得到的YUV图像')

T=dctmtx(8);%产生一个8*8的DCT变换举证


%进行DCT变换 BY BU BV是double类型
BY=blkproc(Y,[8 8],'P1*x*P2',T,T');
BU=blkproc(U,[8 8],'P1*x*P2',T,T');
BV=blkproc(V,[8 8],'P1*x*P2',T,T');

a=[16 11 10 16 24 40 51 61;
      12 12 14 19 26 58 60 55;
      14 13 16 24 40 57 69 55;
      14 17 22 29 51 87 80 62;
      18 22 37 56 68 109 103 77;
      24 35 55 64 81 104 113 92;
      49 64 78 87 103 121 120 101;
      72 92 95 98 112 100 103 99;]; %量化值
  
  b=[17 18 24 47 99 99 99 99;
      18 21 26 66 99 99 99 99;
      24 26 56 99 99 99 99 99;
      47 66 99 99 99 99 99 99;
      99 99 99 99 99 99 99 99;
      99 99 99 99 99 99 99 99;
       99 99 99 99 99 99 99 99;
       99 99 99 99 99 99 99 99;];
   
   %BY2 BU2 BV2是double类型
   BY2=blkproc(BY,[8 8],'x./P1',a);
   BU2=blkproc(BU,[8 8],'x./P1',b);
   BV2=blkproc(BV,[8 8],'x./P1',b);
   %这里进行取整量化,BY3 BU3 BV3是uint8类型
   BY3=int8(BY2);
   BU3=int8(BU2);
   BV3=int8(BV2);
   
   %BY4 BU4 BV4是double类型
   BY4=blkproc(double(BY3),[8 8],'x.*P1',a);
   BU4=blkproc(double(BU3),[8 8],'x.*P1',b);
   BV4=blkproc(double(BV3),[8 8],'x.*P1',b);
   
   %测试代码
   %BY4=blkproc(BY2,[8 8],'x.*P1',a);
   %BU4=blkproc(BU2,[8 8],'x.*P1',b);
   %BV4=blkproc(BV2,[8 8],'x.*P1',b);
   
   mask=[
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;
         1 1 1  1 1 1 1 1;];
     %BY5 BU5 BV5是double类型
     BY5=blkproc(BY4,[8 8],'P1.*x',mask);
     BU5=blkproc(BU4,[8 8],'P1.*x',mask);
     BV5=blkproc(BV4,[8 8],'P1.*x',mask);
     
     
     
     
     %YI UI VI是double类型
     YI=blkproc(double(BY5),[8 8],'P1*x*P2',T',T);
     UI=blkproc(double(BU5),[8 8],'P1*x*P2',T',T);
     VI=blkproc(double(BV5),[8 8],'P1*x*P2',T',T);
   
   %YUVI是double类型
    YUVI=cat(3,uint8(YI),uint8(UI),uint8(VI));%经过DCT变换和量化后的YUV图像
    figure,imshow(YUVI),title('经过DCT变换和量化后的YUV图像');
    
    RI=YI-0.001*UI+1.402*VI;
    GI=YI-0.344*UI-0.714*VI;
    BI=YI+1.772*UI+0.001*VI;
    RGBI=cat(3,RI,GI,BI);%经过DCT变换和量化后的YUV图像
    RGBI=uint8(RGBI);
    figure,imshow(RGBI),title('经过DCT变换和量化后的RGB图像');
    toc
    disp(['运行时间: ',num2str(toc)]);
    imwrite(RGBI,'end.jpg'); %保存压缩图像 

result:

 I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值