本文共 1865 字,大约阅读时间需要 6 分钟。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下,请各位大神有事没事来看看~代码不多~应该不难的~在下感激不尽!!
clear,clc
%读入背景
Imzero = zeros(240,360,3);
Imzero=imread('background.jpg');
Imback=double(Imzero);
[MR,MC,Dim] = size(Imback);
%求出Vx 和 Vy
I0 = imread(strcat('man',int2str(0)) ,'jpg');
Imwork = double(I0);
[cc(1),cr(1),~, ~,~,index] = extractman(Imwork,Imback,1);
I79 = imread(strcat('man',int2str(79)) ,'jpg');
Imwork = double(I79);
[cc(80),cr(80),flag, stats,labeled,index] = extractman(Imwork,Imback,80);
Sx = cc(1)-cc(80);
Sy = cr(1)-cr(80);
Vx = Sx/80;
Vy = Sy/80;
% Kalman filter initialization
dt=1;
A=[[1,0,0,0]',[0,1,0,0]',[-Vx*dt,0,1,0]',[0,-Vy*dt,0,1]'];
C=[[1,0]',[0,1]',[0,0]',[0,0]'];
Q=0.5*eye(4);
R=[[0.2845,0.0045]',[0.0045,0.0455]'];
x=zeros(80,4);
P = 80*eye(4);
I = eye(4);
firstinit=1;
% loop over all images
h = 1;
for i = 1 : 20
% 装入各帧
Im = imread(strcat('man',int2str(i*4-1)) ,'jpg');
if h > 0
figure(h);
clf;
end
imshow(Im)
Imwork = double(Im);
%目标检测
[cc(i),cr(i),flag, stats,labeled,index] = extractman(Imwork,Imback,i);
%判断是否提取到目标特征
if flag==0
continue
end
%绘制检测结果的矩形框
rectangle('Position', round(stats(index).BoundingBox),'EdgeColor','g');
% 卡尔曼滤波
if firstinit == 1;
xp = [280,145,0,0]';
fristinit = 0;
else
xp=A*x(i-1,:)';
end
PP = A*P*A'+Q;
K = PP*C'*inv(C*PP*C'+R);
x(i,:) = (xp + K*([cc(i),cr(i)]' - C*xp))';
x(i,:)
[cc(i),cr(i)]
P = (eye(4)-K*C)*PP;
%P = (I - K*C)*PP*(I - K*C)' + K*R*K';
%inv(P) = inv(PP) + C'*inv(R)*K;
%绘制跟踪矩形框
[row, col, v] = find(labeled == index);
width = abs(max(col)-min(col));
hight = abs(max(row)-min(row));
xdownleft = x(i,1)-width/2;
ydownleft = x(i,2)-hight/2;
hold on
if width > 0 && hight > 0
rectangle('Position',[xdownleft,ydownleft,width, hight],'EdgeColor', 'r');
%plot(x(i,1),x(i,2),'r*');
end
%各帧之间的延迟
pause(0.3)
end
%显示检测中心点的轨迹
figure
plot(cc,'r*')
hold on
plot(cr,'g*')
%显示跟踪中心点的轨迹
figure
plot(x(:,1),'rx')
hold on
plot(x(:,2),'gx')
转载地址:http://jwrav.baihongyu.com/