MATLAB: Image comparison is getting slower in matlab

fasterMATLABsurf

base code : Object Detection in a Cluttered Scene Using Point Feature Matching
— my code —
show = 0;
path_directory='C:\Users\KimJS\Desktop\picture';
boximg=[path_directory '/3.jpg'];
boxImage = imread(boximg);
boxImage = rgb2gray(boxImage);
if(show == 1)
figure;
imshow(boxImage);
title("Image of a Box");
end
fnames=dir('C:\Users\KimJS\Desktop\pic');
fname_char = 'C:\Users\KimJS\Desktop\pic\'
A={'.jpg','.bmp','.png','.tif'};
AVG = 0;
COUNT = 0;
boxPoints = detectSURFFeatures(boxImage);
hold on;
[boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints);
tic
for j = 1:length(fnames)
[pathstr,name,ext] = fileparts(fnames(j).name);
if (strcmpi(ext,'.jpg') || strcmpi(ext, '.png')|| strcmpi(ext, '.JPEG') ==1)
pt = [fname_char fnames(j).name];
sceneImage = imread(pt);
sceneImage = rgb2gray(sceneImage);
hold on;
[sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints);
boxPairs = matchFeatures(boxFeatures, sceneFeatures);
if(length(boxPairs)>80)
toc
fprintf("anser %s", fnames(j).name)
break
end
end
end
I only compared 20 images for one image, but it is too slow.
how to vectorization of my code
I want to finish this in less than a second as soon as possible. I would really appreciate it if you could help me.

Best Answer

If you want to do it in less than one second, then pre-read the files and pre-convert to gray. Possibly even extract the features ahead of time.
Your code uses scenePoints before it is defined, which makes it tricky to figure out whether you are deliberately updating the points as you go along (such as would be the case for tracking.)