完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
%分形压缩的程序
clear clc tic Image1=imread('lena1_resize_resize.bmp'); Image1=rgb2gray(Image1); [imagem imagen]=size(Image1); Sr=4;Sd=8; Rnum=(imagem/Sr)*(imagen/Sr); Dnum=(imagem/Sd)*(imagen/Sd); Image2=blkproc(Image1,[Sd/Sr,Sd/Sr],'mean(mean(x))'); %这个可以继续进行修改,,, size(Image1) size(Image2) %the pitches of the image. RBlocks=zeros(Rnum,Sr,Sr); DBlocks=zeros(Dnum,Sd,Sd); DBlocksReduce=zeros(Dnum*8,Sr,Sr); 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 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,:); DBlocksReduce(k+2*Dnum,:,:)=DBlocksReduce(k,:,n:-1:1); DBlocksReduce(k+3*Dnum,:,:)=DBlocksReduce(k,m:-1:1,n:-1:1); DBlocksReduce(k+4*Dnum,:,:)=reshape(DBlocksReduce(k,:,:),Sr,Sr)'; A=reshape( DBlocksReduce(k+3*Dnum,:,:),Sr,Sr)'; DBlocksReduce(k+5*Dnum,:,:)=A(:,n:-1:1); DBlocksReduce(k+6*Dnum,:,:)=imrotate(reshape(DBlocksReduce(k,:,:),Sr,Sr),90); DBlocksReduce(k+7*Dnum,:,:)=imrotate(reshape(DBlocksReduce(k,:,:),Sr,Sr),270); DBlocks(k,:,:)=Image1((i-1)*Sd+1:i*Sd,(j-1)*Sd+1:j*Sd); end end %存储最佳匹配的参数 RandDbest=zeros(Rnum,1)+256^3; RandDbesta=zeros(Rnum,1); RandDbestb=zeros(Rnum,1); RandDbestj=zeros(Rnum,1); %下面用遗传算法来进行求解 %自适应函数:fitness %种群个体数:NP %最大进化代数:NG %杂交概率:Pc %变异概率:Pm L=log2(Dnum*8); %遗传算法编码的长度 NG=50; NP=20; Pc=0.9; Pm=0.1; for ii=1:Rnum x=zeros(NP,L); x=round(rand(NP,L)); %产生初始种群; for i=1:NP [myfitness,a,b]=fitness(DBlocksReduce,RBlocks,ii,Dec(x(i,:),L),Sr); fx(i)=myfitness; end for k=1:NG sumfx=sum(fx); Px=fx/sumfx; %自适应遗传算法.... maxfitness = Px(1); maxindex = 1; maxnumber = 0; for k = 2:NP if Px(k)>maxfitness maxfitness = Px(k); maxindex = k; end end for k = 1:NP if Dec(x(k,:),L)==Dec(x(k,:),L) maxnumber = maxnumber +1; end end if maxnumber/NP>0.3 %最大适应度的浓度 Pm = 0.2; %增大变异概率 else Pm = 0.1; end PPx=0; PPx(1)=Px(1); for i=2:NP PPx(i)=PPx(i-1)+Px(i); end for i=1:NP sita=rand(); for n=1:NP if sita<= PPx(n) selfather=n; break; end end selmother=floor(rand()*(NP-1))+1; poscut=floor(rand()*(L-2))+1; %随机确定交叉点 r1=rand(); if r1 nx(i,(poscut+1):L)=x(selmother,(poscut+1):L); r2=rand(); if r2<=Pm posmut=round(rand()*(L-1)+1); nx(i,posmut)=~nx(i,posmut); end else nx(i,:)=x(selfather,:); end end x=nx; for i=1:NP [myfitness,a,b]=fitness(DBlocksReduce,RBlocks,ii,Dec(x(i,:),L),Sr); fx(i)=myfitness; end end fv=-inf; for i=1:NP [fitx,a,b]=fitness(DBlocksReduce,RBlocks,ii,Dec(x(i,:),L),Sr); if fitx>fv fv=fitx; xv=Dec(x(i,:),L); besta=a; bestb=b; end end RandDbest(ii)=fv; RandDbesta(ii)=besta; RandDbestb(ii)=bestb; RandDbestj(ii)=xv; end % begin to decode,,,, 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; l1=(l-k1)/Dnum+1; 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,:,:)=RandDbesta(k)*DBlocksReduce(l,:,:)+RandDbestb(k); Image3((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr)=reshape(RBlocks(k,:,:),Sr,Sr); end end error=double(Image1)-Image3; Ps1(L)=20*log10(255/(sqrt(mean(mean(error.^2))))); end imshow(uint8(Image3)) toc save('sa.mat') 报错 ??? Undefined command/function 'Dec'. Error in ==> fenxingyasuoga at 67 [myfitness,a,b]=fitness(DBlocksReduce,RBlocks,ii,Dec(x(i,:),L),Sr); 怎么修改 |
|
相关推荐 |
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1622 浏览 1 评论
200圆!求助大佬给一份VSG并网和离网模式的simulink仿真
1900 浏览 0 评论
MATLAB(3)--矩阵的引用(sub2ind、ind2sub、reshape函数使用)
2720 浏览 0 评论
3055 浏览 0 评论
4095 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 05:08 , Processed in 0.602287 second(s), Total 74, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号