Template Matching using Fast Normalized Cross Correlation

Kai Briechle and Uwe D Haneb eck

Institute of Automatic Control Engineering Technische Universitat Munc hen

Munc hen Germany

ABSTRACT

In this pap er we present an algorithm for fast calculation of the normalized cross correlation NCC and its applica

tion to the problem of template matching Given a template t whose p osition is to b e determined in an image f the

basic idea of the algorithm is to represent the template for which the normalized cross correlation is calculated as

a sum of rectangular basis functions Then the correlation is calculated for each basis function instead of the whole

template Theresultofthe correlation of the template t and the image f is obtained as the weighted sum of the

correlation functions of the basis functions

Dep ending on the approximation the algorithm can by far outp erform Fouriertransform based implementations

of the normalized cross correlation algorithm and it is esp ecially suited to problems where many dierent templates

are to b e found in the same image f

Keywords Normalized cross correlation image pro cessing template matching basis functions

INTRODUCTION

A basic problem that often o ccurs image pro cessing is to determine the p osition of a given pattern in an image or

part of an image the socalled region of interest This problem is closely related to the determination of a received

digital signal in signal pro cessing using eg a matched lter

Two basic cases can b e dierentiated

The p osition of the pattern is unknown

estimate for the p osition of the pattern is given An

Usually b oth cases have to b e treated to solve the problem of determining the p osition of a given pattern in an

image In the latter case the information ab out the p osition of the pattern can b e used to reduce the computational



eort signicantly It is also known as feature tracking in a sequence of images

For b oth feature tracking and the initial estimation of the p osition of the given pattern a lot of dierent well



known algorithms have b een developped One basic approach that can b e used in b oth cases mentioned ab ove

is template matching This means that the p osition of the given pattern is determined by a pixelwise comparision

of the image with a given template that contains the desired pattern For this the template is shifted u discrete

stepsinthex direction and v steps in the y direction of the image and then the comparison is calculated over the

template area for each p osition u v To calculate this comparison normalized cross correlation is a reasonable



Nevertheless it is computationally exp ensive and therefore a fast correlation algorithm that choice in many cases

requires less calculations than the basic version is of interest

In section the problem treated in this pap er is dened and a brief summary of the normalized cross correlation

in tro duces a new fast algorithm that computes the normalized cross correlation algorithm is given Section in

an ecient manner for an appro ximation of the template The p erformance of the new algorithm is compared to

standard naive implementation of the normalized cross correlation and to the wellknown Fouriertransform Section

briey describ es how the algorithm can b e applied recursively In section an example is presented in whichthe

prop osed algorithm is applied for template matching Finally an outlo ok to future research activities is presented

K Briechle Email kaibriechleeitumde

U D Haneb eck Email uwehanebeckeitumde

Page 1/8

NCCALGORITHM

The problem treated in this pap er is to determine the position of a given pattern in a two dimensional image f

Let f x y denote the intensityvalue of the image f of the size M M at the p ointx y x fM g

x y x

y fM g The pattern is represented by a given template t of the size N N A common way to

y x y

alue calculate the p osition u v of the pattern in the image f is to evaluate the normalized cross correlation v

pos pos

eachpointu v forf and the template twhich has b een shifted by u stepsinthex direction and by v steps at

in the y direction Equation gives a basic dention for the normalized cross correlation co ecient

P

f x y f tx u y v t

uv

xy

q

P P

f x y f tx u y v t

uv

xy xy

In f denotes the mean value of f x y within the area of the template t shifted to u v which is calculated

uv

by

v N 

y uN 

x

X X

f x y f

uv

N N

x y

xu y v

With similar notation t is the mean value of the template t The denominator in is the variance of the zero

mean image function f x y f and the shifted zero mean template function tx u y v t Due to this

uv

normalization u v is indep endenttochanges in brightness or contrast of the image which are related to the mean

value and the standard deviation

The desired p osition u v of the pattern which is represented by t is equivalent to the p osition u v

pos pos max max

