Lucas Kanade ALgorithm

8 May

As the name suggests it was proposed by Lucas and Kanade and widely known as  LK tracking algorithm. This algorithm is used for tracking of objects in video as well as for stereo correspondence problem. It can be applied on all those use cases where the following assumptions hold true for a larger amount of time .

This algorithm is based on 3 assumptions

a. Brightness Constancy Equation : It means the pixel in concern follow the following equation  Ex*u + Ey*v = Et. Here, Ex is the derivative of E wrt x and similarly for Ey and Et. While ‘u’ and ‘v’ are velocity in x and y direction respectively wrt frame. Now question arises, what is E. Well E image Irradiance. For further info on what is E just Google it. 😉

b. Spatial Coherence :  It means that object does not break apart in next frame.So all the pixels of the object will have same velocity.

c. Last but most important, for video object tracking the object should move with very small distance in successive frames.

Based above assumptions, we now will derive the whole algorithm.Hey wait, I have cleared initial doubts now derive yourself.For reference go through relevant  paper published.

The following is its implementation.

####################################################################

import cv
import cv2
import numpy

# some constants and default parameters
lk_params = dict(winSize=(15,15),maxLevel=2,criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,0.03))
subpix_params = dict(zeroZone=(-1,-1),winSize=(10,10),criteria = (cv2.TERM_CRITERIA_COUNT | cv2.TERM_CRITERIA_EPS,20,0.03))
feature_params = dict(maxCorners=500,qualityLevel=0.01,minDistance=5)
# setup video capture
cap = cv2.VideoCapture(0)
ret,im = cap.read()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
fourcc = cv.CV_FOURCC(‘I’,’4′,’2′,’0′)

while True:
ret,im = cap.read()
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

features = cv2.goodFeaturesToTrack(gray, **feature_params)
tracks = [[p] for p in features.reshape((-1,2))]

prev_gray = gray
if features != []:
ret,im_new = cap.read()
gray=cv2.cvtColor(im_new,cv2.COLOR_BGR2GRAY)
tmp = numpy.float32(features).reshape(-1, 1, 2)
features,status,track_error = cv2.calcOpticalFlowPyrLK(prev_gray,gray,tmp,None,**lk_params)
features = [p for (st,p) in zip(status,features) if st]
features = numpy.array(features).reshape((-1,2))
for i,f in enumerate(features):
tracks[i].append(f)
ndx = [i for (i,st) in enumerate(status) if not st]
ndx.reverse() #remove from back
for i in ndx:
tracks.pop(i)
prev_gray = gray
for point in features:
cv2.circle(im,(int(point[0]),int(point[1])),3,(0,255,0),-1)

cv2.imshow(‘LKtrack’,im)

cv2.waitKey(1)
#################################################################################

PS: check indentation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: