clc,clf,clear all,close all;
path='image200s/';
filecount = 200;
for i=1:filecount
name=num2str(i);
if i<=9
filename=strcat('0000000',name, '.bmp');
elseif i<=99
filename=strcat('000000',name,'.bmp');
elseif i<=199
filename=strcat('00000',name,'.bmp');
end
I=imread([path filename]); % 序列图像的读取
I1=im2bw(I,0.75); % 图像分割
% 形心计算
[m n]=size(I1);
k=0;
xc=0;
yc=0;
for a=1:m
for b=1:(n-10)
if I1(a,b)==0;
k=k+1;
xc=xc+a;
yc=yc+b;
end
end
end
% 形心跟踪
imshow(I,[]);rectangle('position',[yc/k-32,xc/k-16,64,32]);
title(['Frame NO. ' num2str(i)]);
pause(0.01);
end
clc,clf,clear all,close all;
path = 'f16takeoff_396s/';
prefix = '';
fmt = '.jpg';
nframes = 396;
% 参考模版制作
I0=rgb2gray(imread('f16takeoff_396s/001.jpg'));
I0=double(I0);
L=10;
x0=90;y0=130;
height=40;width=110;
M1=I0(x0:x0+height,y0:y0+width);
% 模板匹配
for k = 1:nframes
fnum = num2str(k, '%03d');
fileName = strcat(path,prefix,fnum,fmt);
source=imread(fileName);
sourceImg =im2double(rgb2gray((source)));
A=zeros(L+1,L+1);
for i=(x0-L/2):(x0+L/2)
for j=(y0-L/2):(y0+L/2)
M2=sourceImg(i:i+height,j:j+width);
A(i+(L/2)+1-x0,j+(L/2)+1-y0)=sum(sum(abs(M2-M1)));
end
end
[q,p]=find(A==min(min(A)));
x1=x0-(L/2)+q-1;
y1=y0-(L/2)-1+p;
imshow(source),hold on
rectangle('Position',[y1 x1 width height],'edgecolor','k');
title(['Frame NO. ' num2str(k)]);
x0=x1;
y0=y1;
M1=sourceImg(x0:x0+height,y0:y0+width); % 搜索区域
MAD(1:1+height,1:1+width,k)=1/(width*height)*M1; % 匹配准则
pause(0.01);
end