matlab jpeg,【求助】JPEG的Matlab实现

下面是我自己实现的JPEG程序 量化后编码是用的RLC 没有使用Huffman(没发现合适的) 但是过程中JPEG之后的图像有很明显的块效应 重建之后PSNR不理想

而且没有实现码率bpp的可控制

敬请各位虫友指导 讨论

如果有更好的JPEG实现程序 希望能贴出来或发给小弟 yan_li19850430@yahoo.com

sad.gif

sad.gif急盼回复

% My_JPEG main function

function My_JPEG

clear all;

clc

%load image

I_Ori=imread('lena.bmp');

I=double(I_Ori);

figure(1)

imshow(I_Ori)

% DCT_8*8 Transform  and  Quantification

%dct_transformed_image = Classic_DCT( I );

dct_transformed_image = image_8x8_block_flowgraph_forward_dct( I );

figure(2)

II=uint8(dct_transformed_image);

imshow(II)

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

quantization_matrix_128x128 = repmat(Q_8x8,64,64); %for coarse quantization

%quantization_matrix_128x128 = repmat((ceil(double(Q_8x8)./40)),64,64 );   %for fine quantization

quantized_image_128x128 =  round(dct_transformed_image ./quantization_matrix_128x128); %round operation should be done here for lossy quantization

figure(3)

imshow(uint8(quantized_image_128x128))

% Entropy Coding

%This suitable Zigzag order is formed from the  JPEG standard

ZigZag_Order = uint8([

1  9  2  3  10 17 25 18

11 4  5  12 19 26 33 41

34 27 20 13 6  7  14 21

28 35 42 49 57 50 43 36

29 22 15 8  16 23 30 37

44 51 58 59 52 45 38 31

24 32 39 46 53 60 61 54

47 40 48 55 62 63 56 64]);

% Finding the reverse zigzag order (8x8 matrix)

reverse_zigzag_order_8x8 = zeros(8,8);

for k = 1

sad.gifsize(ZigZag_Order,1) *size(ZigZag_Order,2))

reverse_zigzag_order_8x8(k) = find(ZigZag_Order== k);

end;

% Break 8x8 block into columns

Single_column_quantized_image=im2col(quantized_image_128x128, [8 8],'distinct');

%--------------------------- zigzag ----------------------------------

% using the MatLab Matrix indexing power (specially the ':' operator) rather than any function

ZigZaged_Single_Column_Image=Single_column_quantized_image(ZigZag_Order,

smile.gif;   

%---------------------------------------------------------------------

%---------------------- Run Level Coding -----------------------------

% construct Run Level Pair from ZigZaged_Single_Column_Image

run_level_pairs=uint8([]);

for block_index=1:4096    %block by block - total 256 blocks (8x8) in the 128x128 image

single_block_image_vector_64(1:64)=0;

for Temp_Vector_Index=1:64

single_block_image_vector_64(Temp_Vector_Index) = ZigZaged_Single_Column_Image(Temp_Vector_Index, block_index);  %select 1 block sequentially from the ZigZaged_Single_Column_Image

end

non_zero_value_index_array = find(single_block_image_vector_64~=0); % index array of next non-zero entry in a block

number_of_non_zero_entries = length(non_zero_value_index_array);  % # of non-zero entries in a block

% Case 1: if first ac coefficient has no leading zeros then encode first coefficient

if non_zero_value_index_array(1)==1,

run=0;   % no leading zero

run_level_pairs=cat(1,run_level_pairs, run, single_block_image_vector_64(non_zero_value_index_array(1)));

end

% Case 2: loop through each non-zero entry

for n=2:number_of_non_zero_entries,

% check # of leading zeros (run)

run=non_zero_value_index_array(n)-non_zero_value_index_array(n-1)-1;

run_level_pairs=cat(1, run_level_pairs, run, single_block_image_vector_64(non_zero_value_index_array(n)));

end

% Case 3: "End of Block" mark insertion

run_level_pairs=cat(1, run_level_pairs, 255, 255);

end

Compressed_image_size = size(run_level_pairs);        % file size after compression

Compression_Ratio = 262144/Compressed_image_size(1,1);

Rate=8/ Compression_Ratio

% Entropy Decoding

% % %  -------------------------------------------------------------------

% % %  -------------------------------------------------------------------

% % %                DECODING

% % %  -------------------------------------------------------------------

% % %  -------------------------------------------------------------------

%---------------------- Run Level Decoding ---------------------------

% construct  ZigZaged_Single_Column_Image from Run Level Pair

c=[];

for n=1:2:size(run_level_pairs), % loop through run_level_pairs

% Case 1 & Cae 2

% concatenate zeros according to 'run' value

if run_level_pairs(n)<255 % only end of block should have 255 value

zero_count=0;

zero_count=run_level_pairs(n);

for l=1:zero_count    % concatenation of zeros accouring to zero_count

c=cat(1,c,0);   % single zero concatenation

end

c=cat(1,c,run_level_pairs(n+1)); % concatenate single'level' i.e., a non zero value

% Case 3: End of Block decoding

else

number_of_trailing_zeros= 64-mod(size(c),64);

for l= 1:number_of_trailing_zeros    % concatenate as much zeros as needed to fill a block

c=cat(1,c,0);

end

end

end

%---------------------------------------------------------------------

%---------------------------------------------------------------------

%    prepare the ZigZaged_Single_Column_Image vector (each column represents 1 block) from the

%    intermediate concatenated vector "c"

for i=1:4096

for j=1:64

ZigZaged_Single_Column_Image(j,i)=c(64*(i-1)+j);

end

end

%---------------------------------------------------------------------

%--------------------------- reverse zigzag --------------------------

%reverse zigzag procedure using the matrix indexing capability of MatLab (specially the ':' operator)

Single_column_quantized_image = ZigZaged_Single_Column_Image(reverse_zigzag_order_8x8,

smile.gif;

%---------------------------------------------------------------------

%image matrix construction from image column

quantized_image_128x128 = col2im(Single_column_quantized_image,   [8 8],   [512 512],   'distinct');

% Inverse_DCT_8*8 Transform  and  Inverse Quantification

dct_transformed_image_IQ =  quantized_image_128x128 .*quantization_matrix_128x128;

%restored_image = image_8x8_block_inv_dct(dct_transformed_image_IQ );

restored_image = image_8x8_block_flowgraph_inverse_dct( dct_transformed_image_IQ );

III=uint8(restored_image);

figure(4)

imshow(III)

% Reconstruct image

%SNR

PSNR=psnr(restored_image, I);

end

[Last edited by gjliu on 2009-5-11 at 13:29]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值