clear; clc;

ObjectImage = imread('input/object.png');
SceneImage = imread('input/scene.png');

figure(1);
subplot(121); imshow(ObjectImage); title('Object');
subplot(122); imshow(SceneImage); title('Scene');

ObjectPoints = detect_features(ObjectImage, 'SURF');
ScenePoints  = detect_features(SceneImage,  'SURF');

figure(2);
subplot(121); imshow(ObjectImage); title('Object with all candidate points'); hold on; plot(ObjectPoints);
subplot(122); imshow(SceneImage); title('Scene with all candidate points'); hold on; plot(ScenePoints);

ObjectPoints = select_strongest_features(ObjectPoints, 100, 0.000001);
ScenePoints  = select_strongest_features(ScenePoints,  200, 0.000001);

figure(3);
subplot(121); imshow(ObjectImage); title('Object and the selected points only'); hold on; plot(ObjectPoints);
subplot(122); imshow(SceneImage); title('Scene and the selected points only'); hold on; plot(ScenePoints);

[ObjectFeatures, ObjectPoints] = extractFeatures(ObjectImage, ObjectPoints);
[SceneFeatures, ScenePoints]   = extractFeatures(SceneImage, ScenePoints);

boxPairs = matchFeatures(ObjectFeatures, SceneFeatures);

matchedObjectPoints = ObjectPoints(boxPairs(:, 1), :);
matchedScenePoints  = ScenePoints( boxPairs(:, 2), :);

figure(4);
showMatchedFeatures(ObjectImage, SceneImage, matchedObjectPoints, matchedScenePoints, 'montage');
title('Match points');