Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 928:063d1267585d
work in progress
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 12 Jul 2017 01:24:31 -0400 |
parents | dbd81710d515 |
children | be28a3538dc9 |
comparison
equal
deleted
inserted
replaced
927:c030f735c594 | 928:063d1267585d |
---|---|
255 return fps | 255 return fps |
256 else: | 256 else: |
257 print('Video capture for {} failed'.format(videoFilename)) | 257 print('Video capture for {} failed'.format(videoFilename)) |
258 return None | 258 return None |
259 | 259 |
260 def imageBoxSize(obj, frameNum, homography, width, height, px = 0.2, py = 0.2): | 260 def imageBoxSize(obj, frameNum, width, height, px = 0.2, py = 0.2): |
261 'Computes the bounding box size of object at frameNum' | 261 'Computes the bounding box size of object at frameNum' |
262 x = [] | 262 x = [] |
263 y = [] | 263 y = [] |
264 if obj.hasFeatures(): | 264 if obj.hasFeatures(): |
265 for f in obj.getFeatures(): | 265 for f in obj.getFeatures(): |
266 if f.existsAtInstant(frameNum): | 266 if f.existsAtInstant(frameNum): |
267 projectedPosition = f.getPositionAtInstant(frameNum).project(homography) | 267 projectedPosition = f.projectPositions[:, frameNum-f.getFirstInstant()] |
268 x.append(projectedPosition.x) | 268 x.append(projectedPosition[0]) |
269 y.append(projectedPosition.y) | 269 y.append(projectedPosition[1]) |
270 xmin = min(x) | 270 xmin = min(x) |
271 xmax = max(x) | 271 xmax = max(x) |
272 ymin = min(y) | 272 ymin = min(y) |
273 ymax = max(y) | 273 ymax = max(y) |
274 xMm = px * (xmax - xmin) | 274 xMm = px * (xmax - xmin) |
278 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a))) | 278 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a))) |
279 xCropMin = int(max(0, .5 * (xmin + xmax - a))) | 279 xCropMin = int(max(0, .5 * (xmin + xmax - a))) |
280 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a))) | 280 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a))) |
281 return yCropMin, yCropMax, xCropMin, xCropMax | 281 return yCropMin, yCropMax, xCropMin, xCropMax |
282 | 282 |
283 def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, minNPixels = 800): | 283 def imageBox(img, obj, frameNum, width, height, px = 0.2, py = 0.2, minNPixels = 800): |
284 'Computes the bounding box of object at frameNum' | 284 'Computes the bounding box of object at frameNum' |
285 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height, px, py) | 285 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, width, height, px, py) |
286 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels: | 286 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels: |
287 return img[yCropMin : yCropMax, xCropMin : xCropMax] | 287 return img[yCropMin : yCropMax, xCropMin : xCropMax] |
288 else: | 288 else: |
289 return None | 289 return None |
290 | 290 |
516 for i in xrange(cvmat.rows): | 516 for i in xrange(cvmat.rows): |
517 for j in xrange(cvmat.cols): | 517 for j in xrange(cvmat.cols): |
518 out.write('{0} '.format(cvmat[i,j])) | 518 out.write('{0} '.format(cvmat[i,j])) |
519 out.write('\n') | 519 out.write('\n') |
520 | 520 |
521 def projectArray(homography, points): | 521 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None): |
522 '''Returns the coordinates of the projected points through homography | 522 '''Returns the coordinates of the projected points through homography |
523 (format: array 2xN points)''' | 523 (format: array 2xN points)''' |
524 if points.shape[0] != 2: | 524 if points.shape[0] not in [2, 3]: |
525 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) | 525 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) |
526 | 526 |
527 if (homography is not None) and homography.size>0: | 527 augmentedPoints = append(points,[[1]*points.shape[1]], 0) |
528 #alternatively, on could use cv2.convertpointstohomogeneous and other conversion to/from homogeneous coordinates | 528 if homography is not None and homography.size>0: |
529 augmentedPoints = append(points,[[1]*points.shape[1]], 0) | |
530 prod = dot(homography, augmentedPoints) | 529 prod = dot(homography, augmentedPoints) |
531 return prod[0:2]/prod[2] | 530 projected = prod/prod[2] |
531 projected[3,:] = 0 | |
532 else: | 532 else: |
533 return points | 533 projected = augmentedPoints |
534 | |
535 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: | |
536 projected = cv2.projectPoints(projected, None, None, intrinsicCameraMatrix, distortionCoefficients) | |
537 return projected | |
534 | 538 |
535 def project(homography, p): | 539 def project(homography, p): |
536 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] | 540 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] |
537 through homography''' | 541 through homography''' |
538 return projectArray(homography, array([[p[0]],[p[1]]])) | 542 return projectArray(homography, array([[p[0]],[p[1]]])) |