close all
BB = importdata('./MotorcycleChase/groundtruth_rect.txt');
Idir = dir('./MotorcycleChase/img/*.jpg');
filename = horzcat(Idir(1).folder,'/',Idir(1).name);
I = imread(filename);
imshow(I);
moto = imcrop(I,BB(1,2:5));
imshow(moto);
im_obj = rgb2gray(moto);
kp_obj = detectSURFFeatures(im_obj);
kp_obj = selectStrongest(kp_obj,50);
[feat_obj,kp_obj] = extractFeatures(im_obj,kp_obj);
nf = size(Idir);
for i = 2:nf
filename = horzcat(Idir(i).folder,'/',Idir(i).name);
im_esc = rgb2gray(imread(filename));
kp_esc = detectSURFFeatures(im_esc);
kp_esc = selectStrongest(kp_esc,50);
[feat_esc,kp_esc] = extractFeatures(im_esc,kp_esc);
pairs = matchFeatures(feat_obj, feat_esc, 'MatchThreshold', 3);
m_kp_obj = kp_obj(pairs(:, 1), :);
m_kp_esc = kp_esc(pairs(:, 2), :);
if size(m_kp_obj, 1) >= 3 && size(m_kp_esc, 1) >= 3
T = estimateGeometricTransform2D(m_kp_obj, m_kp_esc, "affine");
[f, c] = size(im_obj);
box = [1, 1; c, 1; c, f; 1, f; 1, 1];
nbox = transformPointsForward(T, box);
nbox = sortrows(nbox, [2 1]);
minCoord = min(nbox);
maxCoord = max(nbox);
nbox2 = [minCoord(1), minCoord(2), maxCoord(1)-minCoord(1), maxCoord(2)-minCoord(2)];
overlapRatio = bboxOverlapRatio(nbox2, BB(i, 2:5));
imshow(im_esc);
rectangle('Position', nbox2, 'EdgeColor', 'blue');
drawnow
else
end
end