|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
代码如下 功能是实现JPEG图像的基本压缩<BR>1.主程序<BR>I=imread('autumn.tif');<BR>yiq=rgb2ntsc(I);<BR>my=[16 11 10 16 24 40 51 61;12 12 14 19 26 58 60 55;<BR>14 13 16 24 40 57 69 56;14 17 22 29 51 87 80 62;<BR>18 22 37 56 68 109 103 77;24 35 55 64 81 104 113 92;<BR>49 64 78 87 103 121 120 101;72 92 95 98 112 100 103 99];<BR>miq=[17 18 24 47 99 99 99 99;18 21 26 66 99 99 99 99;<BR>24 26 56 99 99 99 99 99;47 66 99 99 99 99 99 99;<BR>99 99 99 99 99 99 99 99;99 99 99 99 99 99 99 99;<BR>99 99 99 99 99 99 99 99;99 99 99 99 99 99 99 99];<BR>I1=yiq(:,:,1);I2=(:,:,2);<BR>[m n]=size(I1);<BR>t1=8;ti1=1;<BR>while(t1<BR>t1=t1+8;ti1=ti1+1;<BR>end<BR>t2=8;ti2=1;<BR>while(t2<BR>t2=t2+8;ti2=ti2+1;<BR>end<BR>times=0;<BR>for k=0:ti1-2<BR>for j=0:ti2-2<BR>dct8x8(I1(k*8+1:k*8+8,j*8+1:j*8+8),my,times*64+1);<BR>dct8x8(I2(k*8+1:k*8+8,j*8+1:j*8+8),miq,times*64+1);<BR>times=times+1;<BR>end<BR>block(I2(k*8+1:k*8+8,j*8+1:t2),[8 8], 'dctmtx(8)');<BR>end<BR>for j=0:ti2-2<BR>dct8x8(I1(k*8+1:t1,j*8+1:j*8+8),times*64+1);<BR>times=times+1;<BR>end<BR>dct8x8(I1(k*8+1:t1,j*8+1:t2),times*64+1);<BR>2.function dct8x8(I,m,s) %定义DCT量化子程序<BR>T=inline('dctmtx(8)');<BR>y=blkproc(I,[8 8],T);<BR>y=round(y./m);<BR>p=1;te=1;<BR>while(p<=64)<BR>for q=1:te<BR>y1(s+p)=y(te-q+1,q);p=p+1;<BR>end<BR>for q=te:-1:1<BR>y1(s+p)=y(te-q+1,q);p=p+1;<BR>end<BR>end<BR>f=haffman(y1);<BR>c(s:s+64,1)=f(:,1);c(s:s+64,2)=f(:,2);c(s:s+64,3)=f(:,3)<BR>3.function c=haffman(I) %定义Huffman编码子程序<BR>[m,n]=size(I);<BR>p1=1;s=m*n;<BR>for k=1:m<BR>for h=1:n<BR>f=0;<BR>for b=1:p1-1<BR>if(c(b,1)==I(k,h)) f=1;break;end<BR>end<BR>if(f==0) c(p1,1)=I(k,h);p1=p1+1;end<BR>end<BR>end<BR>for g=1:p1-1<BR>p(g)=0;c(g,2)=0;<BR>for k=1:m<BR>for h=1:n<BR>if(c(g,1)==I(k,h)) p(g)=p(g)+1;end<BR>end<BR>end<BR>p(g)=p(g)/s;<BR>end<BR>pn=0;po=1;<BR>while(1)<BR>if(pn>=1.0) break;<BR>else<BR>[pm p2]=min(p(1:p1-1));p(p2)=1.1;<BR>[pm2,p3]=min(p(1:p1-1));p(p3)=1.1;<BR>pn=pm+pm2;p(p1)=pn;<BR>tree(po,1)=p2;tree(po,2)=p3;<BR>po=po+1;p1=p1+1;<BR>end<BR>end<BR>for k=1:po-1<BR>tt=k;m1=1;<BR>if(or(tree(k,1)<9,tree(k,2)<9))<BR>if(tree(k,1)<9)<BR>c(tree(k,1),2)=c(tree(k,1),2)+m1;<BR>m2=1;<BR>while(tt<BR>m1=m1*2;<BR>for h=tt:po-1<BR>if(tree(h,1)==tt+g)<BR>c(tree(k,1),2)=c(tree(k,1),2)+m1;<BR>m2=m2+1;tt=h;break;<BR>elseif(tree(h,2)==tt+g)<BR>m2=m2+1;tt=h;break;<BR>end<BR>end<BR>end<BR>c(tree(k,1),3)=m2;<BR>end<BR>tt=k;m1=1;<BR>if(tree(k,2)<9)<BR>m2=1;<BR>while(tt<BR>m1=m1*2;<BR>for h=tt:po-1<BR>if(tree(h,1)==tt+g)<BR>c(tree(k,2),2)=c(tree(k,2),2)+m1;<BR>m2=m2+1;tt=h;break;<BR>elseif(tree(l,2)==tt+g)<BR>m2=m2+1,tt=h;break;<BR>end<BR>end<BR>end<BR>c(tree(k,2),3)=m2;<BR>end<BR>end<BR>end<BR>!!!!!!!急用啊!!!!!!! |
|