Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 99:e7dc5a780f09
added conversion functions and homography computation
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 12 Jul 2011 17:01:59 -0400 |
parents | b85912ab4064 |
children | 2a3cafcf5faf |
comparison
equal
deleted
inserted
replaced
98:b85912ab4064 | 99:e7dc5a780f09 |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 '''Image/Video utilities''' | 2 '''Image/Video utilities''' |
3 | 3 |
4 import Image, ImageDraw # PIL | 4 import Image, ImageDraw # PIL |
5 import cv | |
6 from sys import stdout | |
7 | |
5 #import aggdraw # agg on top of PIL (antialiased drawing) | 8 #import aggdraw # agg on top of PIL (antialiased drawing) |
6 from moving import Point | 9 from moving import Point |
7 #import utils | 10 #import utils |
8 | 11 |
9 __metaclass__ = type | 12 __metaclass__ = type |
21 #draw.line([p1.x, p1.y, p2.x, p2.y], pen) | 24 #draw.line([p1.x, p1.y, p2.x, p2.y], pen) |
22 del draw | 25 del draw |
23 | 26 |
24 #out = utils.openCheck(resultFilename) | 27 #out = utils.openCheck(resultFilename) |
25 img.save(resultFilename) | 28 img.save(resultFilename) |
29 | |
30 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): | |
31 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' | |
32 cvSrcPoints = arrayToCvMat(srcPoints); | |
33 cvDstPoints = arrayToCvMat(dstPoints); | |
34 H = cv.CreateMat(3, 3, cv.CV_64FC1) | |
35 cv.FindHomography(cvSrcPoints, cvDstPoints, H, method, ransacReprojThreshold) | |
36 return H | |
37 | |
38 def cvMatToArray(cvmat): | |
39 '''Converts an OpenCV CvMat to numpy array.''' | |
40 from numpy.core.multiarray import zeros | |
41 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) | |
42 for i in xrange(cvmat.rows): | |
43 for j in xrange(cvmat.cols): | |
44 a[i,j] = cvmat[i,j] | |
45 return a | |
46 | |
47 def arrayToCvMat(a, t = cv.CV_64FC1): | |
48 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' | |
49 cvmat = cv.CreateMat(a.shape[0], a.shape[1], t) | |
50 for i in range(cvmat.rows): | |
51 for j in range(cvmat.cols): | |
52 cvmat[i,j] = a[i,j] | |
53 return cvmat | |
54 | |
55 def printCvMat(cvmat, out = stdout): | |
56 '''Prints the cvmat to out''' | |
57 for i in xrange(cvmat.rows): | |
58 for j in xrange(cvmat.cols): | |
59 out.write('{0} '.format(cvmat[i,j])) | |
60 out.write('\n') | |
26 | 61 |
27 def projectArray(homography, points): | 62 def projectArray(homography, points): |
28 '''Returns the coordinates of the projected points (format 2xN points) | 63 '''Returns the coordinates of the projected points (format 2xN points) |
29 through homography''' | 64 through homography''' |
30 from numpy.core._dotblas import dot | 65 from numpy.core._dotblas import dot |