Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 402:f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 29 Jul 2013 19:45:43 -0400 |
| parents | 3399bd48cb40 |
| children | 37c7b46f6e21 |
comparison
equal
deleted
inserted
replaced
| 401:b829ebdc18e6 | 402:f29204e68aab |
|---|---|
| 77 for j in xrange(cvmat.cols): | 77 for j in xrange(cvmat.cols): |
| 78 a[i,j] = cvmat[i,j] | 78 a[i,j] = cvmat[i,j] |
| 79 return a | 79 return a |
| 80 | 80 |
| 81 if opencvAvailable: | 81 if opencvAvailable: |
| 82 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): | 82 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=3.0): |
| 83 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' | 83 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' |
| 84 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) | 84 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) |
| 85 return H | 85 return H |
| 86 | 86 |
| 87 def arrayToCvMat(a, t = cv2.cv.CV_64FC1): | 87 def arrayToCvMat(a, t = cv2.cv.CV_64FC1): |
| 160 imgNum +=1 | 160 imgNum +=1 |
| 161 capture.release() | 161 capture.release() |
| 162 else: | 162 else: |
| 163 print('Video capture failed') | 163 print('Video capture failed') |
| 164 return images | 164 return images |
| 165 | |
| 166 | 165 |
| 167 def getFPS(videoFilename): | 166 def getFPS(videoFilename): |
| 168 capture = cv2.VideoCapture(videoFilename) | 167 capture = cv2.VideoCapture(videoFilename) |
| 169 if capture.isOpened(): | 168 if capture.isOpened(): |
| 170 fps = capture.get(cv2.cv.CV_CAP_PROP_FPS) | 169 fps = capture.get(cv2.cv.CV_CAP_PROP_FPS) |
| 208 key = cv2.waitKey() | 207 key = cv2.waitKey() |
| 209 if saveKey(key): | 208 if saveKey(key): |
| 210 cv2.imwrite('image.png', img) | 209 cv2.imwrite('image.png', img) |
| 211 frameNum += 1 | 210 frameNum += 1 |
| 212 cv2.destroyAllWindows() | 211 cv2.destroyAllWindows() |
| 212 | |
| 213 def computeHomographyFromPDTV(cameraFilename, method=0, ransacReprojThreshold=3.0): | |
| 214 '''Returns the homography matrix at ground level from PDTV format | |
| 215 https://bitbucket.org/hakanardo/pdtv''' | |
| 216 import pdtv | |
| 217 from numpy import array | |
| 218 camera = pdtv.load(cameraFilename) | |
| 219 srcPoints = [[x,y] for x, y in zip([1.,2.,2.,1.],[1.,1.,2.,2.])] # need floats!! | |
| 220 dstPoints = [] | |
| 221 for srcPoint in srcPoints: | |
| 222 projected = camera.image_to_world(tuple(srcPoint)) | |
| 223 dstPoints.append([projected[0], projected[1]]) | |
| 224 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method, ransacReprojThreshold) | |
| 225 return H | |
| 226 | |
| 213 | 227 |
| 214 def printCvMat(cvmat, out = stdout): | 228 def printCvMat(cvmat, out = stdout): |
| 215 '''Prints the cvmat to out''' | 229 '''Prints the cvmat to out''' |
| 216 print('Deprecated, use new interface') | 230 print('Deprecated, use new interface') |
| 217 for i in xrange(cvmat.rows): | 231 for i in xrange(cvmat.rows): |
