求一段matlab代码使用mean-shift算法找出随机生成的1000个坐标值的中心 50
1个回答
展开全部
%mean shift tracking
%citation: Comaniciu D,Ramesh V,Meer P. Kernel-based object tracking [J].
% IEEE Transaction on Pattern Analysis and Machine Intelligence,2003, 25(5): 564-577.
% written by swf.
% date: 2008.12.
% 第一帧用鼠标选择要跟踪的物体
clear all;
rgb=imread('0329.jpg');
figure(1),imshow(rgb);
[temp,rect]=imcrop(rgb);
[a,b,c]=size(temp);
%目标中心坐标
y(1)=a/2;
y(2)=b/2;
m_wei=zeros(a,b);%权值矩阵
h=y(1)^2+y(2)^2 ;%带宽
%计算权值矩阵
for i=1:a
for j=1:b
dist=(i-y(1))^2+(j-y(2))^2;
m_wei(i,j)=1-dist/h; %epanechnikov profile
end
end
C=1/sum(sum(m_wei));%归一化系数
%计算目标权值直方图qu
%hist1=C*wei_hist(temp,m_wei,a,b);%target model
hist1=zeros(1,4096);
for i=1:a
for j=1:b
%rgb颜色空间量化为16*16*16 bins
q_r=fix(double(temp(i,j,1))/16);
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;
rect(3)=ceil(rect(3));
rect(4)=ceil(rect(4));
jishu=1;
%%%%%%%%%%%%%%%%%%%%%%%%%读取序列图像
for i=0329:0380;
Im=imread(['0',int2str(i),'.jpg']);
jishu=jishu+1;
%Im=medfilt2(Im);
num=0;
Y=[2,2];
tic
%%%%%%%mean shift迭代
while((Y(1)^2+Y(2)^2>0.5)&num<20) %迭代条件
num=num+1;
temp1=imcrop(Im,rect);
%计算侯选区域直方图
%hist2=C*wei_hist(temp1,m_wei,a,b);%target candidates pu
%
hist2=zeros(1,4096);
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;
%bdist1 = bhattacharyya(hist1, hist2)
w=weights(hist1,hist2);
% w=sqrt(hist1./hist2);
%变量初始化
sum_w=0;
xw=[0,0];
for i=1:a;
for j=1:b
bitwei(i,j)=w(uint32(q_temp1(i,j))+1);
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];
end
end
Y=xw/sum_w
%中心点位置更新
rect(1)=rect(1)+Y(2);
rect(2)=rect(2)+Y(1);
end
v1=rect(1);
v2=rect(2);
v3=rect(3);
v4=rect(4);
%%显示跟踪结果
figure(2)
clf
imshow(uint8(Im))
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',1,'Color','w')
t(jishu)=toc; %统计程序运行时间
end
weight.m文件
function w=weights(hist1,hist2);
w=zeros(1,4096);
for i=1:4096
if(hist2(i)~=0)
w(i)=sqrt(hist1(i)/hist2(i));
else
w(i)=0;
end
end
return
%citation: Comaniciu D,Ramesh V,Meer P. Kernel-based object tracking [J].
% IEEE Transaction on Pattern Analysis and Machine Intelligence,2003, 25(5): 564-577.
% written by swf.
% date: 2008.12.
% 第一帧用鼠标选择要跟踪的物体
clear all;
rgb=imread('0329.jpg');
figure(1),imshow(rgb);
[temp,rect]=imcrop(rgb);
[a,b,c]=size(temp);
%目标中心坐标
y(1)=a/2;
y(2)=b/2;
m_wei=zeros(a,b);%权值矩阵
h=y(1)^2+y(2)^2 ;%带宽
%计算权值矩阵
for i=1:a
for j=1:b
dist=(i-y(1))^2+(j-y(2))^2;
m_wei(i,j)=1-dist/h; %epanechnikov profile
end
end
C=1/sum(sum(m_wei));%归一化系数
%计算目标权值直方图qu
%hist1=C*wei_hist(temp,m_wei,a,b);%target model
hist1=zeros(1,4096);
for i=1:a
for j=1:b
%rgb颜色空间量化为16*16*16 bins
q_r=fix(double(temp(i,j,1))/16);
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;
rect(3)=ceil(rect(3));
rect(4)=ceil(rect(4));
jishu=1;
%%%%%%%%%%%%%%%%%%%%%%%%%读取序列图像
for i=0329:0380;
Im=imread(['0',int2str(i),'.jpg']);
jishu=jishu+1;
%Im=medfilt2(Im);
num=0;
Y=[2,2];
tic
%%%%%%%mean shift迭代
while((Y(1)^2+Y(2)^2>0.5)&num<20) %迭代条件
num=num+1;
temp1=imcrop(Im,rect);
%计算侯选区域直方图
%hist2=C*wei_hist(temp1,m_wei,a,b);%target candidates pu
%
hist2=zeros(1,4096);
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;
%bdist1 = bhattacharyya(hist1, hist2)
w=weights(hist1,hist2);
% w=sqrt(hist1./hist2);
%变量初始化
sum_w=0;
xw=[0,0];
for i=1:a;
for j=1:b
bitwei(i,j)=w(uint32(q_temp1(i,j))+1);
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];
end
end
Y=xw/sum_w
%中心点位置更新
rect(1)=rect(1)+Y(2);
rect(2)=rect(2)+Y(1);
end
v1=rect(1);
v2=rect(2);
v3=rect(3);
v4=rect(4);
%%显示跟踪结果
figure(2)
clf
imshow(uint8(Im))
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',1,'Color','w')
t(jishu)=toc; %统计程序运行时间
end
weight.m文件
function w=weights(hist1,hist2);
w=zeros(1,4096);
for i=1:4096
if(hist2(i)~=0)
w(i)=sqrt(hist1(i)/hist2(i));
else
w(i)=0;
end
end
return
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询