clear all;
I=imread('E:\Desktop\Walking\img\0001.jpg');
figure(1);imshow(I);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%在图像中画框temp返回框中图像信息,rect返回框的坐标、大小信息
[temp,rect]=imcrop(I);
[a b c]=size(temp);%长宽3
tic_x=rect(1)+rect(3)/2; %%%表示剪切中心点的x坐标
tic_y=rect(2)+rect(4)/2; %%%表示剪切中心点的y坐标
m_wei=zeros(a,b);%权值矩阵(全零矩阵)
y(1)=a/2; %中心点位置
y(2)=b/2;
h=y(1)^2+y(2)^2 ;%h为核函数的带宽,一般设为跟踪目标窗口的一半
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%初始(目标模型)图像的权值
for i=1:a
for j=1:b
dist=(i-y(1))^2+(j-y(2))^2; %%%求每个像素到中心点的距离dist
%设置权值,距离中心越远,权值越小
m_wei(i,j)=1-dist/h; %epanechnikov profile 抛物线形
end
end
C=1/sum(sum(m_wei));%为了保证总的概率为1的归一化系数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%目标权值直方图
hist1=zeros(1,4372);
for i=1:a
for j=1:b
%rgb颜色空间量化为16*16*16 bins
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); %计算直方图统计中每个像素点占的权重
end
end
hist1=hist1*C;
subplot(131);plot(hist1);
rect(3)=ceil(rect(3)); %%%向右取整(矩阵的长宽)
rect(4)=ceil(rect(4));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%读取序列图像
myfile=dir('E:\Desktop\Walking\img*.jpg');
lengthfile=length(myfile);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for l=2:lengthfile
%读文件
filePath = ['E:\Desktop\Walking\img\',myfile(l).name]
Im=imread(filePath);
%????
num=0;
Y=[2,2];
%%%%%%%mean shift迭代
while((Y(1)^2+Y(2)^2>0.5)) %迭代条件
display(Y(1));
display(Y(2));
num=num+1;
temp1=imcrop(Im,rect); %%%把其他帧的与剪切出来相同位置的矩阵拿出来(指定了剪切的位置)
%计算侯选区域直方图
%hist2=C*wei_hist(temp1,m_wei,a,b);%target candidates pu
hist2=zeros(1,4372);
for i=1:a
for j=1:b
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);
end
end
hist2=hist2*C;
subplot(1,3,2);
plot(hist2);
hold on;
w=zeros(1,4372);
for i=1:4372
if(hist2(i)~=0) %%%不等于0的时候
w(i)=sqrt(hist1(i)/hist2(i));
else
w(i)=0;
end
end
%变量初始化
sum_w=0;%计算的总的wi
xw=[0,0];
for i=1:a;
for j=1:b
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]); %减0.5是对计算精度的控制
end
end
Y=xw/sum_w;
%中心点位置更新 (起始点的变化)
rect(1)=rect(1)+Y(2);
rect(2)=rect(2)+Y(1);
end
%%%跟踪轨迹矩阵%%%
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,3,3);
imshow(uint8(Im));
title('目标跟踪结果及其运动轨迹');
hold on;
% 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([v1,v1+v3],[v2,v2],[v1+v3,v1+v3],[v2,v2+v4],[v1,v1+v3],[v2+v4,v2+v4],[v1,v1],[v2,v2+v4],'LineWidth',2,'Color','b');
plot(tic_x,tic_y,'LineWidth',2,'Color','b');
end
这几行代码:
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]); %减0.5是对计算精度的控制
end
end
Y=xw/sum_w;
rect(1)=rect(1)+Y(2);
rect(2)=rect(2)+Y(1);
xw代表什么意思?
另外为什么Y算出来的是偏差值,和meanshift公式不一样啊