Tracking Methods in OpenCV
Localisation and tracking can be achieved very effectively, with robust methods available in Opencv. One of the most common examples is the face detection functionality in the form of cv.HaarDetectObjects(), see example at end of tutorial.
Good Features
Good features are another example of tracking. Certain parts of an image have relational properties that mean they can be tracked from frame to frame. For instance, on a black background, a single white pixel would stand out. It has a higher intensity value than other pixels around it. If it moved in the next image, it would still be apparent, due to the same relationships to it's surrounding pixels.

Adding to this idea.... surfaces can be extracted. Regions or segments that are likely to remain identifiable even undergoing frame by frame changes.
# create the wanted images
eig = cv.CreateImage (cv.GetSize (grey), 32, 1)
temp = cv.CreateImage (cv.GetSize (grey), 32, 1)
# the default parameters
quality = 0.01
min_distance = 10
# search the good points
features = cv.GoodFeaturesToTrack (
grey, eig, temp,
MAX_COUNT,
quality, min_distance, None, 3, 0, 0.04)
for (x,y) in features:
print "Good feature a: "+x+','+y
cv.Circle (image, (x, y), 3, (0, 255, 0), -1, 8, 0)

Template matching can be a fast and effective way of monitoring the position of an object within the image. A template is a copy of an image, usually with smaller dimensions. When both the image and the template are given to the cv.MatchTemplate() method, the template is compared to every region of matching size within the image. This forms the result matrix that contains a value for each x,y location that was compared in the source image. The lowest value, returned by the cv.MinMaxLoc() represents where there was LEAST difference between the image and the template.
cv.SetImageROI(image, (100,100,50,50))
template=cv.CloneIamge(image)
cv.ShowImage("template",template)

cv.ResetImageROI(image)
W,H=cv.GetSize(image)
w,h=cv.GetSize(template)
width=W-w+1
height=H-h+1
result=cv.CreateImage((width,height),32,1)
cv.MatchTemplate(frame,template, result,cv.CV_TM_SQDIFF)
(min_x,max_y,minloc,maxloc)=cv.MinMaxLoc(result)
(x,y)=minloc
cv.Rectangle(image2,(int(x),int(y)),(int(x)+w,int(y)+h),(255,255,255),1,0)

It is recommended that the function is called with methods other than cv.CV_TM_SQDIFF, as this will provided a larger basis on where to place the template within the image.
Haar like features area series of classifiers that are trained against positive and negative samples. An xml file is formed which holds of the information of classifier. Opencv comes with an assortment of classifers, but it is also possible to train your own.
#Load the haar cascade
hc = cv.Load("haarcascade_frontalface_alt.xml")
#Detect face in image
face = cv.HaarDetectObjects(frame, hc, cv.CreateMemStorage(), 1.2,2, cv.CV_HAAR_DO_CANNY_PRUNING, (0,0) )
for (x,y,w,h) in face:
print 'face found at: '+str(w)+','+str(h)
Click here for the next part of the tutorial

