matlab中的JPEG压缩

时间:2014-07-17 01:59:17

标签: matlab

我使用matlab进行了JPEG压缩,但我对如何添加之字形扫描,rle和huffman感到困惑。我希望你能帮助我。

这是我所做的代码。输入文件是图像(* .bmp)。

% dctmtx generates an 8 * 8
dct_matrix = dctmtx(8);
% define function handlers for DCT
dct = @(block_struct) dct_matrix * block_struct.data * dct_matrix';
% define function handlers for inverse DCT
idct = @(block_struct) dct_matrix' * block_struct.data * dct_matrix;
% quantization tables
q_max = 255;
% the quality factor is hardwired here
qf = get(handles.popupmenu1,'Value');   
% quantization table for luminance
q_y = ...
[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].*qf;
% quantization table for chrominance
q_c = ...
[17 18 24 47 99 99 99 99;
 18 21 26 66 99 99 99 99;
 24 26 56 99 99 99 99 99;
 47 66 99 99 99 99 99 99;
 99 99 99 99 99 99 99 99;
 99 99 99 99 99 99 99 99;
 99 99 99 99 99 99 99 99;
 99 99 99 99 99 99 99 99].*qf;
% RGB to YCbCr        
orig = handles.citra;
ycc = rgb2ycbcr(im2double(orig));   
%display the ycbcr 
y = ycc(:, :, 1);
cb = ycc(:, :, 2);
cr = ycc(:, :, 3);

% Discrete cosine transform, with scaling before quantization
% q_max is the quantization

% set up the blockprocess functions for the DCT
y = blockproc(y, [8 8], dct) .*q_max;
cb = blockproc(cb, [8 8], dct) .*q_max;
cr = blockproc(cr, [8 8], dct) .*q_max;
% implement the quantization
y = blockproc(y, [8 8], @(block_struct) round(round(block_struct.data) ./ q_y));
cb = blockproc(cb, [8 8], @(block_struct) round(round(block_struct.data) ./ q_c));
cr = blockproc(cr, [8 8], @(block_struct) round(round(block_struct.data) ./ q_c));
% Dequantize DCT coefficients
y = blockproc (y, [ 8 8 ], @(block_struct) block_struct.data .*q_y ) ;
cb = blockproc (cb, [ 8 8 ], @(block_struct) block_struct.data .*q_c ) ;
cr = blockproc (cr, [ 8 8 ], @(block_struct) block_struct.data .*q_c ) ;
% Inverse DCT
y = blockproc (y ./ q_max , [ 8 8 ] , idct);
cb = blockproc (cb ./ q_max , [ 8 8 ] , idct);
cr = blockproc (cr ./ q_max , [ 8 8 ] , idct);
% Concatenate the channels
ycc1 = ycbcr2rgb(cat(3, y, cb ,cr));
axes(handles.axes_hasil); 
imshow(ycc1);

1 个答案:

答案 0 :(得分:0)

Please check "Digital image processing Using MATLAB" book, there is a detailed chapter of JPEG Compression it has some helpful code for JPEG compression for the rle, this code might of help

function out=rle(image)

L=prod(size(image));
im=reshape(image,1,L);
x=1;
out=[];
while L~=0;
temp=min(find(im==x));  
if isempty(temp)
    out=[out L];
    break;
end;
out=[out temp-1];
x=1-x;
im=im(temp:L);
L=L-temp+1;
end;

end