Image compression using DCT

1, experimental purposes: familiar image compression method of transform coding

Second, the Experiment: In a given image as an example, using dct image compression encoding, by changing the number of non-zero elements in the template matrix, to obtain different compression encoded image , according to the formula

 

, And the original image is calculated programming dct between the converted images obtained mean square error. Used matlab function im2double, dctmtx, blkproc .

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The first experiment to use pictures

The following program template matrices are 8 * 8

. 1 the I = imread ( ' C: /woman_blonde.tif ' );
 2 the I = im2double (the I);
 . 3 T = dctmtx ( 8 ); * 8% to obtain a discrete cosine matrix 8
 . 4 B = blkproc (the I, [ 8  8 ], ' Pl * X * P2 ' , T, T ' ); X% is divided for each 8 * 8 block size, P1 * x * P2 corresponding to pixel blocks handler, p1 = T p2 = T ', i.e. P1 * = X * Fun P2 ' = T * T * X ' function is a discrete cosine transform 
. 5 m = [ . 1  . 1  . 1  0  0  0  0  0 
. 6     . 1  . 1  0  0  0  0  0  0
 7    1 0 0 0 0 0 0 0
 8    0 0 0 0 0 0 0 0
 9    0 0 0 0 0 0 0 0
10    0 0 0 0 0 0 0 0
11    0 0 0 0 0 0 0 0
12    0 0 0 0 0 0 0 0 ];
13 B2=blkproc(B,[8 8], ' Pl * X. ' , M);% discarded frequency coefficients in each block, the compressed image object reaches
 14 I2 blkproc = (B2, [ . 8  . 8 ], ' Pl * X * P2 ' , T ' , T); image after inverse cosine transform, compression% 
15 Cha = ABS (the I- I2);
 16 Junfang = MSE (Cha);
 . 17 Figure, imshow (the I), title ( ' original image ' , ' fontSize ' , 18 is );
 18 is Figure, imshow (I2), title ( ' compression (decompressed) image ' , ' fontSize ' , 18 is ),
 . 19the xlabel ({ ' mean square error: ' ; Junfang}, ' fontSize ' , 18 is );

 The following program template matrix is ​​16 * 16

 1 I3=imread('C:/woman_blonde.tif');
 2 I3=im2double(I3);
 3 T2=dctmtx(16);
 4 B3=blkproc(I3,[16 16],'P1*x*P2',T2,T2');
 5 m2=[1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
 6    1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
 7    1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 8    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 9    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
13    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
14    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
17    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
19    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
20    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
21 is B4 = blkproc (B3, [ 16  16 ], ' . Pl * X ' , M2);
 22 is I4 = blkproc (B4, [ 16  16 ], ' Pl * X * P2 ' , T2 ' , T2); 
23 is CHA1 ABS = (I3- I4);
 24 junfang2 = MSE (CHA1);
 25 Figure, imshow (I3), title ( ' original image ' , ' fontSize ' , 20 is );
 26 is Figure, imshow (I4), title ( ' compression (decompressed) image ' , ' fontSize ' ,20 is ),
 27 the xlabel ({ ' mean square error: ' ; junfang2}, ' fontSize ' , 20 is );

 Compare the results:

8*8

16*16

Comparison is easy to find, the results of the second code image loss is relatively large extent, because the proportion of the number 0 Procedure 2 more templates, so that more high-frequency pixels are compressed, and the compression is irreversible, so a greater degree of loss Figure II.

The following is a description of several functions in the program

dctmtx

MATLAB Image Processing Toolbox Discrete Cosine Transform in two ways: dct2 and dctmtx ( . 1 ) using the function dct2, with the function of an FFT-based algorithm to improve the calculation speed is larger when the input square.
( 2 ) using a DCT transform matrix dctmtx returned by the function, this method is more suitable for smaller input matrix (e.g. × 8 × 8 or 16 16 ).

① function: dct2 
Realization of two-dimensional discrete cosine transform image. Call format is:
B = dct2(A) 
B = dct2(A,[M N]) 
B = dct2(A,M,N)
Wherein A represents an image, M and N to be transformed is an optional parameter indicating the size of the image matrix after filling, B represents a transformed image matrix obtained.

② function: dctmtx 
D = dctmtx(N) 
式中D是返回N×N的DCT变换矩阵,如果矩阵A是N×N方阵,则A的DCT变换可用D×A×D’来计算。这在有时比dct2计算快,特别是对于A很大的情况。 

blkproc

功能:对图像进行分块处理
函数调用形式:B = blkproc(A,[m n],fun, parameter1, parameter2, ...)
B = blkproc(A,[m n],[mborder nborder],fun,...)
B = blkproc(A,'indexed',...)
参数解释:[m n] :图像以m*n为分块单位,对图像进行处理(如8像素*8像素)
          Fun:   应用此函数对分别对每个m*n分块的像素进行处理
          parameter1, parameter2: 要传给fun函数的参数
          mborder nborder:对每个m*n块上下进行mborder个单位的扩充,左右进行nborder个单位的扩充,扩充的像素值为0,fun函数对整个扩充后的分块进行处理。

mse

等价于sum(a.^2)/lenght(a);

MSE和RMSE都是网络的性能函数。MSE是(神经)网络的均方误差,叫"Mean Square Error"。比如有n对输入输出数据,每对为[Pi,Ti],i=1,2,...,n.网络通过训练后有网络输出,记为Yi。那MSE=(求和(Ti-Yi)^2(i=1,2,..n))/n,即每一组数的误差平方和再除以数据的对数。RMSE叫“Root Mean Square Error”,即在MSE基础上要开根号,中文译为“均方根误差”,MSE=MSE开根号。亦即RMSE是MSE的平方根。

 

Guess you like

Origin www.cnblogs.com/henuliulei/p/10957001.html