24小时热门版块排行榜    

Znn3bq.jpeg
汕头大学海洋科学接受调剂
查看: 1310  |  回复: 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

金虫 (正式写手)


bslt(金币+1,VIP+0):应助有奖,欢迎常来信息科学版! 4-14 09:34
我有一份matlab JPEG的代码,核心部分是由C程序写的,在matlab中调用相关函数就可以了。可以通过matlab读取一个JPEG文件,并提取里面的各项细节,包括各个分量值,量化表等,并且也可以编码。需要的话发邮件到zh1985444@sina.com.cn,注明小木虫
6楼2009-04-13 22:00:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 10 个回答

bslt

金虫 (著名写手)

--<-<-<@

块效应 是不是溢出了
别迷恋哥,哥不只是个传说...
2楼2009-04-09 15:55:05
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

小木头5898

银虫 (小有名气)


2007骑猪逛街(金币+1,VIP+0):谢谢参与交流 4-20 03:31
我觉得是不是量化过程失真太大  还一个就是量化后的编码是不是应该用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的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 271求调剂 +30 2261744733 2026-04-11 30/1500 2026-04-15 06:13 by 逍遥三郎
[考研] 085400电子信息类(川大控制工程)求调剂可跨专业 求老师联系 +6 626776879 2026-04-08 6/300 2026-04-14 20:09 by gwjxiaolang
[考研] 恳请有学校收留 +3 柯淮然 2026-04-12 3/150 2026-04-14 16:25 by 逆水乘风
[考研] 一志愿沪9,326求生物学调剂 +10 刘墨墨 2026-04-13 10/500 2026-04-14 15:16 by zs92450
[教师之家] 转长聘了 +7 简单化xn 2026-04-13 7/350 2026-04-14 14:50 by xindong
[考研] 电气专硕320求调剂 +6 小麻子111 2026-04-10 6/300 2026-04-12 10:54 by lemon6009
[考研] 326求调剂 +6 Shansyn 2026-04-10 6/300 2026-04-12 09:46 by hammer3
[考研] 085410 273求调剂 +10 X1999 2026-04-09 10/500 2026-04-12 09:24 by 逆水乘风
[考研] 求调剂,一志愿材料科学与工程985,365分, +8 材化李可 2026-04-11 10/500 2026-04-12 08:42 by 852137818
[考研] 085404 293求调剂 +9 勇远库爱314 2026-04-08 9/450 2026-04-12 02:24 by 秋豆菜芽
[考研] 299求调剂 +8 ZVVZ13 2026-04-08 8/400 2026-04-12 00:40 by 蓝云思雨
[考研] 085501机械专硕 302分 不挑专业求调剂 +7 汪某. 2026-04-09 7/350 2026-04-11 14:37 by luhong1990
[考研] 282,求调剂 +12 jggshjkkm 2026-04-09 14/700 2026-04-11 09:39 by 猪会飞
[考研] 281求调剂 +11 觉得好的吧 2026-04-10 11/550 2026-04-11 09:35 by 逆水乘风
[考研] 考研调剂 +26 硕星赴 2026-04-09 27/1350 2026-04-10 22:24 by 猪会飞
[考研] 085601初试330分找调剂 +10 流心奶黄包l 2026-04-09 10/500 2026-04-10 08:14 by Sammy2
[考研] 本科211 工科085400 280分求调剂 可跨专业 +3 LZH(等待调剂中 2026-04-09 3/150 2026-04-09 21:29 by wutongshun
[考研] 一志愿中科院105500专业总分315求调剂 +6 lallalh 2026-04-09 7/350 2026-04-09 17:51 by lallalh
[考研] 化学工程与技术专业一志愿哈工程 291分B区 国家级大创负责人 有一作论文 +13 Emmy~ 2026-04-09 13/650 2026-04-09 14:47 by only周
[考研] 283电子信息求调剂 +4 三石WL 2026-04-08 4/200 2026-04-09 10:21 by wp06
信息提示
请填处理意见