DCT變換和JPEG量化對圖像質量的影響

a)框架:

 圖一 基於塊變換編碼的系統框架

本實驗在此係統框架的基礎上,刪除了二進制編解碼的過程,僅通過DCT變換和JPEG量化表進行量化,並進行恢復,以對比恢復圖和原圖之間的質量差距。

b)原理分析

首先將圖像分爲8X8大小的非重疊塊,對每個塊進行DCT變換,變換公式爲

http://images.cnitblog.com/blog/661248/201408/311244196418932.png

變換完成之後,對於每個塊進行量化,量化表爲

量化完成後,正向過程結束。接下來是恢復過程,恢復過程分爲了逆量化和逆DCT變換,其中,逆DCT變換的公式爲

其中A矩陣與DCT變化中的相同。在完成圖像恢復後,我們以恢復圖和原始圖之間的MSE作爲衡量圖像恢復質量的指標,其計算公式爲

其中f_{i,j}爲原始圖像在(i, j)處的像素值,f^{'}_{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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章