clc;
clear;
close all;
warning off;
addpath 'func\'
addpath 'vedio\'
%原重采样
Samples = 1;
SEL = 1;
%粒子更新滤波
Para_Updata = [1,0,1,0;
0,1,0,1;
0,0,1,0;
0,0,0,1];
Num_Particles = 30000;
X_rgb = 30;
X_pos = 15;
X_vec = 5;
%读取视频以及视频参数
PIX = VideoReader('a.avi');
PIX_Size = [PIX.Width,PIX.Height];
Num_Frame = floor(PIX.Duration * PIX.FrameRate);
%粒子初始化
X1 = func_Particle_initial(PIX_Size,Num_Particles);
X2 = func_Particle_initial(PIX_Size,Num_Particles);
for Frm = 15:Num_Frame-15
%获得每一帧图像
PIX_each_frame = read(PIX,Frm); %进行差分运算
Y1 = read(PIX,Frm-1);
Y2 = read(PIX,Frm);
tmps = abs(Y1-Y2);
PIX_DIFF = 255*im2bw(tmps,graythresh(tmps));
PIX_DIFF2= im2bw(tmps,graythresh(tmps));
%通过帧差,锁定目标,然后计算目标的平均像素值
XX=0;
Target = [1;1;1];
for i = 1:size(Y1,1)
for j = 1:size(Y1,2)
if PIX_DIFF(i,j) == 255
Target(1,1) = Target(1,1)+Y1(i,j,1);
Target(2,1) = Target(2,1)+Y1(i,j,2);
Target(3,1) = Target(3,1)+Y1(i,j,3);
XX=XX+1;
end
end
end
Target = Target/XX;
%更新粒子
X1 = func_Particle_Updata(Para_Updata,X_pos,X_vec,X1);
%计算状态
STATE1 = func_Likelihood(X_rgb,[255;255;255],X1(1:2,:),PIX_DIFF);
%粒子重采样
X1 = func_Particle_Resample(X1,STATE1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%更新粒子
X2 = func_Particle_Updata(Para_Updata,X_pos,X_vec,X2);
%计算状态
STATE2 = func_Likelihood(X_rgb,Target,X2(1:2,:),PIX_each_frame);
%粒子重采样
X2 = func_Particle_Resample(X2,STATE2);
%跟踪效果显示
figure(1);
image(PIX_each_frame)
if Samples == 1
STR = ['(原重采样)粒子跟踪效果',num2str(Frm)];
else
STR = ['(改进重采样)粒子跟踪效果',num2str(Frm)];
end
title(STR)
hold on
plot(X2(2,:),X2(1,:),'g.');
hold on
plot(X1(2,:),X1(1,:),'g.');
hold off
drawnow;
end
end