Digital image processing and image compression Matlab- discrete cosine transform (with code)

1.Objectives:

1, understand the concept of lossless compression and lossy compression;
2, understood that the image compression main principles and purpose;
3, image compression using MATLAB program.
4, learning characteristics than discrete transform DCT and implementation.

2.Experiment Content:

1, image compression using matlab program, learn mat2huff, huff2mat, mat2lpc, lpc2mat, im2jpeg, jpeg2im, entropy, imratio, compare usage.
2, programming block transform image DCT discrete I, and frequency domain filtering to achieve

3.Experiment Principle:

See the book.

4.Experiment Steps Result and Conlusion:

1, operation mat2lpc, lpc2mat, entropy, imratio, compare other functions, to understand the role and characteristics.

Here Insert Picture Description
Here Insert Picture DescriptionConv: mat2lpc using spatial redundancy removal of picture coding, the image decoding using lpc2mat. Were used and nrtop entropy function image calculating entropy coding before and after, and can see a reduction in entropy. Finally imratio then calculates the compression ratio, calculated by the compare error is found 0, i.e. a perfect image transmission.

NOTE:
Out = Entropy (the Img) its physical meaning: If a graph (or a set of numbers) there are n different values, the ratio of the value of each station is p (i), i = 1 , ..., n
then the entropy function evaluation is -sum (p (i). * log2 [p (i)]), is popular in terms of seeking the corresponding p (i) for each p. * log2 [p (i ) ], then put them all together and then take the results opposite number.
A routine:
Img = imread ( '1.jpg');
% for the life of the file '1.jpg' picture read from the tags Img
% Note here that if the grayscale Img, there is only one color channel
Out = entropy (Img)% which is calculated on an entropy of the whole image. The image processing is often seen in the image into a block, and then find the entropy for each block, thus obtained is an entropy distribution.

Nrtop function : seeking entropy represents the source of entropy. It said different probabilities of the sample corresponding to the original grayscale image coding, if the smaller entropy of the probability of different samples corresponding to the greater the difference, the more samples can be separated correctly, indicating more efficient coding.

2, Huffman coding

Use mat2huff, implemented huff2mat Huffman encoding and decoding, and after the application imratio calculated compression rate encoded signal.
Here Insert Picture Description
Here Insert Picture Description

Conv: After Huffman encoding and decoding found error is zero, a perfect transmission.

3, the preparation of the lossless predictive codecs (lossless predictive coding) program that has a lower p313Figure 8.7 FIG entropy (5.4436).

Display prediction error (predictionerror) FIG.
A decoding program to decode and verify the correct decoding (compare).
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionConv: The decoded image and the original image are compared, the error is 0 can be obtained, it is understood lossless transmission. Observed prediction error histogram shows that the peak is very high, around 0 prediction and the differential spatial redundancy in addition to the majority.

4, DCT discrete transform more than

1) the input image is decomposed into blocks of 8 × 8.
2) two-dimensional DCT transform for each block.
3) the image into the frequency domain.
4) portion of each block of DCT coefficients (respectively 64 and retention coefficients 1,5,10,30) leaving only.
5) filtering the inverse DCT of the image.

1, the image display frequency domain
Here Insert Picture Description
from the figure seen laterally narrow band image, a change slower, vertical bandwidth described contain higher frequency components, changes rapidly. From the original point of view, it does meet

2, the coefficients show

This is all the data retention coefficients
Here Insert Picture Description

This is to retain a coefficient data
Here Insert Picture Description
which is data retention coefficients 5
Here Insert Picture Description
behind the emergence of the data block processing result can be seen that each block subjected to the same operation, the same number of coefficients retained

This is a reserved data 10 coefficients
Here Insert Picture Description
which are coefficient data 30 to retain
Here Insert Picture Description
to retain the results of these data are as follows:
Here Insert Picture Description
Conv: you can see from the figure, the picture after five recovery from general view, there is no much difference of course due to the coefficient obtained is not the same, there are some differences, however small, to illustrate this compression will achieve a high compression ratio. From the point of view of the coefficient data, not difficult to understand this phenomenon, the first transformed data 558 is data that is later compared to 1040 is very small, so its information is mainly concentrated in the first block of data middle

Note:
the DCT transform stands discrete cosine transform (Discrete Cosine Transform), is mainly used for the pressure data or image reduction, can be converted to a spatial frequency domain signals, a good correlation to performance. DCT is lossless transform itself , but to the next in areas such as image coding quantization, Huffman coding to create a very good condition, at the same time, due to the DCT symmetrical, so that we can take advantage of the anti-DCT after quantization coding converting, at the receiving end to restore the original image information.
DCT transformation has been compressed has a very broad field of use in the current image analysis, our common JPEG still image encoding and MJPEG, MPEG dynamic coding standards are used in the DCT transformation.

