Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 472:a50c026fdf14
functions to compute inverse mapping
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 12 Mar 2014 17:36:49 -0400 |
| parents | 5304299e53a5 |
| children | 178c69ba2970 |
comparison
equal
deleted
inserted
replaced
| 471:a8f95bbd79bc | 472:a50c026fdf14 |
|---|---|
| 263 projected = camera.image_to_world(tuple(srcPoint)) | 263 projected = camera.image_to_world(tuple(srcPoint)) |
| 264 dstPoints.append([projected[0], projected[1]]) | 264 dstPoints.append([projected[0], projected[1]]) |
| 265 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method, ransacReprojThreshold) | 265 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method, ransacReprojThreshold) |
| 266 return H | 266 return H |
| 267 | 267 |
| 268 def correctedCoordinates(x,y, map1, map2): | 268 def correctedCoordinates(x,y, map1, map2, maxDistance = 1.): |
| 269 '''Returns the coordinates of a point in undistorted image | 269 '''Returns the coordinates of a point in undistorted image |
| 270 map1 and map2 are the mapping functions from undistorted image | 270 map1 and map2 are the mapping functions from undistorted image |
| 271 to distorted (original image) | 271 to distorted (original image) |
| 272 map1(x,y) = originalx, originaly''' | 272 map1(x,y) = originalx, originaly''' |
| 273 from numpy import abs, logical_and, unravel_index, dot, sum | 273 from numpy import abs, logical_and, unravel_index, dot, sum |
| 274 from matplotlib.mlab import find | 274 from matplotlib.mlab import find |
| 275 distx = abs(map1-x) | 275 distx = abs(map1-x) |
| 276 disty = abs(map2-y) | 276 disty = abs(map2-y) |
| 277 indices = logical_and(distx<1, disty<1) | 277 indices = logical_and(distx<maxDistance, disty<maxDistance) |
| 278 closeCoordinates = unravel_index(find(indices), distx.shape) # returns i,j, ie y,x | 278 closeCoordinates = unravel_index(find(indices), distx.shape) # returns i,j, ie y,x |
| 279 xWeights = 1-distx[indices] | 279 xWeights = 1-distx[indices] |
| 280 yWeights = 1-disty[indices] | 280 yWeights = 1-disty[indices] |
| 281 return dot(xWeights, closeCoordinates[1])/sum(xWeights), dot(yWeights, closeCoordinates[0])/sum(yWeights) | 281 return dot(xWeights, closeCoordinates[1])/sum(xWeights), dot(yWeights, closeCoordinates[0])/sum(yWeights) |
| 282 | 282 |
| 283 | 283 def computeInverseMapping(originalImageSize, map1, map2): |
| 284 'Computes inverse mapping from maps provided by cv2.initUndistortRectifyMap' | |
| 285 from numpy import ones, isnan | |
| 286 invMap1 = -ones(originalImageSize) | |
| 287 invMap2 = -ones(originalImageSize) | |
| 288 for x in range(0,originalImageSize[1]): | |
| 289 for y in range(0,originalImageSize[0]): | |
| 290 res = cvutils.correctedCoordinates(x,y, map1, map2) | |
| 291 if not isnan(res).any(): | |
| 292 invMap1[y,x] = res[0] | |
| 293 invMap2[y,x] = res[1] | |
| 294 return invMap1, invMap2 | |
| 295 | |
| 284 def printCvMat(cvmat, out = stdout): | 296 def printCvMat(cvmat, out = stdout): |
| 285 '''Prints the cvmat to out''' | 297 '''Prints the cvmat to out''' |
| 286 print('Deprecated, use new interface') | 298 print('Deprecated, use new interface') |
| 287 for i in xrange(cvmat.rows): | 299 for i in xrange(cvmat.rows): |
| 288 for j in xrange(cvmat.cols): | 300 for j in xrange(cvmat.cols): |
