Image Processing Reference
In-Depth Information
where the symbol denotes the exclusive NOR operator. This equation can be easily
implemented and requires significantly fewer resources than the original matching function.
Template matching develops an accumulator space that stores the match of the template
to the image at different locations, this corresponds to an implementation of Equation 5.7.
It is called an accumulator, since the match is accumulated during application. Essentially,
the accumulator is a two-dimensional array that holds the difference between the template
and the image at different positions. The position in the image gives the same position of
match in the accumulator. Alternatively, Equation 5.11 suggests that the peaks in the
accumulator resulting from template correlation give the location of the template in an
image: the co-ordinates of the point of best match. Accordingly, template correlation and
template matching can be viewed as similar processes. The location of a template can be
determined by either process. The binary implementation of template matching, Equation
5.16, usually is concerned with thresholded edge data. This equation will be reconsidered
in the definition of the Hough transform, the topic of the following section.
The Matlab code to implement template matching is the function TMatching given in
Code 5.1 . This function first clears an accumulator array, accum , then searches the whole
picture, using pointers i and j , and then searches the whole template for matches, using
pointers x and y . Notice that the position of the template is given by its centre. The
accumulator elements are incremented according to Equation 5.7. The accumulator array
%Template Matching Implementation
function accum=TMatching(inputimage,template)
%Image size & template size
[rows,columns]=size(inputimage);
[rowsT,columnsT]=size(template);
%Centre of the template
cx=floor(columnsT/2)+1; cy=floor(rowsT/2)+1;
%Accumulator
accum=zeros(rows,columns);
%Template Position
for i=cx:columns-cx
for j=cy:rows-cy
%Template elements
for x=1-cx:cx-1
for y=1-cy:cy-1
err=(double(inputimage(j+y,i+x))-double(template
(y+cy,x+cx)))^2;
accum(j,i)=accum(j,i)+err;
end
end
end
end
Code 5.1
Implementing template matching
Search WWH ::




Custom Search