template matching one to many

2 views (last 30 days)
Glenn
Glenn on 16 Jun 2017
Commented: Glenn on 18 Jun 2017
i want to compare an iris image template on axes1 one by one to a folder filled with iris image templates. the comparation will use hamming distance. my goal is to find a template (from the folder) that has the lowest hamming distance value from the comparation and show it on axes2 as the output template. i have hamming distance code, but its still for comparing between two axes. my question is how to make the codes can do comparation between axes and folder(one to many)? here is the codes:
function [hd]=hammingdist(image1,image2)
%get total pixels in the images.
N=size(image1,1)*size(image1,2);
sum=0;
for i=1:size(image1,1)
for j=1:size(image1,2)
if image1(i,j)==image2(i,j)
sum=sum+0;
else
sum=sum+1;
end
end
end
hd=sum/(2*N);
end

Accepted Answer

Guillaume
Guillaume on 16 Jun 2017
Edited: Guillaume on 16 Jun 2017
I have no idea what it is you call axis. Anyway, I don't see what the problem is with writing a loop a that compare your image to a set of images:
templatefiles = dir(fullfile(yourtemplatefolder, '*.png')); %or whatever the extension fo your images are
hammingdistances = zeros(size(templatefiles));
for fileidx = 1:numel(templatefiles)
templateimage = imread(fullfile(yourtemplatefolder, templatefiles(fileidx).name));
hammingdistances(fileidx) = hammingdist(yourimage, templateimage);
end
A few things about your hammingdist function:
  • It only works on greyscale images (or it only computes the distance between the red channels)
  • It assumes both images are exactly the same size but never checks that it is the case
  • It does not use the ability of matlab to operate directly on matrices. Your whole function can be achieved with just one line of code (three if you add the above checks):
function [hd]=hammingdist(image1,image2)
assert(isequal(size(image1) == size(image2), 'images are not the same size');
assert(ndims(image1) == 2, 'images are not greyscale');
hd = sum(image1(:) ~= image2(:)) / (2 * numel(image1));
end
  • Oh, and don't use sum as a variable name since it prevents you from using the very useful sum function (as used above).
  1 Comment
Glenn
Glenn on 18 Jun 2017
thank you so much, sir. your help mean alot to me. God bless you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!