logo资料库

分形图像压缩和解码程序.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
clear tic %Image1=imread('pic\cameraman.tif'); xianshi; number=input(' input the number:'); Image1=suoxiao('pic\cameraman.tif',number); [imagem imagen]=size(Image1); Sr=4;Sd=8; Rnum=(imagem/Sr)*(imagen/Sr); Dnum=(imagem/Sd)*(imagen/Sd); Image2=zeros(Dnum,Sr,Sr); Image2=blkproc(Image1,[Sd/Sr,Sd/Sr],'mean(mean(x))'); %压缩 image1 为原来 1/2 % there are no eight tranformation for simpleness RBlocks=zeros(Rnum,Sr,Sr); DBlocks=zeros(Dnum,Sd,Sd); DBlocksReduce=zeros(Dnum*8,Sr,Sr); %%取 R 块,K 记标号---------------------------------- for i=1:imagem/Sr for j=1:imagen/Sr k=(i-1)*imagen/Sr+j; RBlocks(k,:,:)=Image1((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr); end end %取 R 块,K 记标号---------------------------------- for i=1:imagem/Sd for j=1:imagen/Sd k=(i-1)*imagen/Sd+j; m=Sr;n=Sr; DBlocksReduce(k,:,:)=Image2((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr); DBlocksReduce(k+Dnum,:,:)=DBlocksReduce(k,m:-1:1,:); 行上下翻转===(x 轴对称) DBlocksReduce(k+2*Dnum,:,:)=DBlocksReduce(k,:,n:-1:1); 列左右翻转 ==== y 轴对称 DBlocksReduce(k+3*Dnum,:,:)=DBlocksReduce(k,m:-1:1,n:-1:1); 先行翻,再列翻 旋转 180 度 % % %
DBlocksReduce(k+4*Dnum,:,:)=reshape(DBlocksReduce(k,:,:),Sr,Sr)'; % 关于 y=-x 对称 A=reshape( DBlocksReduce(k+3*Dnum,:,:),Sr,Sr)'; DBlocksReduce(k+5*Dnum,:,:)=A(:,n:-1:1); % 关于 y=x 对称 DBlocksReduce(k+6*Dnum,:,:)=imrotate(reshape(DBlocksReduce(k,:,:),Sr,Sr), 90); % 逆时针旋转 90 度 DBlocksReduce(k+7*Dnum,:,:)=imrotate(reshape(DBlocksReduce(k,:,:),Sr,Sr), 270); % 逆时针旋转 270 度 DBlocks(k,:,:)=Image1((i-1)*Sd+1:i*Sd,(j-1)*Sd+1:j*Sd); end end RandDbest=zeros(Rnum,1)+256^3; RandDbests=zeros(Rnum,1); RandDbesto=zeros(Rnum,1); RandDbestj=zeros(Rnum,1); for i=1:Rnum x=reshape(RBlocks(i,:,:),Sr*Sr,1); meanx=mean(x); for j=1:Dnum*8 y=reshape(DBlocksReduce(j,:,:),Sr*Sr,1); meany=mean(y); s=(x-meanx)'*(y-meany)/((y-meany)'*(y-meany));%计算 s o=(meanx-s*meany);%计算 o c=(x-s*y-o)'*(x-s*y-o);%距离 if (RandDbest(i)>c)&(abs(s)<1) RandDbest(i)=c; RandDbests(i)=s; RandDbesto(i)=o; RandDbestj(i)=j;%可以找到对应变换和 D 块 end end end %iteration limit toc tic m=8;%解码迭代次数 e=mean(mean(Image1));
Image3=e*ones(imagem,imagen);%解码初始图象 for L=1:m Image4=blkproc(Image3,[Sd/Sr,Sd/Sr],'mean(mean(x))'); for i=1:imagem/Sr for j=1:imagen/Sr m=Sr;n=Sr; k=(i-1)*imagen/Sr+j; l=RandDbestj(k); k1=mod(l-1,Dnum)+1;%第几个 D l1=(l-k1)/Dnum+1;%变换号 %R 对应 D 在 Image4 的起始点 j1=mod(k1-1,imagen/Sd)+1; i1=(k1-j1)/(imagen/Sd)+1; %变换------------------------------------------------------------------------ DBlocksReduce(k1,:,:)=Image4((i1-1)*Sr+1:i1*Sr,(j1-1)*Sr+1:j1*Sr); switch l1-1 case 0 DBlocksReduce(l,:,:)=Image4((i1-1)*Sr+1:i1*Sr,(j1-1)*Sr+1:j1*Sr); case 1 DBlocksReduce(l,:,:)=DBlocksReduce(k1,m:-1:1,:); case 2 DBlocksReduce(l,:,:)=DBlocksReduce(k1,:,n:-1:1); case 3 DBlocksReduce(l,:,:)=DBlocksReduce(k1,m:-1:1,n:-1:1); case 4 DBlocksReduce(l,:,:)=reshape(DBlocksReduce(k1,:,:),Sr,Sr)'; case 5 DBlocksReduce(k1+3*Dnum,:,:)=DBlocksReduce(k1,m:-1:1,n:-1:1); A=reshape( DBlocksReduce(k1+3*Dnum,:,:),Sr,Sr)'; DBlocksReduce(l,:,:)=A(:,n:-1:1); case 6 DBlocksReduce(l,:,:)=imrotate(reshape(DBlocksReduce(k1,:,:),Sr,Sr),90); case 7 DBlocksReduce(l,:,:)=imrotate(reshape(DBlocksReduce(k1,:,:),Sr,Sr),270);
end %变换结束-------------------------------------------------------------------- RBlocks(k,:,:)=RandDbests(k)*DBlocksReduce(l,:,:)+RandDbesto(k); %生成 R--------------------------- Image3((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr)=reshape(RBlocks(k,:,:),Sr,Sr);%更新迭 代图象 end end wucha=double(Image1)-Image3;%误差图 Ps1(L)=20*log10(255/(sqrt(mean(mean(wucha.^2))))) PSNR=psnr(wucha) figure imshow(uint8(Image3)) end toc figure wucha=uint8(wucha); imshow(wucha) figure imshow(uint8(Image1)),title('原图'); save('sa.mat') zhifangtu(wucha);%%%%分形主函数 %%%%%%%%%%%%%%%%%%%%%%%%%%%% 子函数:1: function b=suoxiao(filename,bili) a=imread(filename); a=double(a); [m,n]=size(a); i=1; while i=m/bili j=1; while j=n/bili k=mean(mean(a(bili*(i-1)+1:bili*(i-1)+bili,bili*(j-1)+1:bili*(j-1)+bili))); b(i,j)=k; j=j+1; end i=i+1; end %b=uint8(b);
size(b) %imshow(b) 子函数 2: %clc function zhifangtu(a) J=a; %计算灰度图象的直方图数据,a 为如象数组 L=256; %灰度级 Ps = zeros(L,1); %统计直方图结果数据 nk=zeros(L,1); [row,col]=size(a); n=row*col; %总像素个数 for i = 1:row for j = 1:col num = double(a(i,j))+1; %获取像素点灰度级 nk(num) = nk(num)+1; %统计 nk end end %计算直方图概率估计 for i=1:L Ps(i)=nk(i)/n; end figure; subplot(3,1,1);imshow(J),title('误差图'); subplot(3,1,2),plot(nk),title('直方图(nk)'); subplot(3,1,3),plot(Ps),title('直方图(Ps)'); 子函数 3: function PSNR=psnr(a) [m,n]=size(a); a=uint8(a); a=double(a); imagesize=m*n; MSE=sum(dot(a,a))/ imagesize; PSNR=10*log10(255^2/MSE); %%%%%%%%%%%%%%%%%%%%%% 说明: 1、因为本程序时间长,FX 中先选择图片的大小 2、编码与解码 3、做误差图和只方图 4:画出每次迭代的解码图象
分享到:
收藏