您好,欢迎访问三七文档
1.对自己的头像进行操作,分别比较保留10,20,40个DCT变换系数重构的图像与原始图像进行操作。clearall;I=rgb2gray(imread('2013312109.jpg'));J=im2double(I);T=dctmtx(8);K=blkproc(J,[88],'P1*x*P2',T,T');mask1=[1111000011100000110000001000000000000000000000000000000000000000];K1=blkproc(K,[88],'P1.*x',mask1);L1=blkproc(K1,[88],'P1*x*P2',T',T);figure;subplot(221);imshow(J);subplot(222);imshow(L1);mask2=[1111110011111000111100001110000011000000000000000000000000000000];K2=blkproc(K,[88],'P1.*x',mask2);L2=blkproc(K2,[88],'P1*x*P2',T',T);subplot(223);imshow(L2);mask2=[1111111111111111111111101111110011111000111000001100000010000000];K3=blkproc(K,[88],'P1.*x',mask2);L3=blkproc(K3,[88],'P1*x*P2',T',T);subplot(224);imshow(L3);2.1I=rgb2gray(imread('2013312109.jpg'));I=im2double(I)*255;[height,width]=size(I);%求图像的大小HWmatrix=zeros(height,width);Mat=zeros(height,width);%建立大小与原图像大小相同的矩阵HWmatrix和Mat,矩阵元素为0。HWmatrix(1,1)=I(1,1);%图像第一个像素值I(1,1)传给HWmatrix(1,1)fori=2:height%以下将图像像素值传递给矩阵MatMat(i,1)=I(i-1,1);endforj=2:widthMat(1,j)=I(1,j-1);endfori=2:heightforj=2:widthMat(i,j)=I(i,j-1)/2+I(i-1,j)/2;endendMat=floor(Mat);HWmatrix=I-Mat;SymPro=zeros(2,1);SymNum=1;SymPro(1,1)=HWmatrix(1,1);SymExist=0;fori=1:heightforj=1:widthSymExist=0;fork=1:SymNumifSymPro(1,k)==HWmatrix(i,j)SymPro(2,k)=SymPro(2,k)+1;SymExist=1;break;endendifSymExist==0SymNum=SymNum+1;SymPro(1,SymNum)=HWmatrix(i,j);SymPro(2,SymNum)=1;endendendfori=1:SymNumSymPro(3,i)=SymPro(2,i)/(height*width);endsymbols=SymPro(1,:);p=SymPro(3,:);[dict,avglen]=huffmandict(symbols,p);%产生霍夫曼编码词典,返回编码词典dict和平均码长avglenactualsig=reshape(HWmatrix',1,[]);compress=huffmanenco(actualsig,dict);%利用dict对actuals来编码,其结果存放在compress中UnitNum=ceil(size(compress,2)/8);Compressed=zeros(1,UnitNum,'uint8');fori=1:UnitNumforj=1:8if((i-1)*8+j)=size(compress,2)Compressed(i)=bitset(Compressed(i),j,compress((i-1)*8+j));endendendNewHeight=ceil(UnitNum/512);Compressed(width*NewHeight)=0;ReshapeCompressed=reshape(Compressed,NewHeight,width);imwrite(ReshapeCompressed,'CompressedImage.bmp','bmp');Restore=zeros(1,size(compress,2));fori=1:UnitNumforj=1:8if((i-1)*8+j)=size(compress,2)Restore((i-1)*8+j)=bitget(Compressed(i),j);endendenddecompress=huffmandeco(Restore,dict);RestoredImage=reshape(decompress,512,512);RestoredImageGrayScale=uint8(RestoredImage'+Mat);imwrite(RestoredImageGrayScale,'RestoredImage.bmp','bmp');figure;subplot(1,3,1);imshow(I,[0,255]);%显示原图subplot(1,3,2);imshow(ReshapeCompressed);%显示压缩后的图像subplot(1,3,3);imshow('RestoredImage.bmp');%解压后的图像2.2J=rgb2gray(imread('2013312109.jpg'));X=im2double(J);Y=LPCencode(X);XX=LPCdecode(Y);figure(1),subplot(121);imshow(J);subplot(122),imshow(mat2gray(255-Y));%为方便显示,对预测误差图取反后再作显示e=double(X)-double(XX);[m,n]=size(e);erm=sqrt(sum(e(:).^2)/(m*n));figure(2);[h,x]=hist(X(:));%得到原图直方图subplot(121);bar(x,h,'k');%显示原图像的直方图[h,x]=hist(Y(:));%得到预测误差的直方图subplot(122);bar(x,h,'k');%显示预测误差的直方图%LPCdecode是解码程序,与编码程序用的是同一个预测器functionx=LPCdecode(y,f)error(nargchk(1,2,nargin));ifnargin2f=1;endf=f(end:-1:1);[m,n]=size(y);order=length(f);f=repmat(f,m,1);x=zeros(m,n+order);forj=1:njj=j+order;x(:,jj)=y(:,j)+round(sum(f(:,order:-1:1).*x(:,(jj-1):-1:(jj-order)),2));endx=x(:,order+1:end);%LPCencode函数用一维预测编码压缩图像x,f为预测系数,如果f去默认值,则默认f=1,就是前值预测functiony=LPCencode(x,f)error(nargchk(1,2,nargin));ifnargin2f=1;endx=double(x);[m,n]=size(x);p=zeros(m,n);%存放预测值xs=x;zc=zeros(m,1);forj=1:length(f)xs=[zcxs(:,1:end-1)];p=p+f(j)*xs;endy=x-round(p);
本文标题:数字图像处理1
链接地址:https://www.777doc.com/doc-2387921 .html