comparison python/cvutils.py @ 302:9d88a4d97473

corrected bug in compute-homography
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 18 Mar 2013 23:37:45 -0400
parents aba9711b3149
children 514f6b98cd8c
comparison
equal deleted inserted replaced
301:27f06d28036d 302:9d88a4d97473
57 from numpy.lib.npyio import loadtxt 57 from numpy.lib.npyio import loadtxt
58 from numpy import float32 58 from numpy import float32
59 points = loadtxt(filename, dtype=float32) 59 points = loadtxt(filename, dtype=float32)
60 return (points[:2,:].T, points[2:,:].T) # (world points, image points) 60 return (points[:2,:].T, points[2:,:].T) # (world points, image points)
61 61
62 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0):
63 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)'''
64 #cvSrcPoints = arrayToCvMat(srcPoints);
65 #cvDstPoints = arrayToCvMat(dstPoints);
66 #H = cv.CreateMat(3, 3, cv.CV_64FC1)
67 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
68 return H
69
70 def cvMatToArray(cvmat): 62 def cvMatToArray(cvmat):
71 '''Converts an OpenCV CvMat to numpy array.''' 63 '''Converts an OpenCV CvMat to numpy array.'''
72 from numpy.core.multiarray import zeros 64 from numpy.core.multiarray import zeros
73 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) 65 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height)
74 for i in xrange(cvmat.rows): 66 for i in xrange(cvmat.rows):
75 for j in xrange(cvmat.cols): 67 for j in xrange(cvmat.cols):
76 a[i,j] = cvmat[i,j] 68 a[i,j] = cvmat[i,j]
77 return a 69 return a
78 70
79 if opencvExists: 71 if opencvExists:
72 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0):
73 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)'''
74 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
75 return H
76
80 def arrayToCvMat(a, t = cv2.cv.CV_64FC1): 77 def arrayToCvMat(a, t = cv2.cv.CV_64FC1):
81 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' 78 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.'''
82 cvmat = cv2.cv.CreateMat(a.shape[0], a.shape[1], t) 79 cvmat = cv2.cv.CreateMat(a.shape[0], a.shape[1], t)
83 for i in range(cvmat.rows): 80 for i in range(cvmat.rows):
84 for j in range(cvmat.cols): 81 for j in range(cvmat.cols):