of the maximum value of u v Due to the normalization the use of for the calculation of the p osition

max

of the pattern is more robust than other similarity measures like simple covariance or the sum of the absolute

dierences SAD Nevertheless the main drawback is that the calculation of is computationally exp ensive For

the denominator which normalizes the cross correlation co ecient at every p oint u v u fM N g

x x

u v is determined the energy of the zero mean image v fM N g of the image at which

y y

v N 

uN  y

x

X X

f x y f e u v

uv f

xu y v

and the mean of the image within the area of the template function f have to b e recalculated If this calculation

uv

is implemented in a straightforward naive way according to the number of calculations is prop ortional to

N N M N M N though the energy of the zero mean template function

x y x x y y

N

y N

x

X X

tx y t e u v

t

 y  x

e to be precalculated only once This computational eort is not and the mean of the template function t hav

acceptable for most practical applications The nominator in can be calculated in the frequency range using

the wellknown Fouriertransform yet the numb er of calculations is still comparativly high and an algorithm that

calculates the normalized cross correlation with less calculations is of great interest

To overcome these complexity problems an ecient metho d to calculate the denominator of the normalized



sumtables containing the cross correlation co ecient is prop osed by Lewis The main idea is to precalculate

integral ov er the image function f x y and the squared image function f x y running sum once for each image

f and use these tables for ecient calculation of the expression f x y f at eachpointu v at whichthe

uv

normalized cross correlation co ecientisevaluated Using these sumtables the resulting numb er of calculations for

the denominator do es no longer dep end on the size of the template N N but only on the size of the image function

x y

M M A brief description of this calculation is given in section

x y

In section the key idea that allows a very ecient calculation of the numerator of is explained in detail

Thus the normalized cross correlation co ecient can b e calculated for an approximated template function tx y

with an order of magnitude less calculations than the standard FFT approach which op ens up many new applications

Page 2/8

FAST NCCALGORITHM

Calculation of the denominator

To simplify the calculation of the denominator of the normalized cross correlation co ecient the key idea is to use



two sum tables su v ands u v over the image function f x y and the image energy f x y The sum table

of the image function is recursively dened by

su v f u v su vsu v su v

A similar recursive denition for the sum table over the image energy is given by

s u v s u v f u v s u vs u v

u v when either u v The following algorithm for simplied calculation of the denominator v s with su

can b e applied to the whole image function or to any subimage of the image function f x y Then the sum tables

have to calculated only for this subimage region With these tables can b e calculated in a very ecient manner

indep endent of the size N N of the template

x y

v N 

uN  y

x

X X

s u v N v f x y su N v N su v N su

x x y y

xu y v

It can b e seen from that only three additions subtractions are necessary to evaluate the double sum over f x y

byevaluation of the sumtable su v

The denominator of is then evaluated using the running sum tables and

X X X X X X X X

f f x y f f x y f f x y

uv uv

uv

x y x y x y x y

P P

In and all equations in the remainder of this paragraph the double sum is evalutated over the region of

x y

N With the template which means uxu N and vyv

y x

X X X X

f x y f N N

x y

uv

N N

x y

x y x y

can b e simplied to

X X X X X X

f x y f x y f f x y

uv

N N

x y

x y x y x y

The sum expressions in over f x y andf x y can eciently b e calculated using the running sum tables



Moreover one square ro ot has to calculated for each p ointu v to determine the denominator of

Calculation of the numerator

Application of the algorithm presented in the last subsection allows ecient calculation of the denominator but the

numb er of computations required to calculate the numerator of the NCCco ecient is still comparatively high

even if it is done in the frequency range with an FFT algorithm Therefore further simplication of this calculation

is required The numerator can b e rewritten as

X X X X

N u v f x y t x u y v f t x u y v

uv

x y x y

t x u y y v is the zero mean template function dened b where

t x u y v tx u y v t

Page 3/8 basis function

1

0.8

0.6

0.4

0.2

0 20 N x 15 20 15 N 10 y 10 5 5