In actual image processing, the complexity of the DCT transform is actually relatively high , it is common practice, the image is divided into blocks, and then the image DCT transform and inverse transform in each block, and then merge block, whereby enhance the efficiency of transformation . Specific blocking process, with the sub-blocks becomes large, the algorithm complexity increase rapidly, but using a larger block will significantly reduce the effect of the image block, so there is need to make a compromise, in normal use , mostly using the 8 * 8 block.

Results after 3 DCT processing

Here Insert Picture Description
Conv: mainly to see the second figure, after the major energy can be seen in the picture after conversion concentrated in the upper left corner, it is not hard to understand why coefficient to be arranged in a way Zigzag

Picture data after inverse transform analysis:
Here Insert Picture Description
Here Insert Picture Descriptionsee addition to the data type, the data value is consistent

[Appendix] implementation code

A program

clear all;clc;
addpath('E:\数字图像处理\程序与图像\dipum_toolbox_2.0.2(only run)');%添加相应的.p文件 

f=imread('columbia.tif');
figure(1);
subplot(131);imshow(f);title('原图');  

e=mat2lpc(f);
subplot(132);imshow(mat2gray(e));title('去除空间冗余后图像');
f_ntrop=ntrop(f)%计算原图和编码后的熵
e_ntrop=ntrop(e) 

f_entropy=entropy(f)%计算原图和编码后的熵
e_entropy=entropy(e)  

e_imratio=imratio(f,e)%计算编码后的压缩比 g=lpc2mat(e);%解码 

subplot(133);imshow(g,[]);title('解码后图像');
compare=compare(f,g)%比较两幅图像的误差

Procedure 2

clear all;clc;
addpath('E:\数字图像处理\程序与图像\dipum_toolbox_2.0.2(only run)');%添加相应的.p文件 
f=imread('columbia.tif');
figure(1);
subplot(121);imshow(f,[]);title('原图'); 

c=mat2huff(f);
c_imratio=imratio(f,c) 

g=huff2mat(c);
subplot(122);imshow(g,[]);title('解码后图像'); 

compare=compare(f,g)%比较两幅图像的误差

Procedure 3

clear all;clc;
addpath('E:\数字图像处理\程序与图像\dipum_toolbox_2.0.2(only run)');%添加相应的.p文件 

f=imread('columbia.tif');

e=mat2lpc(f);%求出预测误差图像,这里其实也是获得纵向边沿的一种方式%就是把图像向右移动一个数,然后再减去原图像

figure(1);
subplot(131);imshow(f,[]);title('原图');
subplot(132);imshow(mat2gray(e));title('lpc预测误差图像');
f_entro=entropy(f)
g_entro=entropy(e)
 
%压缩编码
c=mat2huff(e);
cr=imratio(f,c) 

g=lpc2mat(huff2mat(c));
subplot(133);imshow(g,[]);title('压缩并解压后的图像');
compare=compare(f,g) 

[h,x]=hist(e(:)*512,512);
figure(2);
bar(x,h,'k');

Program four

img=imread('Fig0907(c).tif');
i1=blkproc(img,[8 8],@dct_sdu);
%此函数的主要作用是将输入的图像或者说是矩阵分快处理%第一个参数就是输入要进行处理的数据 第二个参数代表着要进行分块处理的矩阵的大小即行列,
%第三个是一个函数句柄,代表着要如何处理这个矩阵fs=fftshift(fft(img));
%这是将图像从空间域转换到频域上,然后再把低频放到图像中间位置
figure,imshow(abs(fs),[]);%显示频域图形
title('图像变换到频域');
%这是为了提取特定个数的系数来制造的mask,分别对应着取1,5,10,30 和64 个系数的时候,
%因为dct的系数是按照Zigzag的形式排列的(横斜竖斜...),我们通过制造mask与对应的系数矩阵相乘,%留下来系数的地方mask中对应的值为1,剩下的为0,即可以通过相乘的方式取得我们想要的系数
mask1=[1 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0];%取1个系数的时候对应的mask
mask2=[1 1 0 0 0 0 0 0
      1 1 0 0 0 0 0 0       
      1 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0       
      0 0 0 0 0 0 0 0];%取5个系数的时候对应的mask 
mask3=[1 1 1 1 0 0 0 0
      1 1 1 0 0 0 0 0
      1 1 0 0 0 0 0 0        
      1 0 0 0 0 0 0 0        
      0 0 0 0 0 0 0 0        
      0 0 0 0 0 0 0 0        
      0 0 0 0 0 0 0 0        
      0 0 0 0 0 0 0 0];%取10个系数的时候对应的mask 
