changeset 100:2a3cafcf5faf

added function to compute the translation between two images
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 12 Jul 2011 19:59:25 -0400
parents e7dc5a780f09
children 5cc30ba3a933
files python/cvutils.py
diffstat 1 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Tue Jul 12 17:01:59 2011 -0400
+++ b/python/cvutils.py	Tue Jul 12 19:59:25 2011 -0400
@@ -96,6 +96,32 @@
     invH /= invH[2,2]
     return invH
 
+def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)):
+    '''Computes the translation between of img2 with respect to img1
+    (loaded using OpenCV)
+    img1Points are used to compute the translation
+
+    TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)'''
+    from numpy.core.multiarray import zeros
+    from numpy.lib.function_base import median
+
+    (img2Points, status, track_error) = cv.CalcOpticalFlowPyrLK(img1, img2, zeros((img1.rows,img1.cols+8)), zeros((img1.rows,img1.cols+8)), img1Points, windowSize, level, criteria, 0)
+
+    deltaX = []
+    deltaY = []
+    for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)):
+        if status[k] == 1:
+            dx = p2[0]-p1[0]
+            dy = p2[1]-p1[1]
+            d = dx**2 + dy**2
+            if d < maxTranslation:
+                deltaX.append(dx)
+                deltaY.append(dy)
+    if len(deltaX) >= 10:
+        return [median(deltaX), median(deltaY)]
+    else:
+        return None
+
 class FourWayIntersection:
     '''Simple class for simple intersection outline'''
     def __init__(self, dimension, coordX, coordY):