0 0

Figure A single rectangular basis function t x y

i

P P

As t x y has zero mean and thus also zero sum the term f t x u y v is zero as well and the

uv

x y

numerator of can written as

X X

N u v f x y t x u y v

x y

The basic idea to simplify the calculation of the numerator is to expand the zero mean template function t x y to

the weighted sum of K rectangular basis functions t yielding an approximation tx y of the template function

i

K

X

y k t x tx y

i i

i

ecient can then b e eciently evaluated with this approxima The numerator of the normalized cross correlation co

i i i i

tion The basis function t x y is constant equal inside an rectangular area x x x  y y y and zero

i

u u

l l

i i

otherwise Thus x and x are the indices of the lower and the upp er b ound of the rectangular area that is constant

u

l

i i

equal in the xdirection y and y are the resp ective indices in the ydirection Figure shows an example for a

u

l

rectangular basis function t x y where N N It is constant equal one for x  y The

i x y

oth on the choice y of the original zero mean template function t x y dep ends b ximation tx quality of the appro

i i i i

of the basis functions that means on their lower and upp er b ounds x x and y y and on the number of basis

u u

l l

functions that are used

Using the sum expansion of the template function tx y allows to calculate an approximation for the numerator

of the cross correlation co ecient

i i

x u y v

K

u u

X X X

N u v k f x y

i

i i

i

xx u y y v

l l

i i

r that x and y t for basis function i Remembe where K is the number of basis functions and k is the co ecien

i

l l

i i

denote the indices of the lower limits of basic function i and x and y subsequently are the indices of the upp er

u u

limits is obtained from using b ecause the basis functions t x y are either constant one or zero With

i

the running sum table over the image function that has already b een calculated to simplify the determination of

the denominator which cannot b e calculated in the frequency range the inner double sum in can b e calculated

Page 4/8

with only additionsubtraction op erations

i i

x y

u u

X X

i i i i i i i i

sx y y sx y sx y f x y sx

l u l l l u u u

i i

xx y y

l l

50 50

100 100 150 Maximum Template 150 200

200 250

250 300

350 300

400 350

450 400

100 200 300 400 500 600 50 100 150 200 250 300 350 400 450 500 550

a Image function f x y b Cross correlation matrix u v

180

190 100

200 80

60

210 40

20 220

0 180 250 230 190 260 200 270 210 280 220 290 240 230 300 240

250 260 270 280 290 300

d Surface plot of tx y

c Template function tx y

180

190 50

200

0 210

220

−50 180 250 230 190 260 200 270 210 280 220 290 240 230 300 240

250 260 270 280 290 300

f Surface plot of tx y

e Sum Expansion tx y

Figure Exp eriment Template matching with fast normalized cross correlation

This means that the number of calculations required to determine an approximation for the numerator in

dep ends only on the number of rectangular basis functions used but not on their size The approximated cross

Page 5/8

correlation co ecient u v is

N u v

q

u v

P P

f x y f tx u y v t

uv

xy xy

whose denominator is equivalent to and can b e eciently calculated using the sumtables Substituting

and nally yields the following compact formula for the approximated cross correlation co ecient

P

K

i i i i i i i i

u y v k sx u y v sx u y v sx u y v sx

i

u u u u

l l l l

i

q

u v

P P

f x y f tx u y v t

uv

xy xy

Analysis of complexity for the calculation of the numerator

The number of computations required to calculate the numerator of the NCC co ecient for an image of the size

M M and a template of the size N N that has b een approximated with K rectangular basis functions is given

x y x y

in table It can b e seen that the number of multiplications dep ends linearly on the number of basis functions K

the normalized cross correlation co ecien t u v at which for a given image function f x y For eachpointu v

is evaluated K multiplications are required to calculate the numerator one for each basis function

In contrast to this a direct evaluation of u v requires N N multiplications at eachpointu v atwhich

x y



is evaluated The complexity of the FFT dep ends on the size of the template and the image function When M is

