The conventional approach to performing pattern detection utilized cross correlation. Cross correlation is identical to convolution, except that one of the sequences (images) is not flipped left to right and up and down. Because of this property, cross correlation accurately indicates the existence and location of a template in an image. The formula for the cross-correlation is as follows:
"i" is a matrix of ones with size equal to the size of the template. By dividing out this "magnitude," the resulting correlation has been normalized, and the problem mentioned above is eliminated.
Although the cross-correlation function accurately detects a pattern matching the template in the image, there are faster methods for detection.
function CORRloc
clear
close all
tmpl = input('Template to find? ','s');
ref = input('Reference? ','s');
threshold = input('Threshold? ');
tic
flops(0)
[templ,map] = gifread(tmpl);
[refer,map] = gifread(ref);
refer=refer-1;
templ=templ-1;
raw = xcorr2(refer,templ);
refersqr = refer.^2;
flat = ones(size(templ));
norml = xcorr2(refersqr,flat);
rtnormal=norml.^0.5;
rtnormal=rtnormal+0.1;
normalized = raw./rtnormal;
most=max(max(normalized));
location = (normalized>=most-threshold)+1;
colormap(gray(2));
image(location);
flops
toc
function CNVloc
clear
close all
tmpl = input('Template to find? ','s');
ref = input('Reference? ','s');
threshold = input('Threshold? ');
tic
flops(0)
[templ,map] = gifread(tmpl);
[refer,map] = gifread(ref);
refer=refer-1;
templ=flipud(fliplr(templ-1));
raw = conv2(refer,templ,'same');
refersqr = refer.^2;
flat = ones(size(templ));
norml = conv2(refersqr,flat,'same');
rtnormal=norml.^0.5;
rtnormal=rtnormal+0.1;
normalized = raw./rtnormal;
most=max(max(normalized));
location = (normalized>=most-threshold)+1;
colormap(gray(2));
image(location);
flops toc