2010-05-15 35 views

回答

1

在MATLAB中有一個用於DCT的內置函數。

您需要信號處理工具箱。在MATLAB命令中輸入'ver'(不帶引號)來查看是否有。

代碼:

image = image; % define your image 

[m,n] = size(image); % get size of your image 

imvector = reshape(image, m*n, 1); % reshape your image to a vector to compute DCT 

imdct = dct(imvector); % compute DCT 

imagedct = reshape(imdct,m,n); \ reshape result back to original form of your image 
1

有在幫助文件中的一個例子,以及這是非常好的:

I = imread('cameraman.tif'); 
I = im2double(I); 
T = dctmtx(8); 
dct = @(block_struct) T * block_struct.data * T'; 
B = blockproc(I,[8 8],dct); 
mask = [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]; 
B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data); 
invdct = @(block_struct) T' * block_struct.data * T; 
I2 = blockproc(B2,[8 8],invdct); 
imshow(I), figure, imshow(I2) 
0

量化DCT係數,您只需通過一個量化項將每個係數並整數到整數。量化項對於每個係數通常是唯一的,並且存儲在量化矩陣中。

Wikipedia has a nice example.以下是如何在Matlab中實現該示例。

coef = [ 
-415 -33 -58 35 58 -51 -15 -12; 
    5 -34 49 18 27 1 -5 3; 
    -46 14 80 -35 -50 19 7 -18; 
    -53 21 34 -20 2 34 36 12; 
    9 -2 9 -5 -32 -15 45 37; 
    -8 15 -16 7 -8 11 4 7; 
    19 -28 -2 -26 -2 7 -44 -21; 
    18 25 -12 -44 35 48 -37 -3 
    ]; 

quant = [ 
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 
    ]; 

quantCoef = round(coef ./ quant) 

quantCoef = 
    -26 -3 -6  2  2 -1  0  0 
    0 -3  4  1  1  0  0  0 
    -3  1  5 -1 -1  0  0  0 
    -4  1  2 -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 
    0  0  0  0  0  0  0  0