much larger than N it mayeven exceed the number of computations required by the direct metho d Indep endentof

M and N it can also by far exceed the number of computations required by the prop osed algorithm dep ending on

the numb er of basis functions K

The numb er of computations required to set up the sumtables and evaluate the denominator of the normalized

cross correlation co ecient is not regarded in table and table To calculate the denominator that normalizes



the cross correlation co ecient the metho d prop osed by Lewis is far more ecient than a naive direct calculation

ds dicussed here and are not three metho Thus the computations to set up the sumtables are necessary for all

included in the comparison Of course the prop osed algorithm for calculation of the nominator can also b e applied

to calculate eciently an ordinary cross correlation without normalization but then the extra eort to set up the

sum tables has to b e taken into account compared to the standard FFT that do es not require any sumtables

Add Sub Mult

Direct calc N N M  N M  N N N M  N M  N

x y x x y y x y x x y y

FFT M M log M M M M log M M

x y x y x y x y

New alg K  M  N M  N K M  N M  N

x x y y x x y y

Table Analysis of Complexitynumerator only

M M

x y

Add Sub Mult

N N K

x y

Direct calc Mio Mio

FFT Mio Mio

New alg Mio Mio

Table Analysis of Complexity Example

Determination of basis functions

To apply the prop osed algorithm for ecient calculation of the numerator of a set of basis functions t has to

i

be determined to approximate the original template function For simple template functions tx y a set of basis

Page 6/8

functions can b e determined manuallyasshown in section For automatic determination of the basis functions

the quadratic criterion

X

J t x y tx y

xy

is used to assess the quality of the approximation A recursive algorithm divides the template function tx y into

rectangular basis functions It starts with a single basis function t x y t constant and calculates J using



and If J J where J is a predened threshold the basis function is devided into two basis functions

max max

and the co ecients k i are recalculated under the condition that J is minimized This pro cess is continued

i

recursively for each basis function t until JJ Note however that the approximation found by this algorithm

i max

to approximate the template This of basis functions required is not globally optimal with regard to the number

e the same qualitiy J but uses less than K basis functions t Many means that a dierent approximation mighthav

i

features encountered in an indo or environment can nevertheless b e well approximated with a few basis functions by

means of the recursive algorithm The numb er of computations required to calculate the approximated numerator

of is directly prop ortional to the numb er of basis functions K that are used for the approximation Therefore a

small number of wellchosen basis functions is desirable On the other hand using to few basis functions results in a

bad approximation of the template and thus of the numerator Automatic determination of the basis functions

allows to determine a tradeo b etween complexity of the calculation and the approximation error

RECURSIVE APPLICATION OF THE ALGORITHM

As pointed out in the last section the qualitiy of the approximation of the template directly eects the result of

the prop osed algorithm the approximated numerator of The results of the direct calculation and the prop osed

functions are used to represent the template Nevertheless this is not N basis algorithm are the same if K N

y x

y of the prop osed algorithm converges to that of a direct calculation of practical as the computational complexit

u v and thus exceeds the complexity of the FFT based calculation

As long as the approximation of the template function is suciently go o d the pattern represented by tx y is

robustly detected using the maximum value of u v withvery little computational eort as shown in the expample

in chapter If the approximation of the template tx y calculated by the algorithm prop osed in subsection is

not go o d enough to yield a suciently go o d approximation of the normalized cross correlation co ecient u v the

prop osed algorithm is applied recursively The denition of a suciently go o d approximation dep ends on the given

problem In template matching applications it is necessary to determine the p osition of the template in an image

by searching the maximum of u v In this context a go o d approximation means that the maximum of u v is

equal or close to the maximum of u v

application of the prop ecient u v is calculated with or a recursive osed algorithm the cross correlation co F

approximations of the template function tx y that use an increasing number of basis functions in each step of the

recursion In the rst step u v is calculated with avery rough approximation of tx y using only a few basis

functions The maximum error in u v obtained by this approximation can be estimated This error b ound is

