Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 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 | abfc54c097d4 |
comparison
equal
deleted
inserted
replaced
| 99:e7dc5a780f09 | 100:2a3cafcf5faf |
|---|---|
| 94 from numpy.linalg.linalg import inv | 94 from numpy.linalg.linalg import inv |
| 95 invH = inv(homography) | 95 invH = inv(homography) |
| 96 invH /= invH[2,2] | 96 invH /= invH[2,2] |
| 97 return invH | 97 return invH |
| 98 | 98 |
| 99 def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)): | |
| 100 '''Computes the translation between of img2 with respect to img1 | |
| 101 (loaded using OpenCV) | |
| 102 img1Points are used to compute the translation | |
| 103 | |
| 104 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)''' | |
| 105 from numpy.core.multiarray import zeros | |
| 106 from numpy.lib.function_base import median | |
| 107 | |
| 108 (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) | |
| 109 | |
| 110 deltaX = [] | |
| 111 deltaY = [] | |
| 112 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)): | |
| 113 if status[k] == 1: | |
| 114 dx = p2[0]-p1[0] | |
| 115 dy = p2[1]-p1[1] | |
| 116 d = dx**2 + dy**2 | |
| 117 if d < maxTranslation: | |
| 118 deltaX.append(dx) | |
| 119 deltaY.append(dy) | |
| 120 if len(deltaX) >= 10: | |
| 121 return [median(deltaX), median(deltaY)] | |
| 122 else: | |
| 123 return None | |
| 124 | |
| 99 class FourWayIntersection: | 125 class FourWayIntersection: |
| 100 '''Simple class for simple intersection outline''' | 126 '''Simple class for simple intersection outline''' |
| 101 def __init__(self, dimension, coordX, coordY): | 127 def __init__(self, dimension, coordX, coordY): |
| 102 self.dimension = dimension | 128 self.dimension = dimension |
| 103 self.coordX = coordX | 129 self.coordX = coordX |
