²é¿´: 1260  |  »Ø¸´: 9
µ±Ç°Ö÷ÌâÒѾ­´æµµ¡£
µ±Ç°Ö»ÏÔʾÂú×ãÖ¸¶¨Ìõ¼þµÄ»ØÌû£¬µã»÷ÕâÀï²é¿´±¾»°ÌâµÄËùÓлØÌû

Сľͷ5898

Òø³æ (СÓÐÃûÆø)

[½»Á÷] ¡¾ÇóÖú¡¿JPEGµÄMatlabʵÏÖ

ÏÂÃæÊÇÎÒ×Ô¼ºÊµÏÖµÄJPEG³ÌÐò¡¡Á¿»¯ºó±àÂëÊÇÓõÄRLC¡¡Ã»ÓÐʹÓÃHuffman£¨Ã»·¢ÏÖºÏÊʵģ©¡¡µ«Êǹý³ÌÖÐJPEGÖ®ºóµÄͼÏñÓкÜÃ÷ÏԵĿéЧӦ¡¡Öؽ¨Ö®ºóPSNR²»ÀíÏë
¶øÇÒûÓÐʵÏÖÂëÂÊbppµÄ¿É¿ØÖÆ¡¡
¾´Çë¸÷λ³æÓÑÖ¸µ¼¡¡ÌÖÂÛ
Èç¹ûÓиüºÃµÄJPEGʵÏÖ³ÌÐò¡¡Ï£ÍûÄÜÌù³öÀ´»ò·¢¸øÐ¡µÜ¡¡yan_li19850430@yahoo.com   ¼±Åλظ´


////////////////////////////////////////////////////////////
% 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 = 1size(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,;   
    %---------------------------------------------------------------------


    %---------------------- 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,;
    %---------------------------------------------------------------------
   

   %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 ]
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

Ifyoufailtoplan,youplantofail!
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

zh1985444

½ð³æ (ÕýʽдÊÖ)

ÓʼþÒѾ­·¢Ë͵½ÄãÓÊÏ䣬´úÂë½ö¹©Ñо¿Ñ§Ï°Ê¹Ó㬰æÈ¨¹éÔ­×÷ÕßËùÓС£ÇëÎðÓÃÓÚÉÌÒµÓÃ;
7Â¥2009-04-16 09:05:29
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
²é¿´È«²¿ 10 ¸ö»Ø´ð

bslt

½ð³æ (ÖøÃûдÊÖ)

--<-<-<@

¿éЧӦ ÊDz»ÊÇÒç³öÁË
±ðÃÔÁµ¸ç£¬¸ç²»Ö»ÊǸö´«Ëµ...
2Â¥2009-04-09 15:55:05
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

Сľͷ5898

Òø³æ (СÓÐÃûÆø)

¡ï
2007ÆïÖí¹ä½Ö(½ð±Ò+1,VIP+0):лл²ÎÓë½»Á÷ 4-20 03:31
ÎÒ¾õµÃÊDz»ÊÇÁ¿»¯¹ý³ÌÊ§ÕæÌ«´ó  »¹Ò»¸ö¾ÍÊÇÁ¿»¯ºóµÄ±àÂëÊDz»ÊÇÓ¦¸ÃÓÃHuffman¸üºÃµã
Ifyoufailtoplan,youplantofail!
3Â¥2009-04-09 16:58:17
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

bslt

½ð³æ (ÖøÃûдÊÖ)

--<-<-<@