then used to determine a subset f x y of all pixels of the image function f x y For this subset the prop osed

algorithm is applied recursively with a b etter approximation of tx y yielding a smaller subset f x y The pro cess



x y issuciently small The correct maximum value of stops when the numb er of pixels in the subset f x y off

N

u v can then b e found bydirect evaluation of on subset f x y which is equivalent to using a representation

N

with the maximum numb er of basis functions K N N and the prop osed algorithm

x y

EXAMPLE

Figure a shows a normal camera image taken from a typical indo or environment The handle of the do or is the

pattern to be found within this image The template function tx y of the pattern which has the size 

is displayed magnied in Fig c This template can well b e approximated by the weighted sum of rectangular

would normally be basis functions which yields a new template function tx y Fig e The basis functions

with the prop osed recursiv e algorithm but this would lead to aworse approximation using more basis calculated

function Therefore for this example the three basis functions t were selected manually to demonstrate how the

i

cross correlation algorithm works A similar but less obvious result can b e obtained with aworse approximation

that is automatically computed Note that the approximated template function tx y has zero mean Figure d

Page 7/8

and e show a surface plot of the original and the approximated template function tx y and tx y The heightof

the plot corresp onds to the value of the function tx y and tx y at the p ointx y

Note however that the values and of tx y are not equal to the co ecients k of the K basis

i

functions b ecause the basis functions t x y overlap in this example This means that the co ecient of the smallest

i

rectangle is calculated taking into accountthe two outer rectangles as the actual value of tx y in the area of the

smallest rectangle is equal to the weighted sum of all three basis functions

In Fig b the resulting cross correlation function u v of the NCC computed with the algorithm

corresp ond to high values of that are close to one and light pixels to low values close to is given Dark pixels

the rough approximation of the template function Fig e the fast NCCalgorithm determines the Despite

osition of the template in the original image correctly yielding the maximum value of u v atx y p

Eciency

For the example that uses a VGAcamera image the size of the image function is pixel and

pixels for the template function Table shows that the number of multiplications for the numerator is reduced

times compared to the FFT and times compared to a direct calculation assuming that the sum tables used

up to basis functions may for calculating the denominator are required for each algorithm This means that

efore the computational load is equivalent to the FFT algorithm It is assumed that the FFT algorithm b e used b

requires that f and t b e extended with zeros to a common p ower of two zero padding

CONCLUSIONS

A new fast algorithm for the computation of the normalized cross correlation has been derived that uses a sum

expansion of the given template function t and rectangular basis functions t x y The number of calculations

i

required to evaluate the normalized cross correlation co ecient u v for the image function f x y dep ends linearly

on the number of basis functions used but not on the size of the template t It has been demonstrated that the

handle can be determined in a VGA camera image with a times less p osition of a simple feature like a do or

compared to an ev u v that uses the FFT This makes the prop osed algorithm attractive aluation of multiplications

for real time image pro cessing applications like feature tracking

ACKNOWLEDGMENTS

This research has b een sp onsored bytheBavarian ResearchFoundation BFS as part of the pro ject DIROKOL

REFERENCES

Gregory D Hager and Kentaro Toyama The XVision System A GeneralPurpose Substrate for Portable Real

Time Vision Applications and Image Understanding Volume Numb er pp

B D Lucas and T Kanade An Iterative Technique with an Application to Stereo Vision

IJCAI

Hiro chika Inoue TetsuyaTachikawa and Masayuki Inaba Robot Vision System with a Correlation Chip for Real

ation Pro ceedings of the IEEE International Conference on racking Optical Flow and Depth Map Gener time T

Rob otics and Automation pp France

Changming Sun AFast Stereo Matching Method Digital Image Computing Techniques and Applications pp

Massey University Auckland New Zealand December

J P Lewis Fast NormalizedCrossCorrelation Industrial Light and Magic

Rafael C Gonzales Paul Wintz Second Edition AddisonWesley Publishing Com

pany Reading Massachusetts November ISBN

Page 8/8