Thursday, September 8, 2011

Image Registration and Python OpenCV cvCalcOpticalFlowPyrLK

In an image sequence following a pixel from one frame to next can be performed by OpenCV cvCalcOpticalFlowPyrLK . As an example we prepared two images, we manually shifted objects in it from first to the second, then again manually chose 3 points to follow from one image to the next. The optical flow algorithm uses Lucas-Kanade.
import cv

seq = [cv.LoadImage("flow%d.png" % (i+1), 0) for i in range(2)]
crit = (cv.CV_TERMCRIT_ITER, 100, 0.1)
crit = (cv.CV_TERMCRIT_EPS, 0, 0.001)

pts = [(142,190), (139,110), (160,60)]

color = (100,100,255)

r = cv.CalcOpticalFlowPyrLK(seq[0], seq[1], None, None,
pts, (100,100), 0, crit, 0)
for pt in pts:
cv.Circle(seq[0], pt, 5, color, 0, cv.CV_AA, 0)
a = cv.CreateImage((400,300), 8, 1)
cv.Resize(seq[0], a, cv.CV_INTER_NN)
cv.ShowImage('First', a)
cv.WaitKey()
cv.DestroyAllWindows()

for pt in r[0]:
cv.Circle(seq[1], (int(pt[0]), int(pt[1])), 5, color, 0, cv.CV_AA, 0)
b = cv.CreateImage((400,300), 8, 1)
cv.Resize(seq[1], b, cv.CV_INTER_NN)

cv.ShowImage('Second', b)
cv.WaitKey()
cv.DestroyAllWindows()
Code