目标检测算法讲解.docx
- 文档编号:28011273
- 上传时间:2023-07-07
- 格式:DOCX
- 页数:17
- 大小:78.06KB
目标检测算法讲解.docx
《目标检测算法讲解.docx》由会员分享,可在线阅读,更多相关《目标检测算法讲解.docx(17页珍藏版)》请在冰豆网上搜索。
目标检测算法讲解
function MovingTargetDetectionByMMI()
%MovingTargetDetection
%BasedonMaximunMutualInformation
%
%EDITBYPSL@CSU
%QQ:
547423688
%Email:
anyuezhiji@
%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:
4
d=imfilter(d,se);
end
fori=1:
2
d=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);
whilemi<1.2
=PSO(CA1,CA2);
end
fprintf('tx:
%f ty:
%f ang:
%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.01
mi_old=mi;
=powell(R,F,pa);
end
fprintf('------Powellend\n\n');
time=toc;
fprintf('tx:
%.4f ty:
%.4f ang:
%.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:
m
fori=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&&x1 0px;word-break: break-all;word-wrap: break-word;">=1&&y1 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); end end end %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: m fori=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&&x1 0px;word-break: break-all;word-wrap: break-word;">=1&&y1 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; end end end %下面求边缘概率密度 fori=1: 256 ha(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: ps p=pos(i,: ); %第i个粒子位置 out(i)=BI_mi(p (1),p (2),p(3),R,F); %求函数值 end pbestval=out; %每个粒子当前函数值 =max(pbestval); %全局最优函数值 gbest=pbest(idx,: ); %全局极值 tr (1)=gbestval; %保存当前全局最优函数值 %startPSOiterativeprocedures cnt=0;%counterusedforupdatingdisplayaccordingtodfintheoptions cnt2=0;%counterusedforthestoppingsubroutinebasedonerrorconvergence %开始迭代 fori=1: maxinterations forj=1: ps ifflagg==1 %randomizationcontrol,onerandomsetforeachparticleateachepoch rannum1=rand (1); rannum2=rand (1); end p=pos(j,: ); %第i个粒子位置 out(j)=BI_mi(p (1),p (2),p(3),R,F); %求函数值 e(j)=out(j); %更新pbest ifpbestval(j)<=e(j);%%%==== pbestval(j)=e(j); pbest(j,: )=pos(j,: ); end %更新gbest =max(pbestval); ifgbestval<=iterbestval%%%=== gbestval=iterbestval; gbest=pbest(idx1,: ); end tr(i+1)=gbestval; te=i; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%更新速度,位置 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ifi<=iwe%%%=== iwt(i)=((iw2-iw1)/(iwe-1))*(i-1)+iw1; %惯性权重 else iwt(i)=iw2; end %iwt(i)=1; %%%%%%%%%%速度%%%%%%%%%%%%%%%%%%%% ifflagg==2 %粒子的每个参数的随机数不同 fordimcnt=1: D rannum1=rand (1); rannum2=rand (1); vel(j,dimcnt)=iwt(i)*vel(j,dimcnt)... +ac1*rannum1*(pbest(j,dimcnt)-pos(j,dimcnt))... +ac2*rannum2*(gbest(1,dimcnt)-pos(j,dimcnt)); end else %粒子的每个参数的随机数相同 vel(j,: )=iwt(i)*vel(j,: )... +ac1*rannum1*(pbest(j,: )-pos(j,: ))... +ac2*rannum2*(gbest(1,: )-pos(j,: )); end % 固定权重 % vel(j,: )=iw*vel(j,: )... % +ac1*rannum1*(pbest(j,: )-pos(j,: ))... % +ac2*rannum2*(gbest(1,: )-pos(j,: )); %%%%%%%%%%位置%%%%%%%%%%%%%%%%%%%%%%% pos(j,: )=pos(j,: )+vel(j,: ); %%%%%%%%%速度和位置范围%%%%%%%%% fordimcnt=1: D ifvel(j,dimcnt)>mv vel(j,dimcnt)=mv; end ifvel(j,dimcnt)<-mv vel(j,dimcnt)=-mv; end ifpos(j,dimcnt)>=VR(dimcnt,2) pos(j,dimcnt)=VR(dimcnt,2); end ifpos(j,dimcnt)<=VR(dimcnt,1)%%%=== pos(j,dimcnt)=VR(dimcnt,1); end end end %结束一次粒子循环 temp=gbest'; fprintf('%f,%f,%f,%f\n',temp (1),temp (2),temp(3),gbestval); Y(i)=gbestval; X(i)=i; %收敛条件 tmp1=abs(tr(i)-gbestval); iftmp1>ergrd cnt2=0; elseiftmp1<=ergrd%%%=== cnt2=cnt2+1; ifcnt2>=ergrdep break end end end %迭代结束 fprintf('totalinterations: %d\n',i); %OUT=; pa=gbest; mi=gbestval; %POWELL优化算法 function=powell(R,F,x) len=5; %搜索区间 itmax=30;%最大循环次数 e=1e-3; %允许误差 %方向矢量矩阵存放d1,d2,d3三个方向矢量 D=; %起始点 %x0=; x0=x; fx0=BI_mi(x0 (1),x0 (2),x0(3),R,F); %fx0=pv_mi(x0 (1),x0 (2),-x0(3),R,F); %循环搜索 fork=0: itmax %从起始点出发,沿d1方向搜索,得到最大值fx1,对应点x1 d1=D(1,: ); =oneDimSearch(R,F,x0,d1,len); fprintf('%f, %f, %f, %f\n',x1 (1),x1 (2),x1(3),fx1); %从x1出发,沿d2方向搜索 d2=D(2,: ); =oneDimSearch(R,F,x1,d2,len); fprintf('%f, %f, %f, %f\n',x2 (1),x2 (2),x2(3),fx2); %从x2出发,沿d3方向搜索 d3=D(3,: ); =oneDimSearch(R,F,x2,d3,len); %加速方向 dn=x3-x0; y=sum(dn.*dn); fprintf('%f\n',y); fprintf('%f, %f, %f, %f\n',x3 (1),x3 (2),x3(3),fx3); ify<=e %满足结束条件,退出循环%%%=== pa=x3; mi=fx3; return; end %调整搜索方向 %计算目标值下降最多的那个方向 cha=; =max(cha); % xe=2*x3-x0; fe=BI_mi(xe (1),xe (2),xe(3),R,F); %fe=pv_mi(xe (1),xe (2),-xe(3),R,F); %这里求极大值 if(fe<=fx0)||(2*(fx0-2*fx3+fe)*(fx0-fx3-maxcha)*(fx0-fx3-maxcha)>=(fx0-fe)*(fx0-fe)*maxcha) %不引入新的方向%%%=== x0=x3; %下次搜索的新起点 fx0=fx3; else %引进新的方向 %以xn为起点沿dn进行搜索,得到下次搜索的新起点 =oneDimSearch(R,F,x3,dn,len); %替换方向 D(4,: )=dn; fori=j0: 3 D(i,: )=D(i+1,: ); end end end %endfor pa=x3; mi=fx3; %一维搜索。 从-len到len,找出最大值的点 function=oneDimSearch(R,F,X,direction,len) %一维brent搜索求函数最小值 CGOLD=0.3819660; a=-len; b=len; %a=a0; %b=b0; tol=0.01; e=0.0; ITMAX=100; v=a+CGOLD*(b-a); Xv=X+direction*v; %fv=pv_mi(Xv (1),Xv (2),-Xv(3),R,F); fv=BI_mi(Xv (1),Xv (2),Xv(3),R,F); w=v; x=v; fw=fv; fx=fv; fork=0: ITMAX xm=0.5*(a+b); %中点 tol1=tol*abs(x); tol2=2*tol1;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 目标 检测 算法 讲解