Mercurial Hosting > traffic-intelligence
annotate python/cvutils.py @ 117:fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 09 Aug 2011 15:10:31 -0400 |
parents | 2bf5b76320c0 |
children | 0f552c8b1650 |
rev | line source |
---|---|
28 | 1 #! /usr/bin/env python |
2 '''Image/Video utilities''' | |
3 | |
4 import Image, ImageDraw # PIL | |
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
5 try: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
6 import cv |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
7 opencvExists = True |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
8 except ImportError: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
9 print('OpenCV library could not be loaded') |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
10 opencvExists = False |
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
11 from sys import stdout |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
12 |
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
13 #import aggdraw # agg on top of PIL (antialiased drawing) |
28 | 14 #import utils |
15 | |
16 __metaclass__ = type | |
17 | |
18 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): | |
19 '''Draws lines over the image ''' | |
20 | |
21 img = Image.open(filename) | |
22 | |
23 draw = ImageDraw.Draw(img) | |
24 #draw = aggdraw.Draw(img) | |
25 #pen = aggdraw.Pen("red", width) | |
26 for p1, p2 in zip(origins, destinations): | |
27 draw.line([p1.x, p1.y, p2.x, p2.y], width = w, fill = (256,0,0)) | |
28 #draw.line([p1.x, p1.y, p2.x, p2.y], pen) | |
29 del draw | |
30 | |
31 #out = utils.openCheck(resultFilename) | |
32 img.save(resultFilename) | |
33 | |
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
34 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
35 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
36 cvSrcPoints = arrayToCvMat(srcPoints); |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
37 cvDstPoints = arrayToCvMat(dstPoints); |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
38 H = cv.CreateMat(3, 3, cv.CV_64FC1) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
39 cv.FindHomography(cvSrcPoints, cvDstPoints, H, method, ransacReprojThreshold) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
40 return H |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
41 |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
42 def cvMatToArray(cvmat): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
43 '''Converts an OpenCV CvMat to numpy array.''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
44 from numpy.core.multiarray import zeros |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
45 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
46 for i in xrange(cvmat.rows): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
47 for j in xrange(cvmat.cols): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
48 a[i,j] = cvmat[i,j] |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
49 return a |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
50 |
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
51 if opencvExists: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
52 def arrayToCvMat(a, t = cv.CV_64FC1): |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
53 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
54 cvmat = cv.CreateMat(a.shape[0], a.shape[1], t) |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
55 for i in range(cvmat.rows): |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
56 for j in range(cvmat.cols): |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
57 cvmat[i,j] = a[i,j] |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
58 return cvmat |
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
59 |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
60 def printCvMat(cvmat, out = stdout): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
61 '''Prints the cvmat to out''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
62 for i in xrange(cvmat.rows): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
63 for j in xrange(cvmat.cols): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
64 out.write('{0} '.format(cvmat[i,j])) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
65 out.write('\n') |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
66 |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
67 def projectArray(homography, points): |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
68 '''Returns the coordinates of the projected points (format 2xN points) |
28 | 69 through homography''' |
70 from numpy.core._dotblas import dot | |
71 from numpy.core.multiarray import array | |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
72 from numpy.lib.function_base import append |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
73 |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
74 if points.shape[0] != 2: |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
75 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
76 |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
77 if (homography!=None) and homography.size>0: |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
78 augmentedPoints = append(points,[[1]*points.shape[1]], 0) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
79 prod = dot(homography, augmentedPoints) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
80 return prod[0:2]/prod[2] |
28 | 81 else: |
82 return p | |
83 | |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
84 def project(homography, p): |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
85 '''Returns the coordinates of the projection of the point p |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
86 through homography''' |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
87 from numpy.core.multiarray import array |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
88 return projectArray(homography, array([[p[0]],p[1]])) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
89 |
28 | 90 def projectTrajectory(homography, trajectory): |
91 '''Projects a series of points in the format | |
92 [[x1, x2, ...], | |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
93 [y1, y2, ...]]''' |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
94 from numpy.core.multiarray import array |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
95 return projectArray(homography, array(trajectory)) |
28 | 96 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
97 def invertHomography(homography): |
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
98 'Returns an inverted homography' |
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
99 from numpy.linalg.linalg import inv |
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
100 invH = inv(homography) |
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
101 invH /= invH[2,2] |
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
102 return invH |
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
103 |
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
104 if opencvExists: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
105 def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)): |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
106 '''Computes the translation between of img2 with respect to img1 |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
107 (loaded using OpenCV) |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
108 img1Points are used to compute the translation |
100
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
109 |
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
110 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)''' |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
111 from numpy.core.multiarray import zeros |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
112 from numpy.lib.function_base import median |
100
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
113 |
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
114 (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) |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
115 |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
116 deltaX = [] |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
117 deltaY = [] |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
118 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)): |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
119 if status[k] == 1: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
120 dx = p2[0]-p1[0] |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
121 dy = p2[1]-p1[1] |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
122 d = dx**2 + dy**2 |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
123 if d < maxTranslation: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
124 deltaX.append(dx) |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
125 deltaY.append(dy) |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
126 if len(deltaX) >= 10: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
127 return [median(deltaX), median(deltaY)] |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
128 else: |
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
129 return None |