声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1860|回复: 2

[混合编程] [转帖]实现基本JPEG的压缩和编码

[复制链接]
发表于 2006-4-18 10:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  1. %%%%%%%%%% 实现基本JPEG的压缩和编码 %%%%%%%%%%%%
  2. % 1. 首先通过DCT变换去除数据冗余;
  3. % 2. 使用量化表对DCT系数进行量化;
  4. % 3. 对量化后的系数进行Huffman编码。
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  6. clear;
  7. clc;
  8. %%%%%%%%%%%%%%%%%%%%%%
  9. %%1.主程序
  10. %%%%%%%%%%%%%%%%%%%%%%
  11. I=imread('lenna.bmp');
  12. yiq=rgb2ntsc(I); % transform RGB to NTSC



  13. my=[16 11 10 16 24 40 51 61;
  14. 12 12 14 19 26 58 60 55;
  15. 14 13 16 24 40 57 69 56;
  16. 14 17 22 29 51 87 80 62;
  17. 18 22 37 56 68 109 103 77;
  18. 24 35 55 64 81 104 113 92;
  19. 49 64 78 87 103 121 120 101;
  20. 72 92 95 98 112 100 103 99];



  21. miq=[17 18 24 47 99 99 99 99;
  22. 18 21 26 66 99 99 99 99;
  23. 24 26 56 99 99 99 99 99;
  24. 47 66 99 99 99 99 99 99;
  25. 99 99 99 99 99 99 99 99;
  26. 99 99 99 99 99 99 99 99;
  27. 99 99 99 99 99 99 99 99;
  28. 99 99 99 99 99 99 99 99];



  29. I1=yiq(:, :, 1);
  30. I2=yiq(:, :, 2);
  31. [m n]=size(I1);



  32. t1=8;
  33. ti1=1;
  34. while (t1<(m+8))
  35. t1=t1+8;
  36. ti1=ti1+1;
  37. end



  38. t2=8;
  39. ti2=1;
  40. while (t2<(n+8))
  41. t2=t2+8;
  42. ti2=ti2+1;
  43. end



  44. times=0;
  45. for k=0:ti1-2
  46. for j=0:ti2-2
  47. dct8x8(I1(k*8+1:k*8+8,j*8+1:j*8+8),my,times*64+1);
  48. dct8x8(I2(k*8+1:k*8+8,j*8+1:j*8+8),miq,times*64+1);
  49. times=times+1;
  50. end
  51. block(I2(k*8+1:k*8+8,j*8+1:t2),[8 8], 'dctmtx(8)');
  52. end



  53. for j=0:ti2-2
  54. dct8x8(I1(k*8+1:t1,j*8+1:j*8+8),times*64+1);
  55. times=times+1;
  56. end



  57. dct8x8(I1(k*8+1:t1,j*8+1:t2),times*64+1);



  58. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  59. %%2.function dct8x8(I,m,s) %定义DCT量化子程序
  60. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. function dct8x8(I,m,s)
  62. T=inline('dctmtx(8)');
  63. y=blkproc(I,[8 8],T);
  64. y=round(y./m);
  65. p=1;te=1;
  66. while (p<=64)
  67. for q=1:te
  68. y1(s+p)=y(te-q+1,q);p=p+1;
  69. end
  70. for q=te:-1:1
  71. y1(s+p)=y(te-q+1,q);p=p+1;
  72. end
  73. end



  74. f=haffman(y1);
  75. c(s:s+64,1)=f(:,1);
  76. c(s:s+64,2)=f(:,2);
  77. c(s:s+64,3)=f(:,3)



  78. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  79. %%3.function c=haffman(I) %定义Huffman编码子程序
  80. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  81. [m,n]=size(I);
  82. p1=1;
  83. s=m*n;
  84. for k=1:m
  85. for h=1:n
  86. f=0;
  87. for b=1:p1-1
  88. if (c(b,1)==I(k,h))
  89. f=1;
  90. break;
  91. end
  92. end
  93. if (f==0)
  94. c(p1,1)=I(k,h);
  95. p1=p1+1;
  96. end
  97. end
  98. end



  99. for g=1:p1-1
  100. p(g)=0;c(g,2)=0;
  101. for k=1:m
  102. for h=1:n
  103. if (c(g,1)==I(k,h))
  104. p(g)=p(g)+1;
  105. end
  106. end
  107. end
  108. p(g)=p(g)/s;
  109. end



  110. pn=0;po=1;
  111. while (1)
  112. if(pn>=1.0)
  113. break;
  114. else
  115. [pm p2]=min(p(1:p1-1));
  116. p(p2)=1.1;
  117. [pm2,p3]=min(p(1:p1-1));
  118. p(p3)=1.1;
  119. pn=pm+pm2;
  120. p(p1)=pn;
  121. tree(po,1)=p2;
  122. tree(po,2)=p3;
  123. po=po+1;
  124. p1=p1+1;
  125. end
  126. end



  127. for k=1:po-1
  128. tt=k;
  129. m1=1;
  130. if (or(tree(k,1)<9,tree(k,2)<9))
  131. if (tree(k,1)<9)
  132. c(tree(k,1),2)=c(tree(k,1),2)+m1;
  133. m2=1;
  134. while(tt)
  135. m1=m1*2;
  136. for h=tt:po-1
  137. if (tree(h,1)==tt+g)
  138. c(tree(k,1),2)=c(tree(k,1),2)+m1;
  139. m2=m2+1;
  140. tt=h;
  141. break;
  142. elseif (tree(h,2)==tt+g)
  143. m2=m2+1;
  144. tt=h;
  145. break;
  146. end
  147. end
  148. end
  149. c(tree(k,1),3)=m2;
  150. end
  151. tt=k;
  152. m1=1;
  153. if (tree(k,2)<9)
  154. m2=1;
  155. while (tt)
  156. m1=m1*2;
  157. for h=tt:po-1
  158. if (tree(h,1)==tt+g)
  159. c(tree(k,2),2)=c(tree(k,2),2)+m1;
  160. m2=m2+1;
  161. tt=h;
  162. break;
  163. elseif (tree(l,2)==tt+g)
  164. m2=m2+1,
  165. tt=h;
  166. break;
  167. end
  168. end
  169. dct8x8(I1(k*8+1:k*8+8,j*8+1:j*8+8),my,times*64+1);
  170. dct8x8(I2(k*8+1:k*8+8,j*8+1:j*8+8),miq,times*64+1);
  171. times=times+1;
  172. end
  173. c(tree(k,2),3)=m2;
  174. end
  175. end
  176. end
复制代码

[ 本帖最后由 suffer 于 2006-10-9 20:50 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-6-15 23:13 | 显示全部楼层

求助

<P>好象有问题啊,不能运行?哪位大大能给修改修改,小弟先在这里谢了</P>
发表于 2006-8-15 08:51 | 显示全部楼层
不能运行的主要问题可能是子程序没有分开,不过我发现部分语句出现问题。比如times,被定义成了函数,又定义成了变量。呵呵。需要再调试。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-20 02:37 , Processed in 0.069891 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表