简体   繁体   中英

JPEG compression in matlab

I have made a JPEG compression using matlab, but i feel confused how to add zig zag scanning, rle, and huffman. I hope you can help me.

This is the code that i have made. Input file is an image (*.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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM