a)框架:
图一 基于块变换编码的系统框架
本实验在此系统框架的基础上,删除了二进制编解码的过程,仅通过DCT变换和JPEG量化表进行量化,并进行恢复,以对比恢复图和原图之间的质量差距。
b)原理分析
首先将图像分为8X8大小的非重叠块,对每个块进行DCT变换,变换公式为
变换完成之后,对于每个块进行量化,量化表为
量化完成后,正向过程结束。接下来是恢复过程,恢复过程分为了逆量化和逆DCT变换,其中,逆DCT变换的公式为
其中A矩阵与DCT变化中的相同。在完成图像恢复后,我们以恢复图和原始图之间的MSE作为衡量图像恢复质量的指标,其计算公式为
其中为原始图像在(i, j)处的像素值,为恢复图像在(i,j)处的像素值,M和N为图像的长和宽。
c)过程
所采用的数据为512X512的lena图像;
环境为Matlab
测试代码如下
% Author: flztiii
% Date: 2019.3.22
% Info: There is no padding process and make sure the width and height of the input image can be divisible by 8.
clear;
clc;
% Quantification matrix
QUANMATRIX = [16 11 10 16 24 40 51 61 ;
12 12 14 19 26 58 60 55 ;
14 13 16 24 40 57 69 56 ;
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];
% img input
path = './lena.jpg';
raw_img = imread(path);
[width, height] = size(raw_img);
raw_img = double(raw_img);
% DCT
fun_dct = @(block_struct) dct2(block_struct.data);
info = blockproc(raw_img, [8 8], fun_dct);
% quantification
fun_quan = @(block_struct) QuanFun(block_struct.data, QUANMATRIX);
quan_info = blockproc(info, [8 8], fun_quan);
% reverse quantification
fun_re_quan = @(block_struct) ReQuanFun(block_struct.data, QUANMATRIX);
re_quan_info = blockproc(quan_info, [8 8], fun_re_quan);
% IDCT
fun_idct = @(block_struct) idct2(block_struct.data);
output_img = blockproc(re_quan_info, [8 8], fun_idct);
% error calculation
error_matrix = double(output_img) - double(raw_img);
error_matrix = abs(error_matrix);
mse_error = sum(sum(error_matrix.^2))/(width*height);
fprintf('The Calculated MSE is %f\n', mse_error);
% show
figure
subplot(2,3,1);
imshow(uint8(raw_img));
title('原始图');
subplot(2,3,2);
imshow(uint8(info));
title('分块DCT变换图');
subplot(2,3,3);
imshow(uint8(quan_info));
title('量化图');
subplot(2,3,4);
imshow(uint8(re_quan_info));
title('反量化图');
subplot(2,3,5);
imshow(uint8(output_img));
title('DCT反变换恢復图');
subplot(2,3,6);
x = 1: width;
y = 1: height;
[X, Y] = meshgrid(x, y);
mesh(X, Y, error_matrix);
shading interp;
% Quantification function
function[output] = QuanFun(input, QUANMATRIX)
input = double(input);
output = zeros(8, 8);
output = double(output);
for i = 1:8
for j = 1:8
output(i, j) = round(input(i, j)/double(QUANMATRIX(i, j)));
end
end
end
% Reverse Quantification function
function[output] = ReQuanFun(input, QUANMATRIX)
input = double(input);
output = zeros(8, 8);
output = double(output);
for i = 1:8
for j = 1:8
output(i, j) = input(i, j)*double(QUANMATRIX(i, j));
end
end
end
代码和数据放在了https://github.com/flztiii/DCT.git
d)结果
结果如图二所示
图二 结果图
左上为原始图像,中上为DCT变换后的图像,右上为量化之后的结果,左下为逆量化后的图像,中下为恢复后的图像,右下为恢复后的图像与原始图像每一个对应像素点的差的绝对值。最后计算出恢复后的图像与原始图像之间的MSE