基于MeanShift的目标跟踪算法及实现Word文档下载推荐.docx
《基于MeanShift的目标跟踪算法及实现Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《基于MeanShift的目标跟踪算法及实现Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
因为Meanshift算法是收敛的,因此在当前帧中通过反复迭代搜索特征空间中样本点最密集的区域,搜索点沿着样本点密度增加的方向“漂移”到局部密度极大点点xiN,也就是被认为的目标位置,从而达到跟踪的目的,MeanShift跟踪过程结束。
运动目标的实现过程【具体算法】:
三、代码实现
说明:
1.RGB颜色空间刨分,采用16*16*16的直方图
2.目标模型和候选模型的概率密度计算公式参照上文
3.opencv版本运行:
按P停止,截取目标,再按P,进行单目标跟踪
4.Matlab版本,将视频改为图片序列,第一帧停止,手工标定目标,双击目标区域,进行单目标跟踪。
matlab版本:
[plain]viewplaincopyfunction[]=select()
closeall;
clearall;
%%%%%%%%%%%%%%%%%%根据一幅目标全可见的图像圈定跟踪目标%%%%%%%%%%%%%%%%%%%%%%%
I=imread('
result72.jpg'
);
figure
(1);
imshow(I);
[temp,rect]=imcrop(I);
[a,b,c]=size(temp);
%a:
row,b:
col
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%计算目标图像的权值矩阵%%%%%%%%%%%%%%%%%%%%%%%
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
end
end
C=1/sum(sum(m_wei));
%归一化系数
%计算目标权值直方图qu
%hist1=C*wei_hist(temp,m_wei,a,b);
%targetmodel
hist1=zeros(1,4096);
%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);
%计算直方图统计中每个像素点占的权重
hist1=hist1*C;
rect(3)=ceil(rect(3));
rect(4)=ceil(rect(4));
%%%%%%%%%%%%%%%%%%%%%%%%%读取序列图像
myfile=dir('
D:
\matlab7\work\meanshift\image\*.jpg'
lengthfile=length(myfile);
forl=1:
lengthfile
Im=imread(myfile(l).name);
num=0;
Y=[2,2];
%%%%%%%meanshift迭代
while((Y
(1)^2+Y
(2)^2&
gt;
0.5)&
amp;
num&
lt;
20)%迭代条件
num=num+1;
temp1=imcrop(Im,rect);
%计算侯选区域直方图
%hist2=C*wei_hist(temp1,m_wei,a,b);
%targetcandidatespu
hist2=zeros(1,4096);
fori=1:
q_r=fix(double(temp1(i,j,1))/16);
q_g=fix(double(temp1(i,j,2))/16);
q_b=fix(double(temp1(i,j,3))/16);
q_temp1(i,j)=q_r*256+q_g*16+q_b;
hist2(q_temp1(i,j)+1)=hist2(q_temp1(i,j)+1)+m_wei(i,j);
hist2=hist2*C;
figure
(2);
subplot(1,2,1);
plot(hist2);
holdon;
w=zeros(1,4096);
4096
if(hist2(i)~=0)%不等于
w(i)=sqrt(hist1(i)/hist2(i));
else
w(i)=0;
%变量初始化
sum_w=0;
xw=[0,0];
a;
sum_w=sum_w+w(uint32(q_temp1(i,j))+1);
xw=xw+w(uint32(q_temp1(i,j))+1)*[i-y
(1)-0.5,j-y
(2)-0.5];
Y=xw/sum_w;
%中心点位置更新
rect
(1)=rect
(1)+Y
(2);
rect
(2)=rect
(2)+Y
(1);
%%%跟踪轨迹矩阵%%%
tic_x=[tic_x;
rect
(1)+rect(3)/2];
tic_y=[tic_y;
rect
(2)+rect(4)/2];
v1=rect
(1);
v2=rect
(2);
v3=rect(3);
v4=rect(4);
%%%显示跟踪结果%%%
subplot(1,2,2);
imshow(uint8(Im));
title('
目标跟踪结果及其运动轨迹'
plot([v1,v1+v3],[v2,v2],[v1,v1],[v2,v2+v4],[v1,v1+v3],[v2+v4,v2+v4],[v1+v3,v1+v3],[v2,v2+v4],'
LineWidth'
2,'
Color'
'
r'
plot(tic_x,tic_y,'
b'
end运行结果:
opencv版本:
[cpp]viewplaincopy#include"
stdafx.h"
#include"
cv.h"
highgui.h"
#defineu_charunsignedchar
#defineDIST0.5
#defineNUM20
//全局变量
boolpause=false;
boolis_tracking=false;
CvRectdrawing_box;
IplImage*current;
double*hist1,*hist2;
double*m_wei;
//权值矩阵
doubleC=0.0;
//归一化系数
voidinit_target(double*hist1,double*m_wei,IplImage*current)
{
IplImage*pic_hist=0;
intt_h,t_w,t_x,t_y;
doubleh,dist;
inti,j;
intq_r,q_g,q_b,q_temp;
t_h=drawing_box.height;
t_w=drawing_box.width;
t_x=drawing_box.x;
t_y=drawing_box.y;
h=pow(((double)t_w)/2,2)+pow(((double)t_h)/2,2);
//带宽
pic_hist=cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);
//生成直方图图像
//初始化权值矩阵和目标直方图
for(i=0;
i&
t_w*t_h;
i++)
{
m_wei[i]=0.0;
}
for(i=0;
i&
4096;
hist1[i]=0.0;
t_h;
i++)
for(j=0;
j&
t_w;
j++)
dist=pow(i-(double)t_h/2,2)+pow(j-(double)t_w/2,2);
m_wei[i*t_w+j]=1-dist/h;
//printf("
%f\n"
m_wei[i*t_w+j]);
C+=m_wei[i*t_w+j];
//计算目标权值直方
for(i=t_y;
t_y+t_h;
for(j=t_x;
t_x+t_w;
//rgb颜色空间量化为16*16*16bins
q_r=((u_char)current-&
imageData[i*current-&
widthStep+j*3+2])/16;
q_g=((u_char)current-&
widthStep+j*3+1])/16;
q_b=((u_char)current-&
widthStep+j*3+0])/16;
q_temp=q_r*256+q_g*16+q_b;
hist1[q_temp]=hist1[q_temp]+m_wei[(i-t_y)*t_w+(j-t_x)];
//归一化直方图
hist1[i]=hist1[i]/C;
hist1[i]);
//生成目标直方图
doubletemp_max=0.0;
4096;
i++)//求直方图最大值,为了归一化
val_hist[i]);
if(temp_max&
hist1[i])
temp_max=hist1[i];
//画直方图
CvPointp1,p2;
doublebin_width=(double)pic_hist-&
width/4096;
doublebin_unith=(double)pic_hist-&
height/temp_max;
p1.x=i*bin_width;
p1.y=pic_hist-&
height;
p2.x=(i+1)*bin_width;
p2.y=pic_hist-&
height-hist1[i]*bin_unith;
%d,%d,%d,%d\n"
p1.x,p1.y,p2.x,p2.y);
cvRectangle(pic_hist,p1,p2,cvScalar(0,255,0),-1,8,0);
cvSaveImage("
hist1.jpg"
pic_hist);
cvReleaseImage(&
pic_hist);
}
voidMeanShift_Tracking(IplImage*current)
intnum=0,i=0,j=0;
intt_w=0,t_h=0,t_x=0,t_y=0;
double*w=0,*hist2=0;
doublesum_w=0,x1=0,x2=0,y1=2.0,y2=2.0;
intq_r,q_g,q_b;
int*q_temp;
hist2=(double*)malloc(sizeof(double)*4096);
w=(double*)malloc(sizeof(double)*4096);
q_temp=(int*)malloc(sizeof(int)*t_w*t_h);
while((pow(y2,2)+pow(y1,2)&
0.5)&
&
(num&
NUM))
num++;
memset(q_temp,0,sizeof(int)*t_w*t_h);
w[i]=0.0;
hist2[i]=0.0;
t_h+t_y;
t_w+t_x;
j++)
q_temp[(i-t_y)*t_w+j-t_x]=q_r*256+q_g*16+q_b;
hist2[q_temp[(i-t_y)*t_w+j-t_x]]=hist2[q_temp[(i-t_y)*t_w+j-t_x]]+m_wei[(i-t_y)*t_w+j-t_x];
hist2[i]=hist2[i]/C;
hist2[i]);
hist2[i])
temp_max=hist2[i];
width/(4368);
height-hist2[i]*bin_unith;
hist2.jpg"
if(hist2[i]!
=0)
w[i]=sqrt(hist1[i]/hist2[i]);
}else
w[i]=0;
sum_w=0.0;
x1=0.0;
x2=0.0;
%d\n"
q_temp[i*t_w+j]);
sum_w=sum_w+w[q_temp[i*t_w+j]];
x1=x1+w[q_temp[i*t_w+j]]*(i-t_h/2);
x2=x2+w[q_temp[i*t_w+j]]*(j-t_w/2);
y1=x1/sum_w;
y2=x2/sum_w;
//中心点位置更新
drawing_box.x+=y2;
drawing_box.y+=y1;
%d,%d\n"
drawing_box.x,drawing_box.y);
free(hist2);
free(w);
free(q_temp);
//显示跟踪结果
cvRectangle(current,cvPoint(drawing_box.x,drawing_box.y),cvPoint(drawing_box.x+drawing_box.width,drawing_box.y+drawing_box.height),CV_RGB(255,0,0),2);
cvShowImage("
Meanshift"
current);
//cvSaveImage("
result.jpg"
voidonMouse(intevent,intx,inty,intflags,void*param)
if(pause)
switch(event)