Fig. 1. An ideal contour around an object of interest.
after that uses a set of markers in a mask to group the small
regions into areas with similar properties, which are computed
from each marker. The watershed method is computed in
openCV using the function:
cvWatershed(src, markers);
Again, src is the input image, markers is an image with the
same size as src where the regions are marked, so the method
can group smaller regions and segment the image properly.
III. I
MAGE MATCHING
Pattern recognition is an area of computer vision where one
tries to find if there is a known pattern in a given image.
The process that checks in the image for a pattern’s possible
location is called image matching. Matching techniques are
the simplest way to do pattern recognition. In this section it
will present two different approaches for matching: contour
matching and template matching.
A. Contour Matching
After segmentation an image is composed by a set of
groups of pixels where each group represents a region. This
segmented data can be transformed into a compact way that
facilitates the region’s description and help to compare and
match with a given pattern [8]. When thinking about a contour
one considers an object of interest and a line surrounding it as
an ideal contour (althought not necessarily a closed line), as
shown in Figure 1. A contour can be represented in different
ways, two of the most common ways to represent a contour
are polygons and Freeman chain codes [1].
A boundary can be represented by a connected sequence
of straight line segments, each with a specified length and
direction. This type of representation is called a Freeman chain
code, it uses a 4- or 8-connectivity and the segment’s direction
is coded following a numbering code. Figure 2 presents these
two methods, on top the Freeman chain code with the green
marker to set the starting point and the numbering code at the
right. On bottom the polygon given by the set of points at the
corners of the polygon.
In openCV contours are usually computed from a binary
image where it is easier to define contrast. The function used
to compute the contours is:
int cvFindContours(src,sto,first,header,mode,
Fig. 2. Contour representation: on top the Freeman chain code and on bottom
a set of points for a polygon.
method);
The input image, src, is a binary image, sto is a mem-
ory location where the contours will be written first is the
first contours in a sequence, header gives the size of the
object being retrieved and can be either sizeof(CvContour) or
sizeof(CvChain), depending on the method, mode defines the
data structure used to store the contours and can be:
• CV RETR EXTERNAL: retrieves only the most external
contour.
• CV RETR LIST: retrieves all contours as a list where
the first element is most internal contour.
• CV RETR CCOMP: retrieves all contours and organize
them as a two level hierarchy, one level with external
contours and another with internal contours.
• CV RETR TREE: retrieves all contours and organize
them as a tree structure with the outer contour as a root.
The method parameter is related to the way openCV stores
the contour in memory. There are five ways to do this task:
• CV CHAIN CODE: uses the Freeman chain code.
• CV CHAIN APPROX NONE: uses the points deter-
mined by the Freeman chain code.
• CV CHAIN APPROX SIMPLE: compresses the Free-
man chain code and returns the ending points.
• CV CHAIN APPROX TC89 L1 or
CV
CHAIN APPROX TC89 KCOS: uses a special
chain approximation algorithm.
• CV LINK RUNS: can only be used with
CV
RETR LIST and represents contours as links
horizontal segments.
The returning value of the method is the total number of
contours found. One important step before calling cvFindCon-
tours is the input image binarization. This process requires
some filtering afterwards to clean the image, otherwise too
many contours might be found. Figure 3 shows a simple and
clean binary image (on the left side) and the contours found
(on the right) there are a total of 7 contours on the right
image. Figure 4 shows the same process applied to a complex
(meaning real) image, the contour structure, in this case, has
13