changeset 112:67555e968b5e

added tests if OpenCV python libraries are not available
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 18 Jul 2011 14:28:40 -0400
parents 48e3de4acb65
children 606010d1d9a4
files python/cvutils.py
diffstat 1 files changed, 38 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Fri Jul 15 19:51:31 2011 -0400
+++ b/python/cvutils.py	Mon Jul 18 14:28:40 2011 -0400
@@ -2,7 +2,12 @@
 '''Image/Video utilities'''
 
 import Image, ImageDraw # PIL
-import cv
+try:
+    import cv
+    opencvExists = True
+except ImportError:
+    print('OpenCV library could not be loaded')
+    opencvExists = False
 from sys import stdout
 
 #import aggdraw # agg on top of PIL (antialiased drawing)
@@ -43,13 +48,14 @@
             a[i,j] = cvmat[i,j]
     return a
 
-def arrayToCvMat(a, t = cv.CV_64FC1):
-    '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.'''
-    cvmat = cv.CreateMat(a.shape[0], a.shape[1], t)
-    for i in range(cvmat.rows):
-        for j in range(cvmat.cols):
-            cvmat[i,j] = a[i,j]
-    return cvmat
+if opencvExists:
+    def arrayToCvMat(a, t = cv.CV_64FC1):
+        '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.'''
+        cvmat = cv.CreateMat(a.shape[0], a.shape[1], t)
+        for i in range(cvmat.rows):
+            for j in range(cvmat.cols):
+                cvmat[i,j] = a[i,j]
+        return cvmat
 
 def printCvMat(cvmat, out = stdout):
     '''Prints the cvmat to out'''
@@ -95,31 +101,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
+if opencvExists:
+    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)
+        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
 
-    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
+        (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'''