Template Matching Using Fast Normalized Cross Correlation
Total Page:16
File Type:pdf, Size:1020Kb
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