文档介绍:MeanShift运动目标跟踪matlab程序思路简介:,先随机取一块与rect等大的矩形,计算加权直方图hist2。,如果后者差距过大,更新新的矩阵中心Y,进行迭代(MeanShift是一种变步长可以迅速接近概率密度峰值的方法),直至一定条件后停止。源程序(参考过其他程序):[plain] viewplaincopy  function[]=select()closeall;clearall;%%%%%%%%%%%%%%%%%%根据一幅目标全可见的图像圈定跟踪目标%%%%%%%%%%%%%%%%%%%%%%%I=imread('E:\数字图像处理\6\2\');figure(1);imshow(I);[temp,rect]=imcrop(I);[a,b,c]=size(temp); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%计算目标图像的权值矩阵%%%%%%%%%%%%%%%%%%%%%%%y(1)=a/2;y(2)=b/2;tic_x=rect(1)+rect(3)/2;tic_y=rect(2)+rect(4)/2;m_wei=zeros(a,b);%权值矩阵h=y(1)^2+y(2)^2;%带宽fori=1:a  forj=1:b    dist=(i-y(1))^2+(j-y(2))^2;    m_wei(i,j)=1-dist/h;%epanechnikovprofile  endendC=1/sum(sum(m_wei));%归一化系数%计算目标权值直方图qu%hist1=C*wei_hist(temp,m_wei,a,b);%targetmodelhist1=zeros(1,a*b);fori=1:a  forj=1:b      %rgb颜色空间量化为16*16*16bins    q_r=fix(double(temp(i,j,1))/16); %fix为趋近0取整函数    q_g=fix(double(temp(i,j,2))/16);    q_b=fix(double(temp(i,j,3))/16);    q_temp=q_r*256+q_g*16+q_b;      %设置每个像素点红色、绿色、蓝色分量所占比重          hist1(q_temp+1)=hist1(q_temp+1)+m_wei(i,j);  %计算直方图统计中每个像素点占的权重  endendhist1=hist1*C;rect(3)=ceil(rect(3));rect(4)=ceil(rect(4));%%%%%%%%%%%%%%%%%%%%%%%%%读取序列图像myfile=dir('*.bmp');lengthfile=length(myfile);forl=1:lengthfile  Im=imread(myfile(l).name);  num=0;  Y=[2,2];   %%%%%%%meanshift迭代  while((Y(1)^2+Y(2)^2>)&num<20) %迭代条件    num=num+1;    temp1=imcrop(Im,rect);     %计算