您好,欢迎访问三七文档
functionMovingTargetDetectionByMMI()%MovingTargetDetection%BasedonMaximunMutualInformation%%EDITBYPSL@CSU%QQ:547423688%Email:anyuezhiji@qq.com%2012-06-01%读文件Im1=imread('001.jpg');Im2=imread('002.jpg');Im3=imread('003.jpg');Im1=rgb2gray(Im1);Im2=rgb2gray(Im2);Im3=rgb2gray(Im3);tic;d12=GetDifferenceImg(Im2,Im1);d23=GetDifferenceImg(Im2,Im3);d=d12.*d23;se=;fori=1:4d=imfilter(d,se);endfori=1:2d=medfilt2(d,);end%%d=abs((d12-d23).^0.7);d=uint8(d/max(max(d))*255);level=graythresh(d);BW=im2bw(d,level);s=regionprops(BW,'BoundingBox');figure(1)subplot(2,2,1);imshow(uint8(d12/max(max(d12))*255));title('参考帧与前一帧的差值')subplot(2,2,2);imshow(uint8(d23/max(max(d23))*255));title('参考帧与后一帧的差值')subplot(2,2,3);imshow(BW);title('由前后帧得出的差值')subplot(2,2,4);imshow(Im2);%imshow(d);rectangle('Position',s(1).BoundingBox,'Curvature',,'LineWidth',2,'LineStyle','--','EdgeColor','r')title('参考帧与检测结果')%求相邻两帧重合部分差值主函数functionoutImg=GetDifferenceImg(R,F)=dwt2(R,'db1');=dwt2(F,'db1');CA1=uint8(CA1);CA2=uint8(CA2);fprintf('\n------PSOstart\n');=PSO(CA1,CA2);whilemi1.2=PSO(CA1,CA2);endfprintf('tx:%fty:%fang:%fmi:%f\n',pa(1),pa(2),pa(3),mi);fprintf('------PSOend\n\n');%pa=;fprintf('------Powellstart\n');mi_old=0;whileabs(mi-mi_old)0.01mi_old=mi;=powell(R,F,pa);endfprintf('------Powellend\n\n');time=toc;fprintf('tx:%.4fty:%.4fang:%.2fmi:%f\n',pa(1),pa(2),pa(3),mi);fprintf('time:%f\n',time);outImg=GetDifference(pa(1),pa(2),pa(3),R,F);%figure(6);imshow(outImg);%求相邻两帧重合部分差值functionoutImg=GetDifference(tx,ty,ang,R,F)=size(R);%R=im2double(R);F=im2double(F);theta=ang*pi/180;%旋转角度转弧度cx=floor(n/2);%旋转的中心点cy=floor(m/2);outImg=zeros(m,n);forj=1:mfori=1:n%参考图像在浮动图像平移后的对应点%x=i-tx;%列%y=j-ty;%x=(i-cx)*cos(theta)-(j-cy)*sin(theta)+cx-tx;y=(i-cx)*sin(theta)+(j-cy)*cos(theta)+cy-ty;x1=floor(x);y1=floor(y);rval=R(j,i);%图像重合部分求差if(x1=1&&x1n&&y1style=margin:0px;word-break:break-all;word-wrap:break-word;=1&&y1m)dy=y1-y;dx=x1-x;%双线性插值fval=(F(y1+1,x1)-F(y1,x1))*dy+(F(y1,x1+1)-F(y1,x1))*dx+(F(y1+1,x1+1)+F(y1,x1)-F(y1,x1+1)-F(y1+1,x1))*dy*dx+F(y1,x1);outImg(j,i)=abs((rval-fval).^0.7*exp(-min()/20));%outImg(j,i)=abs((rval-fval).^2.5/(min()).^0.2);endendend%outImg=uint8(outImg/max(max(outImg))*255);%双线性插值求互信息functionout=BI_mi(tx,ty,ang,R,F)=size(R);hist=zeros(256,256);ha=zeros(1,256);hb=zeros(1,256);%归一化到256级灰度%ifmax(max(r))~=min(min(r))%max(max(a))结果是A中最大的元素,max(A)结果一个行向量,元素分别是A的每个列向量的最大的元素%r=(r-min(min(r)))/(max(max(r))-min(min(r)));%else%r=zeros(M,N);%end%%ifmax(max(f))-min(min(f))%f=(f-min(min(f)))/(max(max(f))-min(min(f)));%else%f=zeros(M,N);%end%%r=double(int16(r*255))+1;%f=double(int16(f*255))+1;R=R+1;F=F+1;theta=ang*pi/180;%旋转角度转弧度cx=floor(n/2);%旋转的中心点cy=floor(m/2);%求联合概率密度forj=1:mfori=1:n%参考图像在浮动图像平移后的对应点%x=i-tx;%列%y=j-ty;%x=(i-cx)*cos(theta)-(j-cy)*sin(theta)+cx-tx;y=(i-cx)*sin(theta)+(j-cy)*cos(theta)+cy-ty;x1=floor(x);y1=floor(y);rval=R(j,i);%图像重合部分求差if(x1=1&&x1n&&y1style=margin:0px;word-break:break-all;word-wrap:break-word;=1&&y1m)dy=y1-y;dx=x1-x;%双线性插值fval=(F(y1+1,x1)-F(y1,x1))*dy+(F(y1,x1+1)-F(y1,x1))*dx+(F(y1+1,x1+1)+F(y1,x1)-F(y1,x1+1)-F(y1+1,x1))*dy*dx+F(y1,x1);hist(fval,rval)=hist(fval,rval)+1;endendend%下面求边缘概率密度fori=1:256ha(i)=sum(hist(i,:));hb(i)=sum(hist(:,i));end%调用互信息函数out=MI(hist,ha,hb);%下面是求互信息的函数functionmi=MI(hist,ha,hb)%hsum=sum(sum(hist));index=find(hist~=0);p=hist/hsum;Hab=sum(sum(-p(index).*log(p(index))));%联合熵hsum=sum(sum(ha));index=find(ha~=0);p=ha/hsum;Ha=sum(sum(-p(index).*log(p(index))));%边缘熵hsum=sum(sum(hb));index=find(hb~=0);p=hb/hsum;Hb=sum(sum(-p(index).*log(p(index))));%边缘熵mi=Ha+Hb-Hab;%粒子群算法function=PSO(R,F)%粒子群算法%初始化D=3;%维数ps=30;%种群规模VRmin=ones(D,1)*-20;%最小速度VRmax=ones(D,1)*20;%最大速度VR=;%minmax=1;pos=40*rand(ps,D)-20;%随机产生初始位置vel=8*rand(ps,D)-4;%产生随机速度%ps=15;%种群规模%VRmin=ones(D,1)*-10;%最小速度%VRmax=ones(D,1)*10;%最大速度%VR=;%%minmax=1;%pos=20*rand(ps,D)-10;%随机产生初始位置%vel=4*rand(ps,D)-2;%产生随机速度%一些参数maxinterations=20;%最大迭代次数iw=1;%固定权重iw1=0.9;%最大惯性权重iw2=0.4;iwe=15;ac1=2;ac2=2;flagg=1;%ergrd=1e-5;%ergrdep=5;%%mv=4;%ergrd=1e-4;ergrdep=5;%mv=4;%%初始个体极值pbest=pos;%求初始全局极值fori=1:psp=pos(i,:);%第i个粒子位置out(i)=BI_mi(p(1),p(2),p(3),R,F);%求函数值endpbestval=out;%每个粒子当前函数值=max(pbestval);%全局最优函数值gbest=pbest(idx,:);%全局极值tr(1)=gbestval;%保存当前全局最优函数值%startPSOiterativeprocedurescnt=0;%counterusedforupdatingdisplayaccordingtodfintheoptionscnt2=0;%counterusedforthestoppingsubroutinebasedonerrorconvergence%开始迭代fori=1:maxinterationsforj=1:psifflagg==1%randomizationcontrol,onerandomsetforeachparticleateachepochrannum1=rand(1);rannum2=rand(1);endp=pos(j,:);%第i个粒子位置out(j)=BI_mi(p(1),p(2),p(3),R,F);%求函数值e(j)=out(j);%更新pbestifpbestval(j)=e(j);%%%====pbestval(j)=e(j);pbest(j,:)=pos(j,:);end%更新gbest=max(pbestval);ifgbestval=iterbestval%%%===gbestval=iterbestval;gbest=pbest(idx1,:);endtr(i+1)=gbestval;te=i;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%更新速度,位置%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ifi=iwe%%%===iwt(i)=((iw2-iw1)/(iwe-1))*(i-1)+iw1;%惯性权重elseiwt(i)=iw2;end%iwt(i)=1;%%%%%%%%%%速度%%%%%%%%%%%%%%%%%%%%ifflagg==2%粒子的每个参数的随机数不同fordimcnt=1:Drannum1=ra
本文标题:目标检测算法
链接地址:https://www.777doc.com/doc-2170806 .html