clc
clear
% 载入图片
imgPath = 'D:\毕业设计\数据集视频\data1'; imgExt = 'jpg'; %定义文件路径
%检查图片文件路径是否存在
if isdir(imgPath) == 0
error('USER ERROR : The image directory does not exist');
end
%载入路径中的文件
filename = dir([imgPath filesep '*.' imgExt]); % 获取目录下所有文件
Imgnum = size(filename,1); %图片数量
if Imgnum < 0
error('No image!');
end
disp('Loading image files from the video sequence, please be patient...');
%获取图片参数
Imgname = [imgPath filesep filename(1).name]; %获取图片名
fr_g = imread(Imgname);
fr_g=rgb2gray(fr_g);
[height,weight]=size(fr_g);
ImgSeq = zeros(height, weight, Imgnum);%读取目录下全部图片
for i=1:Imgnum
Imgname = [imgPath filesep filename(i).name]; %获取图片名
Img=imread(Imgname);
Im(:,:,:,i)=double(Img)/255;
fr_g1=rgb2gray(Img);
fr_g1=double(fr_g1)/255;
ImgSeq(:,:,i) = fr_g1(:,:); %放入所有图片
end
disp(' ... OK!');
%选取目标区域
Imzero = zeros(height,weight,3);
for i = 1:5
Imzero = Im(:,:,:,i)+Imzero;
end
Imgback = Imzero/5*255;
Imgwork =Im(:,:,:,1)*255;
im= ImgSeq(:,:,1);
f = figure('Name', 'Select object to track'); imshow(Imgwork/255);
[rect(1),rect(2),rect(3),rect(4)] = extract(Imgwork,Imgback);
hold on
rectangle('Position', rect, 'EdgeColor','r');%画矩阵框出初始目标位置
center = [rect(2)+rect(4)/2 rect(1)+rect(3)/2 ];
%高斯
sigma = 100;
gsize = size(im);
[R,C] = ndgrid(1:gsize(1), 1:gsize(2));
g = gaussC(R,C, sigma, center);
g = mat2gray(g);
img = imcrop(im, rect);
g = imcrop(g, rect);
G = fft2(g);
height = size(g,1);
width = size(g,2);
fi = preprocess(imresize(img, [height width]));
Ai = (G.*conj(fft2(fi)));
Bi = (fft2(fi).*conj(fft2(fi)));
N = 128;
for i = 1:N
fi = preprocess(rand_warp(img));
Ai = Ai + (G.*conj(fft2(fi)));
Bi = Bi + (fft2(fi).*conj(fft2(fi)));
end
% 训练滤波器
eta = 0.125; %
fig = figure('Name', 'MOSSE');
for i = 1:Imgnum
img = ImgSeq(:,:,i);
im = img;
if (i == 1)
Ai = eta.*Ai;
Bi = eta.*Bi;
else
Hi = Ai./Bi;
fi = imcrop(img, rect);
fi = preprocess(imresize(fi, [height width]));
gi = uint8(255*mat2gray(ifft2(Hi.*fft2(fi))));
maxval = max(gi(:));
[P, Q] = find(gi == maxval);
dx = mean(P)-height/2;
dy = mean(Q)-width/2;
rect = [rect(1)+dy rect(2)+dx width height];
fi = imcrop(img, rect);
fi = preprocess(imresize(fi, [height width]));
Ai = eta.*(G.*conj(fft2(fi))) + (1-eta).*Ai;
Bi = eta.*(fft2(fi).*conj(fft2(fi))) + (1-eta).*Bi;
end
% 输出
subplot(1,1,1); imshow(im, []);
hold on;
rectangle('Position', rect, 'EdgeColor','r');
drawnow;
end
基于MOSSE算法的目标跟踪(matlab).rar
需积分: 50 132 浏览量
2020-05-10
10:06:33
上传
评论 5
收藏 2KB RAR 举报
vivi橙
- 粉丝: 7
- 资源: 5