¡ï
2007ÆïÖí¹ä½Ö(½ð±Ò+1,VIP+0):лл²ÎÓë½»Á÷ 4-20 03:31
ÊÔÊÔ »ô·òÂü°É
matlab´úÂëgoogleËѲ»µ½ºÃµÄô£¿È¥CSDN ×ÊÔ´ ÀïËÑËÑ¿´¿´
ÎÒ¸ÕÉÏÁ˼¸ÌÃͼÏñ´¦ÀíµÄ¿Î ²»¸ÒÔì´Î °ïÂ¥Ö÷¶¥¹ý¡£¡£¡£
±ðÃÔÁµ¸ç£¬¸ç²»Ö»ÊǸö´«Ëµ...
4Â¥2009-04-09 17:23:44
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] 323Çóµ÷¼Á +6 ÍÝСͰ 2026-03-18 6/300 2026-03-23 00:29 by king123£¡
[¿¼ÑÐ] 0854µç×ÓÐÅÏ¢Çóµ÷¼Á +3 ¦Á____ 2026-03-22 3/150 2026-03-22 21:28 by zhq0425
[¿¼ÑÐ] 315·Ö£¬³ÏÇóµ÷¼Á£¬²ÄÁÏÓ뻯¹¤085600 +3 13756423260 2026-03-22 3/150 2026-03-22 20:11 by edmund7
[¿¼ÑÐ] Ò»Ö¾Ô¸ÖÐÄÏ»¯Ñ§£¨0703£©×Ü·Ö337Çóµ÷¼Á +9 niko- 2026-03-19 10/500 2026-03-22 16:08 by ColorlessPI
[¿¼ÑÐ] Ò»Ö¾Ô¸ÖÐÄÏ´óѧ»¯Ñ§Ñ§Ë¶0703×Ü·Ö337Çóµ÷¼Á +3 niko- 2026-03-22 3/150 2026-03-22 15:15 by ÑîÑîÑî×Ï
[¿¼ÑÐ] 291 Çóµ÷¼Á +3 »¯¹¤2026½ì±ÏÒµÉ 2026-03-21 3/150 2026-03-22 14:26 by ColorlessPI
[¿¼ÑÐ] 280Çóµ÷¼Á +11 ¹¾ààÏþÏþ 2026-03-18 12/600 2026-03-21 22:40 by ACS Nano¡ª¡ª
[¿¼ÑÐ] ³õÊÔ 317 +7 °ëÀ­Ô±û 2026-03-20 7/350 2026-03-21 22:26 by peike
[¿¼ÑÐ] ¹¤¿Æ0856Çóµ÷¼Á +3 ãåÎö͡͡ 2026-03-21 3/150 2026-03-21 18:30 by ѧԱ8dgXkO
[¿¼ÑÐ] ²ÄÁÏѧ˶333Çóµ÷¼Á +3 ±±µÀÏï 2026-03-18 3/150 2026-03-21 18:17 by ѧԱ8dgXkO
[¿¼ÑÐ] 302Çóµ÷¼Á +12 ºôºôºô¡£¡£¡£¡£ 2026-03-17 12/600 2026-03-21 17:29 by ColorlessPI
[¿¼ÑÐ] 265Çóµ÷¼Á +9 ÁºÁºÐ£Ð£ 2026-03-17 9/450 2026-03-21 02:17 by JourneyLucky
[¿¼ÑÐ] Ò»Ö¾Ô¸ Î÷±±´óѧ £¬070300»¯Ñ§Ñ§Ë¶£¬×Ü·Ö287£¬Ë«·ÇÒ»±¾£¬Çóµ÷¼Á¡£ +3 ³¿»èÏßÓëÐǺ£ 2026-03-18 3/150 2026-03-21 00:46 by JourneyLucky
[¿¼ÑÐ] 317Çóµ÷¼Á +5 Éê×ÓÉêÉê 2026-03-19 9/450 2026-03-20 22:26 by JourneyLucky
[¿¼ÑÐ] 298-Ò»Ö¾Ô¸Öйúũҵ´óѧ-Çóµ÷¼Á +9 ÊÖ»úÓû§ 2026-03-17 9/450 2026-03-20 14:24 by ÎÞи¿É»÷111
[¿¼ÑÐ] ÕÐÊÕµ÷¼Á˶ʿ +4 lidianxing 2026-03-19 12/600 2026-03-20 12:25 by lidianxing
[¿¼ÑÐ] ²ÄÁÏÓ뻯¹¤Çóµ÷¼Á +7 Ϊѧ666 2026-03-16 7/350 2026-03-19 14:48 by ¾¡Ë´Ò¢1
[¿¼ÑÐ] Ò»Ö¾Ô¸¸£´ó288Óлú»¯Ñ§£¬Çóµ÷¼Á +3 Сľ³æ200408204 2026-03-18 3/150 2026-03-19 13:31 by houyaoxu
[¿¼ÑÐ] ²ÄÁϹ¤³Ìר˶µ÷¼Á +5 204818@lcx 2026-03-17 6/300 2026-03-18 22:55 by 204818@lcx
[¿¼ÑÐ] 344Çóµ÷¼Á +6 knight344 2026-03-16 7/350 2026-03-18 20:13 by walc
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û