clear all;
I=mmreader('.avi');
frame=I.NumberOfFrames;
rgb=read(I,1); % 读入第一帧作为先前帧
imshow(rgb);
[temp,rect]=imcrop(rgb);
tempsift=rgb2gray(temp) ;
[image1, des1, loc1]= sift(tempsift);
y(1)=rect(3)/2;
y(2)=rect(4)/2;
a0=ceil(rect(4));
b0=ceil(rect(3));
m_wei=zeros(a0,b0);%权值矩阵
h0=y(1)^2+y(2)^2 ;%带宽的平方
%计算目标区域权值矩阵
for i=1:a0
for j=1:b0
dist=(i-y(2))^2+(j-y(1))^2;
m_wei(i,j)=1-dist/h0; %epanechnikov profile
end
end
C0=1/sum(sum(m_wei));%归一化系数
hist1=zeros(1,4096);
q_temp=zeros(a0,b0);
for i=1:a0
for j=1:b0
%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(i,j)=q_r*256+q_g*16+q_b;
hist1(q_temp(i,j)+1)= hist1(q_temp(i,j)+1)+m_wei(i,j);
end
end
hist1=hist1*C0;
width=rect(3);
length=rect(4);
%读取序列图像
for t=2:frame;
Im=read(I,t);
Y=[2,2];
rect(4)=ceil(rect(4));
rect(3)=ceil(rect(3));
%x0=[rect(1),rect(2)];
num=0;
%mean shift运算
while(num<20) %迭代条件
num=num+1;
temp1=imcrop(Im,rect);
a1=ceil(rect(4));
b1=ceil(rect(3));
y(1)=rect(3)/2;
y(2)=rect(4)/2;
h1=y(1)^2+y(2)^2;
h_wei=zeros(a1,b1);%权值矩阵
%计算候选区域权值矩阵
for i=1:a1
for j=1:b1
dist=(i-y(2))^2+(j-y(1))^2;
h_wei(i,j)=1-dist/h1; %epanechnikov profile
end
end
C1=1/sum(sum(h_wei));%归一化系数
%计算侯选区域直方图
hist2=zeros(1,4096);
q_temp1=zeros(a1,b1);
for i=1:a1
for j=1:b1
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)+h_wei(i,j);
end
end
hist2=hist2*C1;
w=weights(hist1,hist2);
%b0=sum(sqrt(hist1.*hist2));
sum_w=0;
xw=[0,0];
for i=1:a1
for j=1:b1
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(2)-0.5,j-y(1)-0.5];
end
end
Y=xw/sum_w;
rect(1)=ceil(rect(1)+Y(2));
rect(2)=ceil(rect(2)+Y(1));
% x1=[rect(1),rect(2)];
% while(1)
% %更新候选区域,并再次计算其直方图
% temp2=imcrop(Im,rect);
% hist3=zeros(1,4096);
% q_temp2=zeros(a1,b1);
% for i=1:a1
% for j=1:b1
% q_r=fix(double(temp2(i,j,1))/16);
% q_g=fix(double(temp2(i,j,2))/16);
% q_b=fix(double(temp2(i,j,3))/16);
% q_temp2(i,j)=q_r*256+q_g*16+q_b;
% hist3(q_temp2(i,j)+1)= hist3(q_temp2(i,j)+1)+h_wei(i,j);
% end
% end
%
% hist3=hist3*C1;
% b1=sum(sqrt(hist1.*hist3));
% x1=(x0+x1)/2;
% if (Y(1)^2+Y(2)^2>0.5 | b0<b1) %迭代条件
% break
% end
end
%基于sift的尺度估计
[tempsift1,useless]=imcrop(Im,rect);
tempsift1=rgb2gray(tempsift1);
[image2, des2, loc2]= sift(tempsift1);
des2t = des2';
match=zeros(1,size(des1,1));
for i = 1 : size(des1,1)
dotprods = des1(i,:) * des2t;
[vals,indx] = sort(acos(dotprods));
if (vals(1) < 0.7 * vals(2))
match(i) = indx(1);
else
match(i) = 0;
end
end
number = sum(match > 0); %提取匹配特征点的坐标
q = match~=0;
k2 = match(q);
k1 = find(match);
s1 = 0;
s2 = 0;
o1 = 0;
o2 = 0;
fprintf('Found %d matches.\n', number);
for i=1:number
s1=s1+loc1(k1(i),3);
s2=s2+loc2(k2(i),3);
o1=o1+loc1(k1(i),4);
o2=o2+loc2(k2(i),4);
end
if s2==0;
s=1;
else
s=s2/s1;
end
rect(3)=s*width;
rect(4)=s*length;
v1=[rect(1),rect(2)];
v2=[rect(1)+rect(3),rect(2)];
v3=[rect(1)+rect(3),rect(2)+rect(4)];
v4=[rect(1),rect(2)+rect(4)];
%显示跟踪结果
figure(3)
clf
imshow(uint8(Im))
hold on;
plot([v1(1),v2(1)],[v1(2),v2(2)],[v2(1),v3(1)],[v2(2),v3(2)],[v3(1),v4(1)],[v3(2),v4(2)],[v4(1),v1(1)],[v4(2),v1(2)],'LineWidth',3,'Color','g')
end
sift算法 目标检测
5星 · 超过95%的资源 需积分: 32 197 浏览量
2012-10-19
15:10:35
上传
评论 2
收藏 6KB RAR 举报
cold_east
- 粉丝: 0
- 资源: 1
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