mask4=[1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 0        
      1 1 1 1 1 1 0 0        
      1 1 1 1 0 0 0 0        
      1 1 1 1 0 0 0 0        
      1 0 0 0 0 0 0 0        
      0 0 0 0 0 0 0 0        
      0 0 0 0 0 0 0 0];%取30个系数的时候对应的mask
%将通过dct变换留下来的系数与mask相乘,其中P1代表句柄的输入值mask,x代表分快处理中的每一块

i2=blkproc(i1,[8 8],'P1.*x',mask1);
i3=blkproc(i1,[8 8],'P1.*x',mask2);
i4=blkproc(i1,[8 8],'P1.*x',mask3);
i5=blkproc(i1,[8 8],'P1.*x',mask4);
%再分块进行反变换操作
ii1=blkproc(i1,[8 8],@idct_sdu);
ii2=blkproc(i2,[8 8],@idct_sdu);
ii3=blkproc(i3,[8 8],@idct_sdu);
ii4=blkproc(i4,[8 8],@idct_sdu);
ii5=blkproc(i5,[8 8],@idct_sdu);
figure%显示结果
subplot(2,3,1),imshow(img),title('原图像');
subplot(2,3,2),imshow(ii2,[]),title('取1个dct系数');
subplot(2,3,3),imshow(ii3,[]),title('取5个dct系数');
subplot(2,3,4),imshow(ii4,[]),title('取10个dct系数');
subplot(2,3,5),imshow(ii5,[]),title('取30个dct系数');
subplot(2,3,6),imshow(ii1,[]),title('取64个dct系数'); 

img = imread('cameraman.tif');
imgdct = dct_sdu(img);  %不进行分快处理,直接进行DCT 正向变换
imgidct = idct_sdu(imgdct); %DCT 逆变换
figure (1);
subplot(131);
imshow(img); title('原始图象')
subplot(132);
%imshow(imgdct,[]); 
imshow(uint8(imgdct));%因为imgdct的值范围很大,值特别大的数很少,直接显示视觉上一片黑色
%因此用uint8,把255以上的都用白点,0以下的用黑点,这样能看清楚主要能量集中在哪儿
title('DCT 变换图象(不分块)'); 

subplot(133);
%imshow(imgidct,[]);imshow(uint8(imgidct));%显示,因为是double型的先变成uint8,再显示
title('DCT 逆变换图象'); 

子程序dct_sdu:
function Y=dct_sdu(X)       %子程序定义
[m,n] = size(X);            %得到输入图像的大小m*n
AM = zeros(m,m);            %得到m*m的零矩阵
AN = zeros(n,n);            %得到n*n的零矩阵
for i = 0:m-1               %根据DCT算法得到AM矩阵中每一元素的值
    for j = 0:m-1
        if i == 0            
            AM(i+1,j+1) = sqrt(1/m)*cos(((2*j+1)*i*pi)/(2*m));         
        else AM(i+1,j+1) = sqrt(2/m)*cos(((2*j+1)*i*pi)/(2*m));        
        end    
    end
end
for i = 0:n-1           %根据DCT算法得到AN矩阵中每一元素的值    
    for j = 0:n-1        
        if i == 0             
            AN(i+1,j+1) = sqrt(1/n)*cos(((2*j+1)*i*pi)/(2*n));         
        else AN(i+1,j+1) = sqrt(2/n)*cos(((2*j+1)*i*pi)/(2*n));         
        end
    end
end
X = double(X);       %将X转化为double形式
Y = AM*X*AN';        
%将AN矩阵和输入图像的矩阵和AN的逆矩阵相乘,得到DCT变换后结果
end 

子程序idct_sdu:
function Y=idct_sdu(X)     %同上
[m,n] = size(X);
AM = zeros(m,m);
AN = zeros(n,n);
for i = 0:m-1     
    for j = 0:m-1         
        if i == 0            
            AM(i+1,j+1) = sqrt(1/m)*cos(((2*j+1)*i*pi)/(2*m));         
        else AM(i+1,j+1) = sqrt(2/m)*cos(((2*j+1)*i*pi)/(2*m));        
        end
    end
end
for i = 0:n-1
    for j = 0:n-1
         if i == 0
             AN(i+1,j+1) = sqrt(1/n)*cos(((2*j+1)*i*pi)/(2*n));         
         else AN(i+1,j+1) = sqrt(2/n)*cos(((2*j+1)*i*pi)/(2*n));         
         end
    end
end
X = double(X);
Y = AM'*X*AN;
%将AM矩阵的逆矩阵和输入图像的矩阵和AN矩阵相乘,得到IDCT反变换后结果
end
Published 17 original articles · won praise 12 · views 1659

Guess you like

Origin blog.csdn.net/weixin_42784535/article/details/105124468